# poorBird server Phase 1 skeleton: configuration, schema, health, job queue and scheduler. There are **no device endpoints yet** — `/enroll`, `/ingest`, `/heartbeat`, `/status` and `/config` are Phase 2. The contract they must satisfy is [`../firmware/API.md`](../firmware/API.md). Stack per SPEC §6, deployed natively per [`../TODO.md`](../TODO.md) Phase 1.2: FastAPI + PostgreSQL 17 + SQLAlchemy 2.0 + Alembic, run as systemd units behind Caddy. No containers — but nothing here blocks them either. --- ## Try it on any Linux machine Needs Python ≥ 3.11 and a PostgreSQL you can reach. Production targets 3.13; the floor is lower so this runs on a stock Debian 12 or Ubuntu 24.04 box. ```bash sudo apt install -y python3-venv postgresql sudo -u postgres psql <<'SQL' CREATE ROLE poorbird LOGIN PASSWORD 'poorbird'; CREATE DATABASE poorbird OWNER poorbird; SQL cd server python3 -m venv .venv && . .venv/bin/activate pip install -e '.[dev]' cat > .env <<'ENV' POORBIRD_DATABASE_URL=postgresql+psycopg://poorbird:poorbird@127.0.0.1:5432/poorbird POORBIRD_IMAGE_ROOT=/tmp/poorbird/images POORBIRD_THUMB_ROOT=/tmp/poorbird/thumbs POORBIRD_STATE_DIR=/tmp/poorbird/state POORBIRD_API_PORT=8090 ENV mkdir -p /tmp/poorbird/{images,thumbs,state} poorbird check # config, paths, database — before starting anything poorbird migrate # alembic upgrade head poorbird serve # http://127.0.0.1:8090/api/v1/health ``` In a second shell: ```bash cd server && . .venv/bin/activate poorbird worker & poorbird enqueue noop '{"hello": "world"}' # the worker logs it within ~2s ``` `poorbird check` is the fast answer to "is this machine set up correctly" — it validates config, storage paths and database connectivity and prints what it found, without starting a service. ### Tests ```bash pytest # everything reachable pytest -m 'not db' # no PostgreSQL needed ``` Queue and schema tests need a real PostgreSQL — `FOR UPDATE SKIP LOCKED` has no equivalent elsewhere, and mocking it would test nothing. They skip automatically when the database is unreachable, so a green run with no Postgres proves less than it looks. Check for `skipped` in the summary. --- ## Layout ``` src/poorbird/ config.py every value from the environment, nothing baked in logging_setup.py stdout only; journald or a log driver catches it db.py engine + session factory (sync, deliberately) models/ SPEC §3 schema + the SPEC §0 additions api/ FastAPI app; Phase 2 mounts device routes here worker/ queue (SKIP LOCKED), handlers, scheduler cli.py serve | worker | migrate | check | enqueue migrations/ Alembic deploy/ systemd units, Caddyfile, env template, INSTALL.md tests/ ``` --- ## Decisions worth knowing before you read the code **Sync SQLAlchemy, not async.** At one camera and a few thousand frames a day the async machinery buys nothing and costs clarity, and the worker's claim loop is far easier to reason about synchronously. FastAPI runs `def` handlers in a threadpool, so this does not block the event loop. The one thing that may argue otherwise later is the MJPEG relay in Phase 6. **Device config is one JSONB column, not forty.** `GET /config` returns ~40 optional keys and the firmware merges and clamps partial documents, so a column per key would mean a migration every time the firmware gains a tunable. Validation belongs in a Pydantic model, not in the DDL. The trade: you cannot index or constrain an individual setting in SQL. If one ever needs querying across devices, promote just that key to a column. **Migration 0001 builds from `Base.metadata`.** A bounded exception, and `tests/test_schema_matches_models.py` migrates a scratch database and asserts an empty autogenerate diff, so the models and the migration cannot silently diverge. Every later revision is a normal autogenerated one. If you prefer explicit DDL in the baseline, autogenerate it against an empty database and replace the file — the drift test passes either way. **The scheduler only enqueues, never executes.** Every unit of work goes through the queue, where it is visible, retried and auditable, rather than running inside a timer callback where a failure disappears. --- ## Two things the code already guards, and the UI must too - `battery_valid = false` means **not measured** — no divider is wired by default. Render it as "—", never as a low-battery alarm. - The daily counters live in the device's RTC memory and reset on power loss. `captures` is authoritative. The counters' value is the *gap* between captured and delivered, which is an upload-health signal. --- ## Deploying for real See [`deploy/INSTALL.md`](deploy/INSTALL.md).