增加外部接口调用开关
This commit is contained in:
27
.env
Normal file
27
.env
Normal file
@@ -0,0 +1,27 @@
|
||||
# 数据库配置
|
||||
DATABASE_URL=postgresql+asyncpg://postgres:12345@localhost:5432/ai_talk_callback_db
|
||||
|
||||
# Redis配置
|
||||
REDIS_URL=redis://localhost:6379/0
|
||||
REDIS_LOCK_TIMEOUT=30
|
||||
REDIS_LOCK_RETRY_DELAY=0.1
|
||||
REDIS_LOCK_MAX_RETRIES=100
|
||||
|
||||
# 业务配置
|
||||
COUNT_THRESHOLD=3
|
||||
EXTERNAL_API_ENABLED=false
|
||||
EXTERNAL_API_RETRY_MAX=3
|
||||
EXTERNAL_API_URL=https://127.0.0.1/api/openapi/customerApi/aiTaskResultFail
|
||||
|
||||
# 日志配置
|
||||
LOG_LEVEL=INFO
|
||||
LOG_FILE=logs/app.log
|
||||
LOG_MAX_BYTES=10485760
|
||||
LOG_BACKUP_COUNT=5
|
||||
LOG_FORMAT=%(asctime)s - %(name)s - %(levelname)s - %(message)s
|
||||
LOG_DATE_FORMAT=%Y-%m-%d %H:%M:%S
|
||||
|
||||
# 应用配置
|
||||
APP_NAME=AI Talk Callback API
|
||||
ENVIRONMENT=development
|
||||
DEBUG=false
|
||||
@@ -9,6 +9,7 @@ REDIS_LOCK_MAX_RETRIES=100
|
||||
|
||||
# 业务配置
|
||||
COUNT_THRESHOLD=3
|
||||
EXTERNAL_API_ENABLED=false
|
||||
EXTERNAL_API_RETRY_MAX=3
|
||||
EXTERNAL_API_URL=https://jeep-api.d2c.stlassac.com/api/openapi/customerApi/aiTaskResultFail
|
||||
|
||||
@@ -22,4 +23,5 @@ LOG_DATE_FORMAT=%Y-%m-%d %H:%M:%S
|
||||
|
||||
# 应用配置
|
||||
APP_NAME=AI Talk Callback API
|
||||
ENVIRONMENT=production # 环境: production, test, development
|
||||
DEBUG=false
|
||||
@@ -13,6 +13,7 @@ class Settings(BaseSettings):
|
||||
|
||||
# 业务配置
|
||||
count_threshold: int = 3 # count阈值,大于等于此值直接返回
|
||||
external_api_enabled: bool = False # 是否启用外部API调用
|
||||
external_api_retry_max: int = 3 # 外部API最大重试次数
|
||||
external_api_url: str = ""
|
||||
|
||||
@@ -26,6 +27,7 @@ class Settings(BaseSettings):
|
||||
|
||||
# 应用配置
|
||||
app_name: str = "AI Talk Callback API"
|
||||
environment: str = "production" # 环境: production, test, development
|
||||
debug: bool = False
|
||||
|
||||
class Config:
|
||||
|
||||
@@ -256,6 +256,17 @@ async def ai_talk_callback(
|
||||
site_id=siteId
|
||||
)
|
||||
|
||||
# 检查是否启用外部API调用
|
||||
if not settings.external_api_enabled:
|
||||
logger.info(f"🔌 外部API调用已禁用,直接返回成功")
|
||||
return CallbackResponse(
|
||||
success=True,
|
||||
message="外部API调用已禁用,直接返回",
|
||||
processed=False,
|
||||
retry_count=0,
|
||||
site_id=siteId
|
||||
)
|
||||
|
||||
logger.info(f"📞 count={callback_data.count} < {settings.count_threshold},调用外部API")
|
||||
|
||||
# count < 3,调用外部API,使用分布式锁防止并发调用
|
||||
|
||||
12
main.py
12
main.py
@@ -69,12 +69,24 @@ app.include_router(router)
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
# 检查环境,生产环境下禁用根接口
|
||||
if settings.environment == "production":
|
||||
logger.warning("🚫 生产环境下禁止访问根接口")
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Not Found")
|
||||
|
||||
logger.info("📝 根接口被访问")
|
||||
return {"message": f"Welcome to {settings.app_name}"}
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
# 检查环境,生产环境下禁用健康检查接口
|
||||
if settings.environment == "production":
|
||||
logger.warning("🚫 生产环境下禁止访问健康检查接口")
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Not Found")
|
||||
|
||||
logger.debug("💓 健康检查接口被访问")
|
||||
return {"status": "healthy"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user