GrowWithTalha commited on
Commit
cdcc65e
·
1 Parent(s): 69be42f

fix: fronted_url

Browse files
Files changed (3) hide show
  1. README.md +2 -2
  2. core/config.py +1 -1
  3. main.py +6 -2
README.md CHANGED
@@ -34,8 +34,8 @@ FastAPI backend for the Todo List application with JWT authentication and Postgr
34
  |----------|-------------|---------|
35
  | `DATABASE_URL` | PostgreSQL connection string | `postgresql://user:password@ep-xxx.aws.neon.tech/neondb?sslmode=require` |
36
  | `JWT_SECRET` | Secret key for JWT tokens | Generate a random string: `openssl rand -hex 32` |
37
- | `FRONTEND_URL` | Your frontend URL for CORS | `https://your-frontend.vercel.app` |
38
- | `ENVIRONMENT` | Environment name | `production` |
39
 
40
  **Note:** For Neon database, make sure to append `?sslmode=require` to your DATABASE_URL.
41
 
 
34
  |----------|-------------|---------|
35
  | `DATABASE_URL` | PostgreSQL connection string | `postgresql://user:password@ep-xxx.aws.neon.tech/neondb?sslmode=require` |
36
  | `JWT_SECRET` | Secret key for JWT tokens | Generate a random string: `openssl rand -hex 32` |
37
+ | `FRONTEND_URL` | Your frontend URL for CORS (optional, defaults to `*`) | `https://your-frontend.vercel.app` |
38
+ | `ENVIRONMENT` | Environment name (optional) | `production` |
39
 
40
  **Note:** For Neon database, make sure to append `?sslmode=require` to your DATABASE_URL.
41
 
core/config.py CHANGED
@@ -20,7 +20,7 @@ class Settings(BaseSettings):
20
  jwt_expiration_days: int = 7
21
 
22
  # CORS
23
- frontend_url: str
24
 
25
  # Environment
26
  environment: str = "development"
 
20
  jwt_expiration_days: int = 7
21
 
22
  # CORS
23
+ frontend_url: str = "*" # Default to allow all origins for HuggingFace Spaces
24
 
25
  # Environment
26
  environment: str = "development"
main.py CHANGED
@@ -50,10 +50,14 @@ app = FastAPI(
50
  )
51
 
52
  # Add CORS middleware
 
 
 
 
53
  app.add_middleware(
54
  CORSMiddleware,
55
- allow_origins=[settings.frontend_url],
56
- allow_credentials=True,
57
  allow_methods=["*"],
58
  allow_headers=["*"],
59
  )
 
50
  )
51
 
52
  # Add CORS middleware
53
+ # If frontend_url is "*", allow all origins (useful for HuggingFace Spaces)
54
+ allow_all_origins = settings.frontend_url == "*"
55
+ allow_origins = ["*"] if allow_all_origins else [settings.frontend_url]
56
+
57
  app.add_middleware(
58
  CORSMiddleware,
59
+ allow_origins=allow_origins,
60
+ allow_credentials=not allow_all_origins, # Can't use credentials with wildcard
61
  allow_methods=["*"],
62
  allow_headers=["*"],
63
  )