加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.py 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
from pathlib import Path
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi import HTTPException
from app.api.endpoints import router
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动时执行
current_dir = Path(__file__).parent
banner_path = current_dir / 'app' / 'banner.txt'
print(f"Looking for banner at: {banner_path.absolute()}")
try:
with open(banner_path, 'r', encoding='utf-8') as f:
banner = f.read()
print(banner)
except FileNotFoundError:
print(f"Banner file not found at {banner_path}, starting server without banner...")
yield
def create_app() -> FastAPI:
app = FastAPI(
lifespan=lifespan
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
app.include_router(router)
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
return app
app = create_app()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化