-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve_dev.py
More file actions
51 lines (42 loc) · 1.22 KB
/
serve_dev.py
File metadata and controls
51 lines (42 loc) · 1.22 KB
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
42
43
44
45
46
47
48
49
50
51
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from data import DATA_DIR
from build import TEMPLATE_DIR, TEMPLATE_STATIC_DIR, get_context_data
app = FastAPI()
templates = Jinja2Templates(directory=TEMPLATE_DIR)
context_data = get_context_data()
# Mount the "static" directory to serve static files
app.mount(
"/static",
StaticFiles(directory=TEMPLATE_STATIC_DIR),
name="static",
)
# Mount the "api" directory to serve data files
app.mount(
"/api",
StaticFiles(directory=DATA_DIR),
name="api",
)
# Favicon must be in the root for browsers to find it
@app.get('/favicon.ico')
def get_favicon():
favicon_path = TEMPLATE_STATIC_DIR / 'favicon.ico'
if favicon_path.exists():
return FileResponse(
path=favicon_path,
filename='favicon.ico',
)
# Define root path
@app.get('/')
def get_root(request: Request):
return templates.TemplateResponse(
name='index.html',
context=context_data,
request=request,
)
# Run with uvicorn
if __name__ == '__main__':
uvicorn.run("serve_dev:app", port=8000)