46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from config import settings
|
|
from routers import vehicles_router, fuel_records_router, costs_router, dashboard_router
|
|
|
|
# 创建 FastAPI 应用
|
|
app = FastAPI(title=settings.TITLE, version=settings.VERSION, debug=settings.DEBUG)
|
|
|
|
# 配置 CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # 生产环境应该限制具体域名
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册路由 - 添加 /carcost 前缀
|
|
app.include_router(vehicles_router, prefix="/carcost")
|
|
app.include_router(fuel_records_router, prefix="/carcost")
|
|
app.include_router(costs_router, prefix="/carcost")
|
|
app.include_router(dashboard_router, prefix="/carcost")
|
|
|
|
|
|
@app.get("/")
|
|
def root():
|
|
"""根路径"""
|
|
return {
|
|
"message": "Welcome to CarCost API",
|
|
"version": settings.VERSION,
|
|
"docs": "/docs",
|
|
}
|
|
|
|
|
|
@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)
|