File size: 1,976 Bytes
6a3de9e
697c967
 
 
6a3de9e
 
697c967
6a3de9e
 
697c967
 
 
 
 
 
 
 
 
6a3de9e
 
 
697c967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a3de9e
697c967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.cors import CORSMiddleware
from api.v1.routes import tasks, auth, events
from ai.endpoints import chat
from database.session import async_engine
from middleware.auth_middleware import AuthMiddleware
from models import task, user, conversation, message  # Import models to register them with SQLModel
from utils.exception_handlers import (
    http_exception_handler,
    validation_exception_handler,
    general_exception_handler
)
import sqlmodel

app = FastAPI(title="Todo List API", version="1.0.0")

# Add Authentication middleware
app.add_middleware(AuthMiddleware)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, replace with specific origins
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Register exception handlers
app.add_exception_handler(StarletteHTTPException, http_exception_handler)
app.add_exception_handler(RequestValidationError, validation_exception_handler)
app.add_exception_handler(Exception, general_exception_handler)

@app.on_event("startup")
async def startup():
    """Create database tables on startup"""
    async with async_engine.begin() as conn:
        await conn.run_sync(sqlmodel.SQLModel.metadata.create_all)

# Include API routes
app.include_router(tasks.router, prefix="/api/{user_id}", tags=["tasks"])
app.include_router(auth.router, prefix="/api", tags=["auth"])
app.include_router(events.router, prefix="/api", tags=["events"])

@app.get("/")
def read_root():
    return {"message": "Todo List API - Phase II Backend"}

@app.get("/health")
def health_check():
    return {"status": "healthy"}

if __name__ == "__main__":
    import uvicorn

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