Spaces:
Sleeping
Sleeping
File size: 1,909 Bytes
b7bf9e4 88293eb b7bf9e4 | 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 | """
FastAPI application for the sorted todo API.
Provides REST endpoints for task management with PostgreSQL persistence.
"""
from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
from src.api.auth import TokenPayload, get_current_user
from src.api.config import get_settings
from src.api.database import get_session, lifespan
from src.api.routers.chat import router as chat_router
from src.api.routers.tasks import router as tasks_router
app = FastAPI(
title="Sorted API",
description="Todo application API with PostgreSQL persistence",
version="0.1.0",
lifespan=lifespan,
)
settings = get_settings()
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origin_list,
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
allow_headers=["Authorization", "Content-Type"],
)
app.include_router(tasks_router)
app.include_router(chat_router)
@app.get("/")
async def root() -> dict[str, str]:
"""Root endpoint for HF Spaces health check."""
return {"status": "ok", "service": "sorted-api"}
@app.get("/health")
async def health(session: AsyncSession = Depends(get_session)) -> dict[str, str]:
"""
Health check endpoint.
Verifies database connectivity by executing a simple query.
Returns:
{"status": "ok"} if database is reachable
"""
await session.execute(text("SELECT 1"))
return {"status": "ok"}
@app.get("/auth/me")
async def auth_me(user: TokenPayload = Depends(get_current_user)) -> dict:
"""
Protected endpoint that returns the authenticated user's identity.
Requires a valid JWT Bearer token in the Authorization header.
Returns:
User id and email from the verified JWT.
"""
return {"id": user.sub, "email": user.email}
|