From 6a95b885b834c3204b0e1ece3fbb8d20b29b2bc4 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 30 Nov 2026 17:43:13 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Document=20the=20PocketBase=20an?= =?UTF-8?q?d=20SignalDB=20stack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The README now describes the PocketBase binary, the per-user settings, the admin UI, the sign-in page and the import-legacy command. PLAN.md records the decisions of the migration and marks the sections that describe the old backend as superseded. --- PLAN.md | 19 ++++++ README.md | 186 +++++++++++++++++++++++++++++------------------------- 2 files changed, 120 insertions(+), 85 deletions(-) diff --git a/PLAN.md b/PLAN.md index 39dd3e3..6f9021d 100644 --- a/PLAN.md +++ b/PLAN.md @@ -297,6 +297,25 @@ Switched from online-first/offline-fallback to fully offline-first. - `+layout.svelte`: removed offline banner (no longer meaningful; behaviour is identical online or offline). - `online.ts`: deleted (unused). +### M12 — PocketBase + SignalDB ✅ + +The backend is now an embedded PocketBase (v0.40.6), which replaces `chi`, the hand-rolled SQLite store, the token auth and the sync log. On the client, SignalDB replaces Dexie and the outbox; collections persist in IndexedDB (one database per user) and sync with the server. + +Decisions: + +- Typed PocketBase collections instead of opaque payload blobs (the server computes closed days and weeks). +- Open sign-up with owner scoped rules. +- Clients are the only writers of entries, settings and balance adjustments. +- Day and week closing stay server routes. +- All deletes are soft. +- `settings.timezone` is the source of truth for day keys instead of the `TZ` environment variable. +- 15 character PocketBase ids. +- `entries.end_time` 0 means running on the server and `null` in the client. +- Stale running entries are stopped at the end of their day by the client and by a server cron. +- `wotra import-legacy` imports the old SQLite database (`--email` of an existing account, optional `--tz`, `--dry-run`; replaces all data of that user). + +The Stack, Project Layout, Database Schema, API Surface, Auth, Sync Protocol and Offline Strategy sections above describe the pre-M12 backend and are superseded where they conflict. + ### M11 — Future CSV/JSON export, monthly summary view. diff --git a/README.md b/README.md index acea105..061dddc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Wotra — Working Time Tracker -A self-hosted working time tracker with a Go backend and a Svelte PWA frontend. Offline-capable, single-binary deploy. +A self-hosted working time tracker with a single Go binary that embeds PocketBase and serves a Svelte PWA frontend. Offline-first, single-binary deploy. ## Features @@ -8,141 +8,157 @@ A self-hosted working time tracker with a Go backend and a Svelte PWA frontend. - **Close days**: merges all entries (worked_ms = sum of durations, breaks excluded). - **Mark days** as holiday, vacation, or sick (auto-credits expected daily hours). - **Close weeks**: computes overtime/undertime against a frozen snapshot of your expected hours. -- **Settings history**: change hours/week with an `effective_from` date — past closed weeks are unaffected. -- **PWA + offline**: works without a network connection; syncs via IndexedDB outbox when back online. +- **Settings history**: change hours/week, workdays and timezone with an `effective_from` date; past closed weeks are unaffected. +- **Accounts**: email + password sign-up on the login page, optional OAuth2 providers. +- **PocketBase admin UI** at `/_/`: manage users, collections and OAuth2. +- **Offline-first PWA**: SignalDB collections persist in IndexedDB (one database per user) and sync with the server, so the app keeps working without a network connection. - **CSV export**: entries, days, weeks. -- **Midnight guard**: running entries are automatically stopped at 23:59:59 if they cross midnight. +- **Midnight guard**: stale running entries are automatically stopped at the end of their day (by the client and a server cron). ## Quick Start Tool versions are declared in `mise.toml`. Install [mise](https://mise.jdx.dev) and run `mise install` once to get the correct Go and Node versions. +**Note**: Dependencies are automatically installed when needed. If you want to manually trigger dependency installation, run `mise deps`. + ### Development ```bash -# Start both servers (Go API + Vite dev server) +# Start PocketBase API and Vite dev server mise run dev -# Go API: http://localhost:8080 +# PocketBase API: http://localhost:8080 (admin UI: /_/) # Vite UI: http://localhost:5173 (with /api proxy) ``` -**Note**: Dependencies are automatically installed when needed. If you want to manually trigger dependency installation, run `mise deps`. - -`AUTH_TOKEN` defaults to `devtoken` in `mise.toml`. Override it by setting the variable in your environment before running. - -Open `http://localhost:5173`, go to **Settings**, enter your token. +Open `http://localhost:5173` for the UI and `http://localhost:8080` for the API. The PocketBase admin UI is at `http://localhost:8080/_/`. Create an account on the sign-in page. ### Production (single binary) -```bash -mise run build # builds web/ then embeds it in the Go binary +The frontend build must run before `go build`, because the binary embeds `web/build/`. -AUTH_TOKEN=mysecrettoken ./wotra +```bash +mise run build # builds web/ (npm run build) then the Go binary + +./wotra serve --http=0.0.0.0:8080 --dir=./pb_data # open http://localhost:8080 ``` +On first run, create the first superuser via the admin UI at `http://localhost:8080/_/`, or with: + +```bash +./wotra superuser create +``` + +### Container + +```bash +podman build -t wotra . +podman run -p 8080:8080 -v wotra-data:/pb_data wotra +``` + ### All tasks -| Task | Description | -|---------------|--------------------------------------------------| -| `mise run build` | Build production binary (web + Go) | -| `mise run build:web` | Build Svelte frontend only | -| `mise run build:go` | Build Go binary only | -| `mise run dev` | Start API + UI dev servers concurrently | -| `mise run dev:api` | Start Go API server only | -| `mise run dev:ui` | Start Vite dev server only | -| `mise run test` | Run all Go tests | -| `mise deps` | Install/update project dependencies | -| `mise run clean` | Remove build artifacts | +| Task | Description | +|----------------------|------------------------------------------------------| +| `mise run build` | Build production binary (web + Go, sequential) | +| `mise run build:web` | Build Svelte frontend only | +| `mise run build:go` | Build Go binary only | +| `mise run dev` | Start PocketBase API + Vite dev servers concurrently | +| `mise run dev:api` | Start PocketBase dev server only | +| `mise run dev:ui` | Start Vite dev server only | +| `mise run test` | Run all Go tests | +| `mise run test:web` | Run frontend Vitest unit tests | +| `mise run test:all` | Run Go tests and frontend tests | +| `mise run check` | Lint and typecheck (go vet, gofmt, svelte-check) | +| `mise deps` | Install/update project dependencies | +| `mise run clean` | Remove build artifacts (recreates `web/build/.gitkeep`) | -## Configuration (environment variables) +## PocketBase Admin UI and OAuth2 -| Variable | Default | Description | -|-------------|-------------|----------------------------------------------| -| `AUTH_TOKEN` | **required** | Bearer token for API access | -| `PORT` | `8080` | HTTP port | -| `DB_PATH` | `wotra.db` | SQLite database file path | -| `TZ` | `UTC` | Timezone for day/week key calculation | +The PocketBase admin UI lives at `/_/` on the server host. There you manage collections and users. Accounts live in the `users` collection (email + password); sign-up happens on the login page. OAuth2 providers are optional and configured in the admin UI (users collection, Options, OAuth2), with the redirect URL `https:///api/oauth2-redirect`. + +Hours per week, workdays and timezone are per-user settings inside the app; the timezone setting is what day keys are computed with. New users get one default settings row (40 h, Monday to Friday, UTC, effective 2000-01-01). ## API Reference -All API endpoints require `Authorization: Bearer ` except `/healthz`. +All routes require an authenticated session except `/healthz` and `/api/health` (PocketBase health endpoint). -### Entries - -| Method | Path | Description | -|--------|-----------------------------|--------------------------| -| POST | `/api/entries/start` | Start tracking | -| POST | `/api/entries/{id}/stop` | Stop a specific entry | -| GET | `/api/entries?from=&to=` | List entries by date | -| PUT | `/api/entries/{id}` | Edit start/end/note | -| DELETE | `/api/entries/{id}` | Soft-delete an entry | +Entries, settings and balance adjustments are written by clients through the sync push (`POST /api/sync/push`), not through REST endpoints. ### Days -| Method | Path | Description | -|--------|-------------------------------|--------------------------------------| -| GET | `/api/days?from=&to=` | List closed days | -| POST | `/api/days/{day}/close` | Close a day (merge entries) | -| POST | `/api/days/{day}/mark` | Mark as holiday/vacation/sick | -| DELETE | `/api/days/{day}/close` | Reopen a closed day | +| Method | Path | Description | +|--------|---------------------------|-----------------------------------| +| POST | `/api/days/{day}/close` | Close a day (merge entries) | +| POST | `/api/days/{day}/mark` | Mark as holiday/vacation/sick | +| DELETE | `/api/days/{day}/close` | Reopen a closed day | ### Weeks -| Method | Path | Description | -|--------|-------------------------------|--------------------------------------| -| GET | `/api/weeks?from=&to=` | List closed weeks | -| POST | `/api/weeks/{week}/close` | Close a week (compute overtime) | -| DELETE | `/api/weeks/{week}/close` | Reopen a closed week | - -Week key format: `YYYY-Www` (e.g. `2024-W03`). - -### Settings - -| Method | Path | Description | -|--------|-------------------------|------------------------------------| -| GET | `/api/settings` | Current effective settings | -| PUT | `/api/settings` | Add a new settings version | -| GET | `/api/settings/history` | All settings history | +| Method | Path | Description | +|--------|----------------------------|-----------------------------------| +| POST | `/api/weeks/{week}/close` | Close a week (compute overtime) | +| DELETE | `/api/weeks/{week}/close` | Reopen a closed week | ### Export -| Method | Path | Description | -|--------|------------------------------|--------------------------| -| GET | `/api/export/entries.csv` | Export entries as CSV | -| GET | `/api/export/days.csv` | Export days as CSV | -| GET | `/api/export/weeks.csv` | Export weeks as CSV | +| Method | Path | Description | +|--------|-----------------------------|--------------------------| +| GET | `/api/export/entries.csv` | Export entries as CSV | +| GET | `/api/export/days.csv` | Export days as CSV | +| GET | `/api/export/weeks.csv` | Export weeks as CSV | ### Sync -| Method | Path | Description | -|--------|-------------------|-------------------------------------| -| POST | `/api/sync/pull` | Pull changes since a version | -| POST | `/api/sync/push` | Push local changes (advisory) | +| Method | Path | Description | +|--------|-----------------------|----------------------------------------------------------| +| POST | `/api/sync/push` | Batch push of client changes (entries, settings, balance adjustments) | + +Pulls use the PocketBase `updated` cursor; pushes are batched. Entries, settings and balance adjustments are written exclusively through this endpoint. ### Health -| Method | Path | Description | -|--------|-------------|--------------------------| -| GET | `/healthz` | Unauthenticated health | +| Method | Path | Description | +|--------|----------------|----------------------------------------| +| GET | `/healthz` | Unauthenticated wotra health endpoint | +| GET | `/api/health` | PocketBase health endpoint | + +## Migrating from the legacy version + +If you ran a pre-PocketBase version of wotra, import your old SQLite database: + +1. Back up the legacy database: `cp wotra.db wotra.db.bak`. +2. Build and start the new version, then create your account in the app (sign-up on the login page). The account must already exist before importing. +3. Stop the server. +4. Run the import with the same `--dir` as the server: + +```bash +./wotra import-legacy --db wotra.db --email [--tz ] [--dry-run] +``` + +The import replaces all data of that user. `--tz` overrides the timezone stored in the imported settings rows; day keys are imported unchanged. Use `--dry-run` to preview first. ## Architecture ``` -┌─────────────────────────┐ ┌──────────────────────────┐ -│ Svelte PWA (client) │ ◄─────► │ Go service (API) │ -│ - IndexedDB (Dexie) │ HTTPS │ - REST/JSON endpoints │ -│ - Service Worker │ JSON │ - Business logic │ -│ - Sync outbox │ │ - SQLite (modernc) │ -└─────────────────────────┘ └──────────────────────────┘ +┌────────────────────────────┐ ┌───────────────────────────────────┐ +│ Svelte PWA (client) │ │ Single Go binary (server) │ +│ - SignalDB collections │ HTTPS │ - Embedded PocketBase │ +│ persisted in IndexedDB │ JSON + │ - Records API │ +│ - Service Worker │ SSE │ - Auth (users + OAuth2) │ +│ - One database per user │ ◄───────► │ - Realtime (SSE) │ +│ │ │ - Close day/week business logic │ +│ │ │ - SQLite storage │ +└────────────────────────────┘ └───────────────────────────────────┘ ``` +Collections: `entries`, `closed_days`, `closed_weeks`, `settings_history`, `balance_adjustments` (all owner scoped). + See [PLAN.md](PLAN.md) for the full design document. ## Development Notes -- **Go module**: `github.com/wotra/wotra` -- **SQLite driver**: `modernc.org/sqlite` (pure Go, no CGO) -- **Day boundary**: entries must not cross midnight; the server auto-stops any running entry at 23:59:59 local time. -- **Settings history**: `effective_from` lets you change hours/week without rewriting past closed weeks. +- **Day boundary**: entries must not cross midnight (in the user's timezone setting); stale running entries are stopped at the end of their day by the client and by a server cron. +- **Settings history**: `effective_from` lets you change hours/week, workdays and timezone without rewriting past closed weeks. - **Overtime**: stored as a signed `delta_ms` on each `closed_weeks` row. Frozen at close time. +- **Sync model**: SignalDB collections sync with the server; ids are client-generated 15 character PocketBase ids, conflicts resolve last-write-wins by `client_updated_at`, deletes are soft. Pulls walk the PocketBase `updated` cursor; pushes go through `POST /api/sync/push`.