-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (67 loc) · 2.18 KB
/
main.py
File metadata and controls
84 lines (67 loc) · 2.18 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
79
80
81
82
83
84
import logging
import os
from pathlib import Path
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from module.api import lifespan, v1
from module.conf import VERSION, settings, setup_logger
from module.network import load_image
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(lifespan=lifespan)
# mount routers
app.include_router(v1, prefix="/api")
return app
app = create_app()
@app.get("/posters/{path:path}", tags=["posters"])
async def posters(path: str):
# TODO: 由于只有取的时候才会下载,所以会导致第一次请求的时候没有图片
post_path = Path("data/posters") / path
if not post_path.exists():
await load_image(path)
return FileResponse(post_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,
)