增加接口文档禁用
This commit is contained in:
@@ -4,19 +4,19 @@ from pydantic_settings import BaseSettings
|
|||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
# 数据库配置
|
# 数据库配置
|
||||||
database_url: str = ""
|
database_url: str = ""
|
||||||
|
|
||||||
# Redis配置
|
# Redis配置
|
||||||
redis_url: str = "redis://localhost:6379/0"
|
redis_url: str = "redis://localhost:6379/0"
|
||||||
redis_lock_timeout: int = 30 # 锁超时时间(秒)
|
redis_lock_timeout: int = 30 # 锁超时时间(秒)
|
||||||
redis_lock_retry_delay: float = 0.1 # 锁重试延迟(秒)
|
redis_lock_retry_delay: float = 0.1 # 锁重试延迟(秒)
|
||||||
redis_lock_max_retries: int = 100 # 最大重试次数
|
redis_lock_max_retries: int = 100 # 最大重试次数
|
||||||
|
|
||||||
# 业务配置
|
# 业务配置
|
||||||
count_threshold: int = 3 # count阈值,大于等于此值直接返回
|
count_threshold: int = 3 # count阈值,大于等于此值直接返回
|
||||||
external_api_enabled: bool = False # 是否启用外部API调用
|
external_api_enabled: bool = False # 是否启用外部API调用
|
||||||
external_api_retry_max: int = 3 # 外部API最大重试次数
|
external_api_retry_max: int = 3 # 外部API最大重试次数
|
||||||
external_api_url: str = ""
|
external_api_url: str = ""
|
||||||
|
|
||||||
# 日志配置
|
# 日志配置
|
||||||
log_level: str = "INFO" # DEBUG, INFO, WARNING, ERROR, CRITICAL
|
log_level: str = "INFO" # DEBUG, INFO, WARNING, ERROR, CRITICAL
|
||||||
log_file: str = "logs/app.log" # 日志文件路径
|
log_file: str = "logs/app.log" # 日志文件路径
|
||||||
@@ -24,15 +24,16 @@ class Settings(BaseSettings):
|
|||||||
log_backup_count: int = 5 # 备份文件数量
|
log_backup_count: int = 5 # 备份文件数量
|
||||||
log_format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
log_format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||||
log_date_format: str = "%Y-%m-%d %H:%M:%S"
|
log_date_format: str = "%Y-%m-%d %H:%M:%S"
|
||||||
|
|
||||||
# 应用配置
|
# 应用配置
|
||||||
app_name: str = "AI Talk Callback API"
|
app_name: str = "AI Talk Callback API"
|
||||||
environment: str = "production" # 环境: production, test, development
|
environment: str = "production" # 环境: production, test, development
|
||||||
debug: bool = False
|
debug: bool = False
|
||||||
|
disable_docs: bool = False
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
case_sensitive = False
|
case_sensitive = False
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|||||||
40
main.py
40
main.py
@@ -13,29 +13,30 @@ from app.logger import get_logger, LoggerManager
|
|||||||
LoggerManager.setup_logging()
|
LoggerManager.setup_logging()
|
||||||
logger = get_logger("main")
|
logger = get_logger("main")
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# 启动时初始化
|
# 启动时初始化
|
||||||
logger.info("🚀 应用启动中...")
|
logger.info("🚀 应用启动中...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 初始化数据库
|
# 初始化数据库
|
||||||
logger.info("📊 初始化数据库连接...")
|
logger.info("📊 初始化数据库连接...")
|
||||||
await init_db()
|
await init_db()
|
||||||
logger.info("✅ 数据库连接成功")
|
logger.info("✅ 数据库连接成功")
|
||||||
|
|
||||||
# 初始化Redis
|
# 初始化Redis
|
||||||
logger.info("🔴 初始化Redis连接...")
|
logger.info("🔴 初始化Redis连接...")
|
||||||
await redis_manager.connect()
|
await redis_manager.connect()
|
||||||
logger.info("✅ Redis连接成功")
|
logger.info("✅ Redis连接成功")
|
||||||
|
|
||||||
logger.info(f"🎉 {settings.app_name} 启动完成!")
|
logger.info(f"🎉 {settings.app_name} 启动完成!")
|
||||||
yield
|
yield
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"❌ 应用启动失败: {e}")
|
logger.error(f"❌ 应用启动失败: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# 关闭时清理
|
# 关闭时清理
|
||||||
logger.info("🛑 应用关闭中...")
|
logger.info("🛑 应用关闭中...")
|
||||||
@@ -44,14 +45,25 @@ async def lifespan(app: FastAPI):
|
|||||||
logger.info("✅ Redis连接已关闭")
|
logger.info("✅ Redis连接已关闭")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"❌ Redis关闭时出错: {e}")
|
logger.error(f"❌ Redis关闭时出错: {e}")
|
||||||
|
|
||||||
logger.info("👋 应用已关闭")
|
logger.info("👋 应用已关闭")
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title=settings.app_name,
|
title=settings.app_name,
|
||||||
version="1.0.0",
|
version="1.0.0",
|
||||||
lifespan=lifespan
|
lifespan=lifespan,
|
||||||
|
docs_url=(
|
||||||
|
"/docs"
|
||||||
|
if not settings.disable_docs and settings.environment != "production"
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
redoc_url=(
|
||||||
|
"/redoc"
|
||||||
|
if not settings.disable_docs and settings.environment != "production"
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
openapi_url="/openapi.json" if not settings.disable_docs else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 添加CORS中间件
|
# 添加CORS中间件
|
||||||
@@ -73,8 +85,9 @@ async def root():
|
|||||||
if settings.environment == "production":
|
if settings.environment == "production":
|
||||||
logger.warning("🚫 生产环境下禁止访问根接口")
|
logger.warning("🚫 生产环境下禁止访问根接口")
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
|
||||||
raise HTTPException(status_code=404, detail="Not Found")
|
raise HTTPException(status_code=404, detail="Not Found")
|
||||||
|
|
||||||
logger.info("📝 根接口被访问")
|
logger.info("📝 根接口被访问")
|
||||||
return {"message": f"Welcome to {settings.app_name}"}
|
return {"message": f"Welcome to {settings.app_name}"}
|
||||||
|
|
||||||
@@ -85,17 +98,14 @@ async def health_check():
|
|||||||
if settings.environment == "production":
|
if settings.environment == "production":
|
||||||
logger.warning("🚫 生产环境下禁止访问健康检查接口")
|
logger.warning("🚫 生产环境下禁止访问健康检查接口")
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
|
||||||
raise HTTPException(status_code=404, detail="Not Found")
|
raise HTTPException(status_code=404, detail="Not Found")
|
||||||
|
|
||||||
logger.debug("💓 健康检查接口被访问")
|
logger.debug("💓 健康检查接口被访问")
|
||||||
return {"status": "healthy"}
|
return {"status": "healthy"}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
uvicorn.run(
|
|
||||||
"main:app",
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=settings.debug)
|
||||||
host="0.0.0.0",
|
|
||||||
port=8000,
|
|
||||||
reload=settings.debug
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user