44 lines
1.3 KiB
Python
Raw Normal View History

2025-04-04 18:07:48 +08:00
import os
2025-04-03 22:11:20 +08:00
import uvicorn
2025-04-04 18:07:48 +08:00
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, Response
from loguru import logger
from starlette.staticfiles import StaticFiles
from starlette.types import Scope
2025-04-03 22:11:20 +08:00
from .controller.domain import router as domain_router
from .controller.report import router as report_router
from .controller.status import router as status_router
2025-04-04 18:07:48 +08:00
class SPAStaticFiles(StaticFiles):
async def get_response(self, path: str, scope: Scope) -> Response:
# 如果是前端路由,直接返回 index.html否则直接访问的时候会404
if path in ("domain", "url"):
return FileResponse(os.path.join(self.directory, "index.html"))
return await super().get_response(path, scope)
2025-04-03 22:11:20 +08:00
class WebApp:
def __init__(self):
self.app = FastAPI()
@staticmethod
async def start():
app = FastAPI()
# 导入路由
app.include_router(status_router)
app.include_router(report_router)
app.include_router(domain_router)
2025-04-04 18:07:48 +08:00
# 挂载前端文件
app.mount("/", SPAStaticFiles(directory="fe/dist", html=True), name="static")
2025-04-03 22:11:20 +08:00
# TODO 先写死,后面从配置文件里取
cfg = uvicorn.Config(app, host="127.0.0.1", port=3000)
server = uvicorn.Server(cfg)
await server.serve()