-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.py
46 lines (34 loc) · 1.53 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
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from route.route_chat import router as chat_router
class CustomCORSMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
origin = request.headers.get('origin')
if not origin:
origin = "*"
if request.method == "OPTIONS":
response = Response()
response.status_code = 204
response.headers['Access-Control-Allow-Origin'] = origin
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Authorization, Content-Type, X-Requested-With'
response.headers['Access-Control-Allow-Credentials'] = 'true'
return response
response = await call_next(request)
# 检查请求来源,并动态设置响应头
response.headers['Access-Control-Allow-Origin'] = origin
response.headers['Access-Control-Allow-Credentials'] = 'true'
response.headers['Access-Control-Allow-Methods'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
return response
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup code here
print("Starting up...")
yield
# Shutdown code he
print("Shutting down...")
app = FastAPI(lifespan=lifespan)
app.add_middleware(CustomCORSMiddleware)
app.include_router(chat_router)