Configure CORS origins and backend port
This commit is contained in:
@@ -18,7 +18,8 @@ class Settings(BaseSettings):
|
||||
mqtt_port: int = 1883
|
||||
mqtt_topic: str = "plasttrack/machines/+/events"
|
||||
mqtt_client_id: str = "plast-track-backend"
|
||||
frontend_origin: str = "http://localhost:5173"
|
||||
frontend_origin: str | None = None
|
||||
frontend_origins: str = "http://localhost:5173,http://127.0.0.1:5173"
|
||||
downtime_check_interval_sec: int = 5
|
||||
database_startup_retries: int = 30
|
||||
database_startup_retry_delay_sec: float = 2.0
|
||||
@@ -36,6 +37,13 @@ class Settings(BaseSettings):
|
||||
database=self.postgres_db,
|
||||
).render_as_string(hide_password=False)
|
||||
|
||||
@property
|
||||
def cors_allowed_origins(self) -> list[str]:
|
||||
configured_origins = [origin.strip() for origin in self.frontend_origins.split(",") if origin.strip()]
|
||||
if self.frontend_origin:
|
||||
configured_origins.append(self.frontend_origin.strip())
|
||||
return list(dict.fromkeys(configured_origins))
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
|
||||
@@ -148,7 +148,7 @@ async def lifespan(app: FastAPI):
|
||||
app = FastAPI(title=settings.app_name, lifespan=lifespan)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=[settings.frontend_origin, "http://localhost:5173", "http://127.0.0.1:5173"],
|
||||
allow_origins=settings.cors_allowed_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
|
||||
Reference in New Issue
Block a user