Spaces:
Sleeping
Sleeping
Update src/main.py
Browse files- src/main.py +28 -10
src/main.py
CHANGED
|
@@ -1,43 +1,61 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from .database import engine
|
| 4 |
from .models import user, task # Import models to register them with SQLModel
|
| 5 |
-
|
|
|
|
| 6 |
def create_app():
|
| 7 |
app = FastAPI(
|
| 8 |
title="Task Management API",
|
| 9 |
description="API for managing tasks with user authentication",
|
| 10 |
version="1.0.0"
|
| 11 |
)
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Add CORS middleware
|
| 14 |
# Note: When allow_credentials=True, you cannot use wildcard '*' for origins
|
| 15 |
# Must specify exact origins
|
| 16 |
app.add_middleware(
|
| 17 |
CORSMiddleware,
|
| 18 |
-
allow_origins=
|
| 19 |
allow_credentials=True,
|
| 20 |
allow_methods=["*"],
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
-
|
| 24 |
# Import and include routers
|
| 25 |
from .routers import auth, tasks
|
| 26 |
app.include_router(auth.router, prefix="/api", tags=["authentication"])
|
| 27 |
app.include_router(tasks.router, prefix="/api", tags=["tasks"])
|
| 28 |
-
|
| 29 |
@app.get("/")
|
| 30 |
def read_root():
|
| 31 |
return {"message": "Task Management API"}
|
| 32 |
-
|
| 33 |
@app.get("/health")
|
| 34 |
def health_check():
|
| 35 |
return {"status": "healthy"}
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return app
|
| 38 |
-
|
| 39 |
app = create_app()
|
| 40 |
-
|
| 41 |
# Create database tables on startup (for development)
|
| 42 |
@app.on_event("startup")
|
| 43 |
def on_startup():
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from .database import engine
|
| 4 |
from .models import user, task # Import models to register them with SQLModel
|
| 5 |
+
from .config import settings
|
| 6 |
+
import os
|
| 7 |
def create_app():
|
| 8 |
app = FastAPI(
|
| 9 |
title="Task Management API",
|
| 10 |
description="API for managing tasks with user authentication",
|
| 11 |
version="1.0.0"
|
| 12 |
)
|
| 13 |
+
# Get allowed origins from environment variable or use defaults
|
| 14 |
+
# Support multiple origins separated by commas
|
| 15 |
+
allowed_origins_str = os.getenv("ALLOWED_ORIGINS", "http://localhost:3000", "https://full-stack-todo-application-gwz8.vercel.app")
|
| 16 |
+
allowed_origins = [origin.strip() for origin in allowed_origins_str.split(",")]
|
| 17 |
+
|
| 18 |
+
# Also add common Vercel patterns if not already included
|
| 19 |
+
vercel_url = os.getenv("VERCEL_URL")
|
| 20 |
+
if vercel_url and vercel_url not in allowed_origins:
|
| 21 |
+
# Add both http and https versions
|
| 22 |
+
if not vercel_url.startswith("http"):
|
| 23 |
+
allowed_origins.append(f"https://{vercel_url}")
|
| 24 |
+
else:
|
| 25 |
+
allowed_origins.append(vercel_url)
|
| 26 |
+
|
| 27 |
# Add CORS middleware
|
| 28 |
# Note: When allow_credentials=True, you cannot use wildcard '*' for origins
|
| 29 |
# Must specify exact origins
|
| 30 |
app.add_middleware(
|
| 31 |
CORSMiddleware,
|
| 32 |
+
allow_origins=allowed_origins,
|
| 33 |
allow_credentials=True,
|
| 34 |
allow_methods=["*"],
|
| 35 |
allow_headers=["*"],
|
| 36 |
)
|
|
|
|
| 37 |
# Import and include routers
|
| 38 |
from .routers import auth, tasks
|
| 39 |
app.include_router(auth.router, prefix="/api", tags=["authentication"])
|
| 40 |
app.include_router(tasks.router, prefix="/api", tags=["tasks"])
|
|
|
|
| 41 |
@app.get("/")
|
| 42 |
def read_root():
|
| 43 |
return {"message": "Task Management API"}
|
|
|
|
| 44 |
@app.get("/health")
|
| 45 |
def health_check():
|
| 46 |
return {"status": "healthy"}
|
| 47 |
+
|
| 48 |
+
@app.get("/debug/cookies")
|
| 49 |
+
def debug_cookies(request: Request):
|
| 50 |
+
"""Debug endpoint to check if cookies are being received"""
|
| 51 |
+
return {
|
| 52 |
+
"cookies": dict(request.cookies),
|
| 53 |
+
"headers": dict(request.headers),
|
| 54 |
+
"origin": request.headers.get("origin"),
|
| 55 |
+
"referer": request.headers.get("referer"),
|
| 56 |
+
}
|
| 57 |
return app
|
|
|
|
| 58 |
app = create_app()
|
|
|
|
| 59 |
# Create database tables on startup (for development)
|
| 60 |
@app.on_event("startup")
|
| 61 |
def on_startup():
|