feat: new file
This commit is contained in:
43
backend/app/main.py
Normal file
43
backend/app/main.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from sqlalchemy.exc import OperationalError
|
||||
|
||||
from .settings import settings
|
||||
from .routes import ai, debug, metrics, trend
|
||||
from .services.db_sample import print_db_sample_to_logs
|
||||
|
||||
|
||||
app = FastAPI(title="Crawl BI Backend", version="0.1.0")
|
||||
|
||||
@app.exception_handler(OperationalError)
|
||||
async def db_operational_error_handler(_: Request, exc: OperationalError):
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={
|
||||
"detail": "数据库连接失败(请检查 MYSQL_HOST/USER/PASSWORD 以及 MySQL 授权 host/IP 白名单)。",
|
||||
"error": str(exc.orig) if getattr(exc, "orig", None) else str(exc),
|
||||
},
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=settings.cors_origins_list,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(metrics.router, prefix="/api/metrics", tags=["metrics"])
|
||||
app.include_router(trend.router, prefix="/api/trend", tags=["trend"])
|
||||
app.include_router(ai.router, prefix="/api/ai", tags=["ai"])
|
||||
app.include_router(debug.router, prefix="/api/debug", tags=["debug"])
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def _startup_print_sample() -> None:
|
||||
if settings.debug_print_db_sample:
|
||||
print_db_sample_to_logs()
|
||||
|
||||
Reference in New Issue
Block a user