-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (61 loc) · 1.91 KB
/
main.py
File metadata and controls
78 lines (61 loc) · 1.91 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import logging
import os
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from module.api import v1
from module.conf import VERSION, settings, setup_logger
setup_logger(reset=True)
logger = logging.getLogger(__name__)
uvicorn_logging_config = {
"version": 1,
"disable_existing_loggers": False,
"handlers": logger.handlers,
"loggers": {
"uvicorn": {
"level": logger.level,
},
"uvicorn.access": {
"level": "WARNING",
},
},
}
def create_app() -> FastAPI:
app = FastAPI()
# mount routers
app.include_router(v1, prefix="/api")
return app
app = create_app()
@app.get("/posters/{path:path}", tags=["posters"])
def posters(path: str):
return FileResponse(f"data/posters/{path}")
if VERSION != "DEV_VERSION":
app.mount("/assets", StaticFiles(directory="dist/assets"), name="assets")
app.mount("/images", StaticFiles(directory="dist/images"), name="images")
# app.mount("/icons", StaticFiles(directory="dist/icons"), name="icons")
templates = Jinja2Templates(directory="dist")
@app.get("/{path:path}")
def html(request: Request, path: str):
files = os.listdir("dist")
if path in files:
return FileResponse(f"dist/{path}")
else:
context = {"request": request}
return templates.TemplateResponse("index.html", context)
else:
@app.get("/", status_code=302, tags=["html"])
def index():
return RedirectResponse("/docs")
if __name__ == "__main__":
if os.getenv("IPV6"):
host = "::"
else:
host = os.getenv("HOST", "0.0.0.0")
uvicorn.run(
app,
host=host,
port=settings.program.webui_port,
log_config=uvicorn_logging_config,
)