diff --git a/src/baby_monitor/main.py b/src/baby_monitor/main.py index af35465..fe08c91 100644 --- a/src/baby_monitor/main.py +++ b/src/baby_monitor/main.py @@ -1,7 +1,18 @@ """Baby Monitor Main Application""" import os -from fastapi import FastAPI +from pathlib import Path +from typing import Annotated +from contextlib import asynccontextmanager +from collections.abc import AsyncIterator +from fastapi import FastAPI, Depends +from fastapi.responses import FileResponse +from fastapi.staticfiles import StaticFiles + +from baby_monitor.routers.auth import router as auth_router +from baby_monitor.routers.auth import verify_token +from baby_monitor.routers.health import router as health_router +from baby_monitor.repositories.dependencies.get_database import init_db # Disable docs in production ENVIRONMENT = os.getenv("ENVIRONMENT", "production") @@ -9,37 +20,40 @@ docs_url = "/docs" if ENVIRONMENT == "development" else None redoc_url = "/redoc" if ENVIRONMENT == "development" else None openapi_url = "/openapi.json" if ENVIRONMENT == "development" else None + +@asynccontextmanager +async def lifespan(app_instance: FastAPI) -> AsyncIterator[None]: + """Lifespan event handler for startup and shutdown.""" + # Startup: Initialize database tables + init_db() + yield + # Shutdown: Add cleanup code here if needed + +# Instantiate app app = FastAPI( docs_url=docs_url, redoc_url=redoc_url, openapi_url=openapi_url, + lifespan=lifespan, ) -@app.get("/") -def read_root() -> dict: - """Root endpoint returning a welcome message.""" - return {"message": "Hello World"} +# Mount static files +static_path = Path(__file__).parent / "static" +app.mount("/static", StaticFiles(directory=str(static_path)), name="static") + +# Include routers +app.include_router(auth_router) +app.include_router(health_router) -@app.get("/health") -def health_check() -> dict[str, str]: - """ - Health check endpoint for container orchestration. - - Returns 200 if the service is alive and able to handle requests. - """ - return {"status": "healthy"} +@app.get("/", include_in_schema=False) +def serve_home() -> FileResponse: + """Serve the home page (handles auth check client-side).""" + return FileResponse(static_path / "index.html") -@app.get("/ready") -def readiness_check() -> dict[str, str]: - """ - Readiness check endpoint for container orchestration. - - Returns 200 if the service is ready to accept traffic. - This can include checks for database connectivity, external services, etc. - """ - # Add dependency checks here if needed (database, cache, etc.) - # For now, simple readiness check - return {"status": "ready"} +@app.get("/api/") +def read_root(token: Annotated[str, Depends(verify_token)]) -> dict: + """API root endpoint (requires authentication).""" + return {"message": "Hello World", "authenticated": True}