forked from cofin/fastapi-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (30 loc) · 983 Bytes
/
main.py
File metadata and controls
41 lines (30 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Third Party Libraries
from starlette.applications import Starlette
from starlette.routing import Mount, Route
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
# Fastapi Vite
from fastapi_vite_dara import runner, settings, vite_asset, vite_hmr_client
templates = Jinja2Templates(directory='assets/templates')
templates.env.globals['vite_hmr_client'] = vite_hmr_client
templates.env.globals['vite_asset'] = vite_asset
async def homepage(request):
return templates.TemplateResponse('index.html', {'request': request})
async def on_startup():
await runner.ViteRunner.start()
async def on_shutdown():
await runner.ViteRunner.stop()
routes = [
Route('/', endpoint=homepage),
Mount(
settings.static_url,
StaticFiles(directory=settings.static_path),
name='static',
),
]
app = Starlette(
debug=True,
routes=routes,
on_startup=[on_startup],
on_shutdown=[on_shutdown],
)