Poll sync cheaply and make the server deployable

Polling re-downloaded the whole store, photos included, every five
seconds. The app now checks a tiny version endpoint and only fetches the
payload when it actually moved, and the server gained request logging,
graceful shutdown, and self-hosting docs so it can run on a VPS instead
of only on localhost.

💘 Generated with Crush

Assisted-by: Crush:kimi-k3
This commit is contained in:
2026-10-08 10:19:55 +08:00
parent 88780f7701
commit f3e6fc8fa6
6 changed files with 189 additions and 10 deletions
+7
View File
@@ -42,6 +42,9 @@ clicking an item copies it back to the clipboard.
code and server URL in `UserDefaults` (`sync.code`, `sync.serverURL`,
`sync.version`), pushes the whole store as JSON, and polls every 5s.
- `Server/main.go` — the Go sync backend (single file, `net/http` + `lib/pq`).
Also ships `Server/README.md` (self-hosting) and `Server/prompt-sync.service`
(systemd unit). Logs one line per request, creates its schema on startup, and
shuts down gracefully on SIGTERM so a redeploy cannot cut off a push.
## Sync protocol
@@ -53,6 +56,7 @@ payload as opaque `JSONB`; it never parses note contents.
| --- | --- |
| `POST /api/codes` | Generates a unique code and inserts an empty group |
| `GET /api/sync/{code}` | `{"version":N,"updatedAt":…,"data":{…}\|null}`, 404 if unknown |
| `GET /api/sync/{code}/version` | `{"version":N}` — the cheap poll |
| `PUT /api/sync/{code}` | Upserts the payload, returns the incremented version |
| `GET /healthz` | Liveness |
@@ -60,6 +64,9 @@ Client rules:
- Payload = `{sections, activeSectionID, images}`, where `images` maps a
filename to base64 PNG so photos travel with the notes.
- Polling is two-step: `GET /version` every 5s (tiny) and the full payload only
when the version moved. Fetching the payload unconditionally would re-download
every base64 photo every 5 seconds.
- Version counter is server-owned; the client stores the last version it saw
in `sync.version` and only applies a remote payload when `remote.version >
lastVersion`.