File size: 2,966 Bytes
1f70d93
 
 
 
 
 
 
 
 
 
 
 
 
860ad93
1f70d93
 
 
 
 
 
 
 
 
 
 
860ad93
 
 
 
 
 
 
 
 
 
 
 
 
 
1f70d93
 
 
 
 
 
860ad93
 
1f70d93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
987546a
1f70d93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
860ad93
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Todo API - Main application entry point

FastAPI application providing RESTful todo management with JWT authentication.
Features:
- Create, read, update, delete todos
- JWT-based authentication with user sessions
- Google OAuth integration
- Health check endpoint for monitoring
- Auto-generated interactive API docs at /docs
"""

import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from routes import todos, auth

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application lifespan event handler"""
    # Startup
    logger.info("Todo API starting up with JWT authentication...")
    logger.info("Server running on http://localhost:8000")
    logger.info("Interactive API docs available at http://localhost:8000/docs")
    logger.info("API endpoints available at /api")
    yield
    # Shutdown
    logger.info("Todo API shutting down...")


# Create FastAPI application instance
app = FastAPI(
    title="Todo API",
    description="RESTful API for managing todo items with JWT authentication",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc",
    lifespan=lifespan
)


# Configure CORS middleware to allow frontend access
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000",  # Next.js development server
        "http://127.0.0.1:3000",  # Alternative localhost format
        "http://localhost:3001",  # Alternative port for Next.js
        "http://127.0.0.1:3001",  # Alternative localhost with port 3001
        "http://localhost:3002",  # Additional port for Next.js
        "http://127.0.0.1:3002",  # Alternative localhost with port 3002
        "http://0.0.0.0:3000",   # Docker/WSL compatibility
        "http://0.0.0.0:3001",   # Docker/WSL compatibility
        "http://0.0.0.0:3002",   # Docker/WSL compatibility
        "https://phase2-todo-app-v2.vercel.app", # Vercel production deployment
    ],
    allow_credentials=True,
    allow_methods=["*"],  # Allow all HTTP methods (GET, POST, PATCH, DELETE, etc.)
    allow_headers=["*"],  # Allow all headers
)

# Include auth routes
app.include_router(auth.router)

# Include todo routes (protected)
app.include_router(todos.router)

@app.get("/health", tags=["health"], summary="Health check")
async def health_check():
    """
    Health check endpoint for monitoring

    Returns:
        dict: Server health status

    This endpoint is used by monitoring systems and deployment tools
    to verify the API server is running and responsive.
    """
    return {"status": "healthy"}


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )