"""FastAPI app: /api/duties and static webapp.""" import logging import duty_teller.config as config from fastapi import Depends, FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from sqlalchemy.orm import Session from duty_teller.api.calendar_ics import get_calendar_events from duty_teller.api.dependencies import ( fetch_duties_response, get_db_session, get_validated_dates, require_miniapp_username, ) from duty_teller.db.schemas import CalendarEvent, DutyWithUser log = logging.getLogger(__name__) app = FastAPI(title="Duty Teller API") app.add_middleware( CORSMiddleware, allow_origins=config.CORS_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/api/duties", response_model=list[DutyWithUser]) def list_duties( request: Request, dates: tuple[str, str] = Depends(get_validated_dates), _username: str = Depends(require_miniapp_username), session: Session = Depends(get_db_session), ): from_date_val, to_date_val = dates log.info( "GET /api/duties from %s", request.client.host if request.client else "?", ) return fetch_duties_response(session, from_date_val, to_date_val) @app.get("/api/calendar-events", response_model=list[CalendarEvent]) def list_calendar_events( dates: tuple[str, str] = Depends(get_validated_dates), _username: str = Depends(require_miniapp_username), ): from_date_val, to_date_val = dates url = config.EXTERNAL_CALENDAR_ICS_URL if not url: return [] events = get_calendar_events(url, from_date=from_date_val, to_date=to_date_val) return [CalendarEvent(date=e["date"], summary=e["summary"]) for e in events] webapp_path = config.PROJECT_ROOT / "webapp" if webapp_path.is_dir(): app.mount("/app", StaticFiles(directory=str(webapp_path), html=True), name="webapp")