feat: 后端支持托管到 Metrix,启动自动安装并接入平台登录
This commit is contained in:
+31
-11
@@ -1,15 +1,17 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.responses import FileResponse, JSONResponse
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from app.api.http import router
|
||||
from app.auth.platform import base_href
|
||||
from app.bootstrap import init_db
|
||||
from app.config import installed
|
||||
|
||||
@@ -25,6 +27,7 @@ WEB_DIST = next(
|
||||
),
|
||||
Path(__file__).resolve().parents[2] / "web" / "dist",
|
||||
)
|
||||
BASE_TAG = re.compile(r"<base\b[^>]*>", re.IGNORECASE)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -33,10 +36,18 @@ async def lifespan(_application: FastAPI):
|
||||
yield
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
def render_index(html: str, href: str) -> str:
|
||||
tag = f'<base href="{href}" />'
|
||||
if BASE_TAG.search(html):
|
||||
return BASE_TAG.sub(tag, html, count=1)
|
||||
return html.replace("<head>", f"<head>\n {tag}", 1)
|
||||
|
||||
|
||||
def create_app(web_dist: Path | None = None) -> FastAPI:
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
application = FastAPI(title="DecomEval", lifespan=lifespan)
|
||||
application.include_router(router)
|
||||
dist = (web_dist or WEB_DIST).resolve()
|
||||
|
||||
@application.middleware("http")
|
||||
async def require_install(request, call_next):
|
||||
@@ -45,17 +56,26 @@ def create_app() -> FastAPI:
|
||||
return JSONResponse({"detail": "尚未安装"}, status_code=503)
|
||||
return await call_next(request)
|
||||
|
||||
if (WEB_DIST / "index.html").is_file():
|
||||
application.mount("/assets", StaticFiles(directory=WEB_DIST / "assets"), name="assets")
|
||||
if (dist / "index.html").is_file():
|
||||
if (dist / "assets").is_dir():
|
||||
application.mount("/assets", StaticFiles(directory=dist / "assets"), name="assets")
|
||||
|
||||
@application.get("/{path:path}")
|
||||
def pages(path: str) -> FileResponse:
|
||||
def index(request: Request) -> HTMLResponse:
|
||||
html = (dist / "index.html").read_text(encoding="utf-8")
|
||||
return HTMLResponse(
|
||||
render_index(html, base_href(request.headers)),
|
||||
headers={"Cache-Control": "no-store", "Vary": "X-Forwarded-Prefix"},
|
||||
)
|
||||
|
||||
@application.get("/{path:path}", response_model=None)
|
||||
def pages(path: str, request: Request) -> FileResponse | HTMLResponse:
|
||||
if path == "api" or path.startswith("api/"):
|
||||
raise HTTPException(status_code=404, detail="接口不存在")
|
||||
target = WEB_DIST / path
|
||||
if path and target.is_file():
|
||||
return FileResponse(target)
|
||||
return FileResponse(WEB_DIST / "index.html")
|
||||
if path and path != "index.html":
|
||||
target = (dist / path).resolve()
|
||||
if target.is_file() and target.is_relative_to(dist):
|
||||
return FileResponse(target)
|
||||
return index(request)
|
||||
|
||||
return application
|
||||
|
||||
|
||||
Reference in New Issue
Block a user