30 lines
1,001 B
Python
30 lines
1,001 B
Python
|
|
from fastapi import FastAPI
|
||
|
|
from starlette.middleware.cors import CORSMiddleware
|
||
|
|
|
||
|
|
|
||
|
|
class CORSMiddlewareConfig:
|
||
|
|
"""
|
||
|
|
Класс для настройки CORS политики в FastAPI
|
||
|
|
"""
|
||
|
|
def __init__(self, app: FastAPI) -> None:
|
||
|
|
""" Инициализируем приложение FastAPI и список допустимых CORS-оригинов """
|
||
|
|
self.app = app
|
||
|
|
self.origins = [
|
||
|
|
"http://localhost.tiangolo.com",
|
||
|
|
"https://localhost.tiangolo.com",
|
||
|
|
"http://localhost",
|
||
|
|
"http://localhost:8080",
|
||
|
|
"http://localhost:5174",
|
||
|
|
"http://localhost:5173",
|
||
|
|
]
|
||
|
|
|
||
|
|
def add_cors_middleware(self) -> None:
|
||
|
|
""" Добавляем CORS в приложение FastAPI """
|
||
|
|
self.app.add_middleware(
|
||
|
|
CORSMiddleware,
|
||
|
|
allow_origins=self.origins,
|
||
|
|
allow_credentials=True,
|
||
|
|
allow_methods=["*"],
|
||
|
|
allow_headers=["*"],
|
||
|
|
)
|