main.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # app/main.py
  2. from fastapi import FastAPI
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from app.core.config import settings
  5. from app.api.endpoints import router
  6. from app.core.exception_handler import register_exception_handlers
  7. app = FastAPI(
  8. title=settings.PROJECT_NAME,
  9. openapi_url=f"{settings.API_V1_STR}/openapi.json"
  10. )
  11. # 注册全局异常处理器(必须在路由注册之前)
  12. register_exception_handlers(app)
  13. # CORS 配置
  14. cors_origins = settings.CORS_ORIGINS.split(",") if settings.CORS_ORIGINS != "*" else ["*"]
  15. app.add_middleware(
  16. CORSMiddleware,
  17. allow_origins=[origin.strip() for origin in cors_origins],
  18. allow_credentials=True,
  19. allow_methods=["*"],
  20. allow_headers=["*"],
  21. )
  22. # 注册路由
  23. app.include_router(router, prefix=settings.API_V1_STR) # 前缀 /api
  24. @app.get("/")
  25. async def root():
  26. return {"message": "MinIO FileManager Backend is Running"}
  27. @app.get("/health")
  28. async def health_check():
  29. """健康检查端点"""
  30. return {"status": "healthy"}
  31. if __name__ == "__main__":
  32. import uvicorn
  33. uvicorn.run("app.main:app", host="0.0.0.0", port=9002, reload=True)