# app/main.py - 简化版本 from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.logging_config import setup_logging_with_rotation import logging import os # 配置 from .config import settings # 配置日志 logging.basicConfig( level=logging.INFO, # format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', # datefmt='%Y-%m-%d %H:%M:%S', # handlers=[ # logging.StreamHandler(), # 控制台 # logging.FileHandler('app/logs/app.log', encoding='utf-8') # 文件 # ] ) # 为特定模块设置更详细的日志 logging.getLogger("app.api.v1").setLevel(logging.DEBUG) logging.getLogger("app.core.security").setLevel(logging.DEBUG) logger = logging.getLogger(__name__) logger.info("✅ 日志配置完成") # logger = setup_logging_with_rotation # 创建 FastAPI 应用 app = FastAPI( title=settings.PROJECT_NAME, version=settings.VERSION, docs_url="/docs" if settings.DEBUG else None, redoc_url="/redoc" if settings.DEBUG else None ) # 配置CORS if settings.BACKEND_CORS_ORIGINS: app.add_middleware( CORSMiddleware, allow_origins=settings.BACKEND_CORS_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 导入并注册路由 try: # 导入所有路由模块 # from .api.v1.admin import router as admin_router from .api.v1.auth import router as auth_router from .api.v1.users import router as users_router # from .api.v1.verify import router as verify_router # 注册路由 app.include_router(auth_router, prefix=settings.API_V1_PREFIX) app.include_router(users_router, prefix=settings.API_V1_PREFIX) # app.include_router(verify_router, prefix=settings.API_V1_PREFIX) logger.info("✅ API 路由注册成功") except ImportError as e: logger.warning(f"⚠️ 部分路由模块未找到: {e}") # 系统级路由 @app.get("/health") async def health_check(): return {"status": "healthy", "service": settings.PROJECT_NAME} @app.get("/") async def root(): return { "message": f"Welcome to {settings.PROJECT_NAME} API", "version": settings.VERSION, "docs": "/docs" } if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=10003, reload=settings.DEBUG)