from fastapi import FastAPI, Depends from fastapi.middleware.cors import CORSMiddleware from config import settings from routers import vehicles_router, fuel_records_router, costs_router from auth import get_current_user # 创建 FastAPI 应用 app = FastAPI(title=settings.TITLE, version=settings.VERSION, debug=settings.DEBUG) # 配置 CORS app.add_middleware( CORSMiddleware, allow_credentials=True, allow_origin_regex=r"http[s]?://172\.30\.0\..*:5173|http[s]?://.*\.yuany3721\.site|http[s]?://yuany3721\.site", allow_methods=["GET", "POST"], allow_headers=["*"], ) # 注册路由 - 添加 /carcost 前缀,并启用认证保护 # 所有路由都需要认证 dependencies = [Depends(get_current_user)] app.include_router(vehicles_router, prefix="/carcost", dependencies=dependencies) app.include_router(fuel_records_router, prefix="/carcost", dependencies=dependencies) app.include_router(costs_router, prefix="/carcost", dependencies=dependencies) @app.get("/") def root(): """根路径""" return { "message": "Welcome to CarCost API", "version": settings.VERSION, "docs": "/docs", "auth": "Protected by Authentik OIDC", } @app.get("/health") def health_check(): """健康检查""" return {"status": "ok"} if __name__ == "__main__": import uvicorn uvicorn.run("main:app", host="0.0.0.0", port=7030, reload=settings.DEBUG)