gonets/SRV/config/cors.py

31 lines
998 B
Python
Raw Permalink Normal View History

2025-06-26 17:49:21 +05:00
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=["*"],
)