2024-01-26 10:46:13 +08:00
|
|
|
from fastapi import FastAPI
|
2024-01-26 09:57:55 +08:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
|
2024-01-26 10:46:13 +08:00
|
|
|
def add_cors_middleware(app: FastAPI):
|
|
|
|
|
# 前端页面url
|
|
|
|
|
origins = [
|
|
|
|
|
"http://localhost:80",
|
|
|
|
|
"http://127.0.0.1:80",
|
|
|
|
|
]
|
2024-01-26 09:57:55 +08:00
|
|
|
|
2024-01-26 10:46:13 +08:00
|
|
|
# 后台api允许跨域
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=origins,
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|