-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
71 lines (59 loc) · 1.93 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
import logging
import warnings
from app.api.md import router as md_router
from app.api.uvdoc import router as uvdoc_router
# 配置警告过滤
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=ResourceWarning)
# 更多具体的警告过滤
warnings.filterwarnings("ignore", message=".*swigvarlink.*")
warnings.filterwarnings("ignore", message=".*unclosed.*SSLSocket.*")
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动时执行
from app.api.uvdoc import load_model_fn
load_model_fn() # 加载模型
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(md_router)
app.include_router(uvdoc_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()