Add opt-in CORS support via PARAKEET_CORS_ORIGINS env var - #1
Open
arvindvenkataramani wants to merge 1 commit into
Open
arvindvenkataramani wants to merge 1 commit into
arvindvenkataramani wants to merge 1 commit into
Conversation
- config.py: read PARAKEET_CORS_ORIGINS (comma-separated origins) into CORS_ORIGINS list - main.py: conditionally add CORSMiddleware when CORS_ORIGINS is set CORS is disabled by default (secure by default). To enable, set the environment variable to a comma-separated list of allowed origins: PARAKEET_CORS_ORIGINS=http://localhost:3000,https://myapp.example.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Adds configurable CORS support to the FastAPI app. CORS is disabled by default — it only activates when
PARAKEET_CORS_ORIGINSis set.Why
When running this service as part of a larger system (e.g. behind a local dashboard or UI that makes requests from the browser), CORS headers are required. Without them, browser requests are blocked.
I ran into this while integrating parakeet-mlx-fastapi into a custom home automation project. The fix that comes to mind is just
allow_origins=["*"], but that's insecure by default — so this adds proper configurability instead.How to use
Set the
PARAKEET_CORS_ORIGINSenvironment variable to a comma-separated list of allowed origins before starting the server:Or in a
.envfile:If the variable is unset or empty, no
CORSMiddlewareis added and the service behaves exactly as before.Changes
config.py— readsPARAKEET_CORS_ORIGINSintoCORS_ORIGINS: list[str], following the existingPARAKEET_WORKERSpatternmain.py— importsCORSMiddlewareand conditionally adds it increate_app()whenCORS_ORIGINSis non-emptyThis has been reviewed and tested in a real integration against a local dashboard.