updated available endpoints
This commit is contained in:
+38
-24
@@ -1,7 +1,18 @@
|
|||||||
"""Baby Monitor Main Application"""
|
"""Baby Monitor Main Application"""
|
||||||
|
|
||||||
import os
|
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
|
# Disable docs in production
|
||||||
ENVIRONMENT = os.getenv("ENVIRONMENT", "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
|
redoc_url = "/redoc" if ENVIRONMENT == "development" else None
|
||||||
openapi_url = "/openapi.json" 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(
|
app = FastAPI(
|
||||||
docs_url=docs_url,
|
docs_url=docs_url,
|
||||||
redoc_url=redoc_url,
|
redoc_url=redoc_url,
|
||||||
openapi_url=openapi_url,
|
openapi_url=openapi_url,
|
||||||
|
lifespan=lifespan,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
# Mount static files
|
||||||
def read_root() -> dict:
|
static_path = Path(__file__).parent / "static"
|
||||||
"""Root endpoint returning a welcome message."""
|
app.mount("/static", StaticFiles(directory=str(static_path)), name="static")
|
||||||
return {"message": "Hello World"}
|
|
||||||
|
# Include routers
|
||||||
|
app.include_router(auth_router)
|
||||||
|
app.include_router(health_router)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/", include_in_schema=False)
|
||||||
def health_check() -> dict[str, str]:
|
def serve_home() -> FileResponse:
|
||||||
"""
|
"""Serve the home page (handles auth check client-side)."""
|
||||||
Health check endpoint for container orchestration.
|
return FileResponse(static_path / "index.html")
|
||||||
|
|
||||||
Returns 200 if the service is alive and able to handle requests.
|
|
||||||
"""
|
|
||||||
return {"status": "healthy"}
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/ready")
|
@app.get("/api/")
|
||||||
def readiness_check() -> dict[str, str]:
|
def read_root(token: Annotated[str, Depends(verify_token)]) -> dict:
|
||||||
"""
|
"""API root endpoint (requires authentication)."""
|
||||||
Readiness check endpoint for container orchestration.
|
return {"message": "Hello World", "authenticated": True}
|
||||||
|
|
||||||
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"}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user