Server: basic implementation. Not tested => no python installed; bugfix in ESP (uvicorn on port 8090)

This commit is contained in:
Thomas Karner committed 2026-09-20 23:49:40 +02:00
1 parent 3694cdf207
commit 8a51e3d95e
38 files changed
+2442 -21

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
# poorBird reverse proxy. Install to /etc/caddy/Caddyfile.
#
# Ports, so nothing collides:
# 127.0.0.1:8090 uvicorn (POORBIRD_API_PORT) -- never exposed
# :8080 plain HTTP for the camera (SPEC C2, firmware/API.md)
# :443 TLS for humans
#
# TLS: Caddy gets a real Let's Encrypt certificate for a LAN-only hostname via
# DNS-01, so there is no port forwarding and no browser warning. That needs a
# Caddy build with your DNS provider's plugin:
#
# caddy add-package github.com/caddy-dns/cloudflare
#
# For a first test on any Linux box you can skip the whole human-facing block
# and just use the :8080 one.
(protected_files) {
# Auth-checked image serving (SPEC 3.3).
#
# An unguessable URL is not access control. The app authorizes the
# request, then replies with an empty body and
# X-Accel-Redirect: /images/2026/09/08/<uuid>.jpg
# and Caddy serves the bytes from disk. The browser never receives a
# directly fetchable URL.
#
# The root below is the PARENT of POORBIRD_IMAGE_ROOT and
# POORBIRD_THUMB_ROOT in /etc/poorbird/poorbird.env. One value, two
# readers -- keep them in step.
@accel header X-Accel-Redirect *
handle_response @accel {
rewrite * {rp.header.X-Accel-Redirect}
file_server {
root /srv/poorbird
}
}
}
# --- humans -----------------------------------------------------------------
poorbird.example.internal {
tls {
dns cloudflare {env.CLOUDFLARE_API_TOKEN}
}
encode zstd gzip
reverse_proxy 127.0.0.1:8090 {
header_up X-Real-IP {remote_host}
import protected_files
}
}
# --- the camera --------------------------------------------------------------
# Plain HTTP on the LAN, deliberately (SPEC C2): TLS on the ESP32 means a CA
# baked into firmware, and a mounted camera that silently stops uploading when
# the chain rotates.
:8080 {
handle /api/v1/* {
request_body {
# A burst frame at UXGA can be a few hundred KB; an OTA image is
# ~1-2 MB. The default limit is too small for both.
max_size 12MB
}
reverse_proxy 127.0.0.1:8090
}
handle /healthz {
reverse_proxy 127.0.0.1:8090
}
handle {
respond 404
}
}
+144
View File
@@ -0,0 +1,144 @@
# Installing poorBird on the thin client
Written for: whoever sets up the server host — assumes comfort with systemd
and `psql`, but not with this project.
Target is an x86 Linux box on the same LAN as the camera (SPEC §0). Native
packages and systemd units, no containers. Commands below assume Debian or
Ubuntu; adjust the package manager otherwise.
---
## 1. Packages and user
```bash
sudo apt update
sudo apt install -y postgresql python3-venv python3-pip git caddy
sudo useradd --system --home-dir /opt/poorbird --shell /usr/sbin/nologin poorbird
sudo mkdir -p /opt/poorbird /etc/poorbird /srv/poorbird/{images,thumbs} /var/lib/poorbird
sudo chown -R poorbird:poorbird /srv/poorbird /var/lib/poorbird /opt/poorbird
```
## 2. Database
Loopback TCP with `scram-sha-256`, deliberately not a Unix socket — see
`TODO.md` Phase 1.2.
```bash
sudo -u postgres psql <<'SQL'
CREATE ROLE poorbird LOGIN PASSWORD 'PUT_A_REAL_PASSWORD_HERE';
CREATE DATABASE poorbird OWNER poorbird;
SQL
```
Confirm `pg_hba.conf` has a `host all all 127.0.0.1/32 scram-sha-256`
line before any `trust` line, then `sudo systemctl reload postgresql`.
Verify the path the app will actually use, not the socket:
```bash
psql "postgresql://poorbird:PUT_A_REAL_PASSWORD_HERE@127.0.0.1:5432/poorbird" -c 'SELECT 1'
```
## 3. Application
```bash
sudo -u poorbird git clone <your-remote> /opt/poorbird/src
sudo ln -sfn /opt/poorbird/src/server /opt/poorbird/app
sudo -u poorbird python3 -m venv /opt/poorbird/venv
sudo -u poorbird /opt/poorbird/venv/bin/pip install -e /opt/poorbird/app
```
## 4. Configuration
```bash
sudo cp /opt/poorbird/app/deploy/poorbird.env.example /etc/poorbird/poorbird.env
sudo chown root:poorbird /etc/poorbird/poorbird.env
sudo chmod 0640 /etc/poorbird/poorbird.env
sudo -e /etc/poorbird/poorbird.env # set the DB password and the enroll secret
```
Generate the enroll secret with `openssl rand -hex 32`.
> **Before you ever rotate `POORBIRD_ENROLL_SECRET`:** it is compiled into the
> firmware image as the last-resort recovery path for a camera that cannot be
> reached over USB once mounted. Rotating it here without also shipping new
> firmware removes that escape hatch (`firmware/API.md`).
Check the machine before starting anything:
```bash
sudo -u poorbird env $(sudo cat /etc/poorbird/poorbird.env | grep -v '^#' | xargs) \
/opt/poorbird/venv/bin/poorbird check
```
## 5. Services
```bash
sudo cp /opt/poorbird/app/deploy/poorbird-*.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now poorbird-api poorbird-worker
systemctl status poorbird-api poorbird-worker
journalctl -u poorbird-api -f
```
The API unit runs `poorbird migrate` in `ExecStartPre`. The worker unit
deliberately does not, so two units never race to apply the same revision.
Smoke test:
```bash
curl -s localhost:8090/api/v1/health | python3 -m json.tool
```
## 6. Reverse proxy
```bash
sudo cp /opt/poorbird/app/deploy/Caddyfile /etc/caddy/Caddyfile
sudo -e /etc/caddy/Caddyfile # set your hostname and DNS provider
sudo systemctl reload caddy
```
Ports, so nothing collides:
| Port | Who |
|---|---|
| `127.0.0.1:8090` | uvicorn — never exposed |
| `:8080` | plain HTTP for the camera (SPEC C2) |
| `:443` | TLS for humans |
DNS-01 needs a Caddy build with your provider's plugin:
`caddy add-package github.com/caddy-dns/cloudflare`. For a first test, replace
the human-facing site block with `http://:8081` and drop the `tls` block.
## 7. Network
Give the server a **DHCP reservation or a static IP, then hardcode that
address in the firmware** — simpler and more reliable than mDNS (SPEC §9).
The camera has no way to be told a new address once it is mounted and cannot
reach the endpoint.
---
## Upgrading
```bash
sudo -u poorbird git -C /opt/poorbird/src pull
sudo -u poorbird /opt/poorbird/venv/bin/pip install -e /opt/poorbird/app
sudo systemctl restart poorbird-api poorbird-worker
```
`ExecStartPre` applies migrations on restart.
> Restarting mid-OTA-rollout matters: a device on a trial image marks that
> image valid **only after a heartbeat the endpoint accepted**. Returning 5xx
> to a trial device rolls it back — correct behaviour, but know it before you
> restart the API during a rollout (`firmware/API.md`).
## Backups
Not built yet — TODO Phase 5, and blocked on open question 9 (whether the NAS
offers SFTP, or only SMB/NFS), which decides between restic and a mounted
path. Until then **this machine is not backed up.** Do not let real captures
accumulate here before that is done.
+44
View File
@@ -0,0 +1,44 @@
[Unit]
Description=poorBird API
After=network-online.target postgresql.service
Wants=network-online.target
Requires=postgresql.service
[Service]
Type=exec
User=poorbird
Group=poorbird
WorkingDirectory=/opt/poorbird/app
EnvironmentFile=/etc/poorbird/poorbird.env
# Migrations run as a separate idempotent command, never at app import time.
# Same command a container entrypoint or init job would call.
ExecStartPre=/opt/poorbird/venv/bin/poorbird migrate
ExecStart=/opt/poorbird/venv/bin/poorbird serve
Restart=on-failure
RestartSec=5
TimeoutStopSec=20
# Logs go to stdout and journald picks them up. No app-managed log files.
StandardOutput=journal
StandardError=journal
SyslogIdentifier=poorbird-api
# --- hardening
NoNewPrivileges=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectSystem=strict
ProtectHome=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
RestrictSUIDSGID=yes
LockPersonality=yes
MemoryDenyWriteExecute=no
ReadWritePaths=/srv/poorbird /var/lib/poorbird
[Install]
WantedBy=multi-user.target
+42
View File
@@ -0,0 +1,42 @@
[Unit]
Description=poorBird worker (job queue + scheduler)
After=network-online.target postgresql.service poorbird-api.service
Wants=network-online.target
Requires=postgresql.service
[Service]
Type=exec
User=poorbird
Group=poorbird
WorkingDirectory=/opt/poorbird/app
EnvironmentFile=/etc/poorbird/poorbird.env
# No ExecStartPre=migrate here: the API unit owns migrations, so two units
# never race to apply the same revision.
ExecStart=/opt/poorbird/venv/bin/poorbird worker
Restart=on-failure
RestartSec=5
# The worker finishes its current job on SIGTERM.
KillSignal=SIGTERM
TimeoutStopSec=60
StandardOutput=journal
StandardError=journal
SyslogIdentifier=poorbird-worker
# --- hardening
NoNewPrivileges=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectSystem=strict
ProtectHome=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes
LockPersonality=yes
ReadWritePaths=/srv/poorbird /var/lib/poorbird
[Install]
WantedBy=multi-user.target
+44
View File
@@ -0,0 +1,44 @@
# poorBird configuration.
#
# Install to /etc/poorbird/poorbird.env, owned by root:poorbird, mode 0640.
# systemd loads it via EnvironmentFile. This same file becomes a compose .env
# unchanged if containers ever happen (TODO.md Phase 1.2).
#
# Note: systemd's EnvironmentFile does NOT do shell quoting or expansion.
# Write VALUES literally, with no surrounding quotes.
# --- database ---------------------------------------------------------------
# Loopback TCP with scram-sha-256, deliberately not a Unix socket.
POORBIRD_DATABASE_URL=postgresql+psycopg://poorbird:CHANGE_ME@127.0.0.1:5432/poorbird
# --- http -------------------------------------------------------------------
# Bind to loopback only; Caddy is the only thing that talks to it.
# Caddy itself takes :8080 for the camera and :443 for humans, so uvicorn sits
# behind on 8090. See deploy/Caddyfile.
POORBIRD_API_HOST=127.0.0.1
POORBIRD_API_PORT=8090
# --- storage ----------------------------------------------------------------
# Read by the app AND referenced by the Caddyfile: the auth-checked image
# handoff needs both to agree on this path (SPEC 3.3).
POORBIRD_IMAGE_ROOT=/srv/poorbird/images
POORBIRD_THUMB_ROOT=/srv/poorbird/thumbs
POORBIRD_STATE_DIR=/var/lib/poorbird
# --- devices ----------------------------------------------------------------
# Shared bootstrap secret the firmware presents in X-Enroll-Secret.
# Generate with: openssl rand -hex 32
#
# WARNING (firmware/API.md): this value is compiled into the firmware image as
# the last-resort recovery path for a camera that cannot be reached over USB.
# Rotating it here WITHOUT shipping new firmware removes that escape hatch.
POORBIRD_ENROLL_SECRET=
# --- worker -----------------------------------------------------------------
POORBIRD_WORKER_POLL_INTERVAL_S=2
POORBIRD_JOB_MAX_ATTEMPTS=3
# --- logging ----------------------------------------------------------------
POORBIRD_LOG_LEVEL=INFO
# text for journalctl by eye, json once you are shipping logs somewhere.
POORBIRD_LOG_FORMAT=text