Fedir Zadniprovskyi 2024-08-10
update cors pr
@34ceec62b0e441830dbdc330c7f21b8170d83066
faster_whisper_server/config.py
--- faster_whisper_server/config.py
+++ faster_whisper_server/config.py
@@ -170,6 +170,13 @@
     log_level: str = "info"
     host: str = Field(alias="UVICORN_HOST", default="0.0.0.0")
     port: int = Field(alias="UVICORN_PORT", default=8000)
+    allow_origins: list[str] | None = None
+    """
+    https://docs.pydantic.dev/latest/concepts/pydantic_settings/#parsing-environment-variable-values
+    Usage:
+        `export ALLOW_ORIGINS='["http://localhost:3000", "http://localhost:3001"]'`
+        `export ALLOW_ORIGINS='["*"]'`
+    """
 
     enable_ui: bool = True
     """
faster_whisper_server/main.py
--- faster_whisper_server/main.py
+++ faster_whisper_server/main.py
@@ -17,6 +17,7 @@
     WebSocket,
     WebSocketDisconnect,
 )
+from fastapi.middleware.cors import CORSMiddleware
 from fastapi.responses import StreamingResponse
 from fastapi.websockets import WebSocketState
 from faster_whisper import WhisperModel
@@ -77,16 +78,14 @@
 
 app = FastAPI()
 
-cors_origins = os.environ.get("CORS_ORIGINS", "").split(",")
-if cors_origins:
+if config.allow_origins is not None:
     app.add_middleware(
         CORSMiddleware,
-        allow_origins=cors_origins,
+        allow_origins=config.allow_origins,
         allow_credentials=True,
         allow_methods=["*"],
         allow_headers=["*"],
     )
-
 
 
 @app.get("/health")
Add a comment
List