fix CORSMiddleware

This commit is contained in:
jxxghp 2023-06-19 12:41:17 +08:00
parent 7248ce3105
commit 8b4a44c8d4
2 changed files with 12 additions and 0 deletions

View File

@ -11,6 +11,8 @@ class Settings(BaseSettings):
API_V1_STR: str = "/api/v1" API_V1_STR: str = "/api/v1"
# 密钥 # 密钥
SECRET_KEY: str = secrets.token_urlsafe(32) SECRET_KEY: str = secrets.token_urlsafe(32)
# 允许的域名
ALLOWED_HOSTS: list = ["*"]
# TOKEN过期时间 # TOKEN过期时间
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
# 时区 # 时区

View File

@ -1,5 +1,6 @@
import uvicorn as uvicorn import uvicorn as uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from uvicorn import Config from uvicorn import Config
from app.api.apiv1 import api_router from app.api.apiv1 import api_router
@ -17,6 +18,15 @@ from app.scheduler import Scheduler
App = FastAPI(title=settings.PROJECT_NAME, App = FastAPI(title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json") openapi_url=f"{settings.API_V1_STR}/openapi.json")
# 跨域
App.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_HOSTS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# API路由 # API路由
App.include_router(api_router, prefix=settings.API_V1_STR) App.include_router(api_router, prefix=settings.API_V1_STR)