增加接口文档禁用
This commit is contained in:
@@ -29,6 +29,7 @@ class Settings(BaseSettings):
|
|||||||
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"
|
||||||
|
|||||||
24
main.py
24
main.py
@@ -13,6 +13,7 @@ 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):
|
||||||
# 启动时初始化
|
# 启动时初始化
|
||||||
@@ -51,7 +52,18 @@ async def lifespan(app: FastAPI):
|
|||||||
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,6 +85,7 @@ 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("📝 根接口被访问")
|
||||||
@@ -85,6 +98,7 @@ 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("💓 健康检查接口被访问")
|
||||||
@@ -93,9 +107,5 @@ async def health_check():
|
|||||||
|
|
||||||
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