Housekeeping, not new behaviour. The status table still said the WebUI was
"nothing beyond the Vite scaffold" and the server was "Phases 1 and 2" at 194
tests, all of which stopped being true three commits ago. A status table that
is wrong is worse than no status table: it is the first thing read and the last
thing checked.
Endpoint lists in server/README.md were taken from the app's own OpenAPI
schema rather than from memory, which is how the last round of this found that
/ingest was documented as multipart when it is a raw body. The operator half is
now listed separately from the device half, because the two authenticate in
ways that must not be mixed up -- bearer token and no cookies on one side,
session cookie plus CSRF on the other.
Ticked auth-checked image serving, which shipped in 735cbcc, and recorded why
there are two delivery paths and only one authorization path.
The test count is 308 because the suite was run, not because 308 was the number
I remembered.
Two comments in api/app.py contradicted the code beside them. One said the
enroll-secret warning was "not fatal at Phase 1 -- no device endpoint exists
yet", six lines above the device endpoints being mounted; it now says the real
reason the API starts anyway, which is that an enrolled camera keeps working
and refusing to start would take the gallery down too. The other said the
Phase 3 UI endpoints were still to come, directly below the two lines that
mount them.
Left alone deliberately: the "12/12 tests" in docs/reviews/**. Those are
point-in-time review snapshots, and updating them to current numbers would
destroy the only thing they are for.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from .. import __version__
|
|
from ..config import get_settings
|
|
from ..logging_setup import configure_logging
|
|
from .routes import (
|
|
auth,
|
|
captures,
|
|
commands,
|
|
conditions,
|
|
device_config,
|
|
enroll,
|
|
firmware,
|
|
health,
|
|
ingest,
|
|
)
|
|
from .userauth import csrf_middleware
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
API_PREFIX = "/api/v1"
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
settings = get_settings()
|
|
log.info(
|
|
"poorbird api starting",
|
|
extra={"version": __version__, "image_root": str(settings.image_root)},
|
|
)
|
|
if not settings.enroll_secret_is_set:
|
|
# A warning rather than a refusal to start: an existing camera keeps
|
|
# working with the token it already holds, and taking the whole API
|
|
# down would also take down the gallery. Only enrollment breaks.
|
|
log.warning("POORBIRD_ENROLL_SECRET is not set; device enrollment will refuse requests")
|
|
yield
|
|
log.info("poorbird api stopping")
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
settings = get_settings()
|
|
configure_logging(settings.log_level, settings.log_format)
|
|
|
|
app = FastAPI(
|
|
title="poorBird",
|
|
version=__version__,
|
|
summary="Ingest, gallery and device management for a radar-triggered bird camera",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# CSRF for cookie-authenticated mutations. Middleware rather than a
|
|
# per-route dependency so a new endpoint cannot forget it; it applies only
|
|
# to unsafe methods on requests that carry the session cookie, which is why
|
|
# the device endpoints -- bearer tokens, no cookies -- are unaffected. See
|
|
# userauth.csrf_middleware.
|
|
app.middleware("http")(csrf_middleware)
|
|
|
|
app.include_router(health.router, prefix=API_PREFIX, tags=["health"])
|
|
app.include_router(health.root_router)
|
|
|
|
# Phase 3 WebUI endpoints.
|
|
app.include_router(auth.router, prefix=API_PREFIX)
|
|
app.include_router(captures.router, prefix=API_PREFIX)
|
|
|
|
# Phase 2 device endpoints. See firmware/API.md for the contract.
|
|
app.include_router(enroll.router, prefix=API_PREFIX)
|
|
app.include_router(conditions.router, prefix=API_PREFIX)
|
|
app.include_router(device_config.router, prefix=API_PREFIX)
|
|
app.include_router(commands.router, prefix=API_PREFIX)
|
|
app.include_router(ingest.router, prefix=API_PREFIX)
|
|
app.include_router(firmware.router, prefix=API_PREFIX)
|
|
|
|
# The device contract (firmware/API.md) is complete apart from /stream,
|
|
# which is Phase 6. Still to come on the operator side: the admin-facing
|
|
# POST /firmware and device management, both Phase 4.
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|