File size: 1,634 Bytes
507973e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Main FastAPI application for the Phase II Full-Stack Todo App."""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .api.auth import router as auth_router
from .api.todos import router as todos_router
from .core.config import settings
from .core.database import create_db_and_tables


# Create FastAPI app instance
app = FastAPI(
    title=settings.app_name,
    description="REST API for the Full-Stack Todo Application with authentication",
    version=settings.version,
    debug=settings.debug,
)


# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=[settings.frontend_url, "http://localhost:3000", "http://localhost:3001"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    # Expose authorization header to frontend
    expose_headers=["Access-Control-Allow-Origin", "Authorization"]
)


# Include API routers
app.include_router(auth_router)
app.include_router(todos_router)


@app.on_event("startup")
async def on_startup():
    """Create database tables on startup."""
    create_db_and_tables()


@app.get("/")
async def root():
    """Root endpoint for health check."""
    return {"message": "Welcome to Phase II Full-Stack Todo App API"}


@app.get("/health")
async def health_check():
    """Health check endpoint."""
    from .core.database import db_health_check
    db_ok = await db_health_check()
    return {
        "status": "healthy",
        "database": "ok" if db_ok else "unavailable"
    }


# For running with uvicorn
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)