-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (39 loc) · 1.23 KB
/
main.py
File metadata and controls
56 lines (39 loc) · 1.23 KB
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
import uvicorn
from typing import cast
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, status, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from app.routers import api_router
from core.database import init_db, db_session
@asynccontextmanager
async def lifespan(app_: FastAPI):
# 初始化数据库
await init_db()
# 创建默认权限
async with db_session() as async_session:
...
yield
app = FastAPI(lifespan=lifespan)
# Customize the return format of Exception
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail}
)
# 配置 CORS
origins = [
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许的前端域名
allow_credentials=True,
allow_methods=["*"], # 允许所有方法,如 GET、POST 等
allow_headers=["*"], # 允许所有请求头
)
# 注册路由
app.include_router(api_router, prefix="/api/v1")
if __name__ == '__main__':
uvicorn.run("main:app", host="0.0.0.0", port=8002, reload=True)