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