carcost/api/main.py
yuany3721 71e11eaf30 feat: Add OIDC authentication with Authentik and refactor project structure
Backend:
- Add auth.py for JWT token verification
- Update main.py to protect all routes with auth middleware
- Remove dashboard router (frontend handles aggregation)
- Add Docker support with Dockerfile and docker-compose.yml

Frontend:
- Add OIDC authentication using oidc-client-ts with PKCE flow
- Create router.js with auth guards for automatic login/logout
- Add api.js for unified Axios instance with auth headers
- Add composables: useAuth.js, useVehicleData.js for caching
- Add views/Main.vue as main application page
- Simplify App.vue to router-view container
- Add deploy-web.sh deployment script

Documentation:
- Update AGENTS.md with new architecture and auth flow
2026-04-12 13:31:27 +08:00

50 lines
1.4 KiB
Python

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)