Adventure with your friends
rove ARCHITECTURE.md
26 kB · 391 lines
Markdown
at main

Rove Architecture #

Stack decisions and rational #

  • Go backend
    • Familiarity + 80/20 perf:cost to implement
    • SQLite-backed AppView
      • cost/simplicity at low scale
      • easy to migrate later
    • Modular monolith design
      • Ease of deploy
      • Can break apart later into separate api/backend/ingestor services
    • BFF auth model — Rove backend is the OAuth client for all surfaces; PDS tokens never leave the server
  • React Native (Expo) for iOS + Android
    • Write once, ship to both stores
  • Separate web codebase (Vite + React + TanStack Router)
    • Shared TS-only packages cover ~25–35% of frontend code (API client, domain logic, data-fetching hooks)
    • UI components, styling, navigation, and maps diverge by design — letting each surface be best-in-class for its medium
    • SEO / link-preview HTML served by the Go backend for public URLs (activity pages, profiles); the SPA hydrates on top

Data flow #

PDS holds canonical records. Jetstream is the JSON firehose. The Go backend is the AppView (queryable index) and the OAuth client for both surfaces — all PDS interactions flow through it. The four core flows:

1. Login (BFF — backend is the OAuth client) #

POST /auth/login {handle}authorize URL (state + PKCE challenge)redirect to /authorizeredirect → BE /auth/callback?code=...GET /auth/callback?code=...POST /token(private_key_jwt + PKCE verifier)access + refresh tokens (DPoP-bound)session cookie (web) /session token (mobile)resolve handle → DID → PDSdiscover PDS auth serveruser authenticates +grants scopes to rove.socialencrypt tokens, store in sessions.dbcreate user sessionRN / Web clientRove backendUser's PDSRN / Web clientRove backendUser's PDS

Tokens never leave the backend. The client only ever sees a Rove session.

2. User submits a post #

POST /activities(GPX, photos, metadata)com.atproto.repo.uploadBlob(GPX, photos, static map)blob CIDscom.atproto.repo.createRecord(canonical social.rove.activity)record URI + CIDactivity URIvalidate sessioncompute metricstrim privacy zonesrender static mapemits firehose event→ ingest worker indexes into appview.dbRN clientRove backendUser's PDSRN clientRove backendUser's PDS

No proxy header, no service-auth lxm dance — the backend writes to the PDS directly using the user's stored OAuth tokens. Likes/follows/comments take the same shape, just smaller payloads.

3. Post lands in the AppView index #

commit event (firehose)filtered event (collection: social.rove.*)INSERT activity row + advance cursorvalidate record schemaextract indexable fieldsUser's PDSJetstreamRove backend (ingest worker)SQLiteUser's PDSJetstreamRove backend (ingest worker)SQLite

This runs continuously as a separate goroutine in the Go monolith. Lag from PDS commit to indexed row is ~1s.

4. User views their feed #

GET social.rove.feed.getActivityFeed(Rove session cookie / token)SELECT activities for viewer's followsORDER BY indexedAt DESCrowsfeed page (records + cursor)validate sessionresolve viewer DIDhydrate & renderRN / Web clientRove backendSQLiteRN / Web clientRove backendSQLite

Feed reads never touch the PDS — entirely served from appview.db.

Reads #

All feed/profile/activity queries hit the Go backend's SQLite index. The backend updates SQLite by consuming Jetstream filtered to social.rove.*. Lag is ~1s.

Auth flow #

Rove backend is the OAuth client for both surfaces (BFF — Backend for Frontend). Mobile and web clients authenticate to the Rove backend; the backend authenticates to the user's PDS. PDS access tokens never live on user devices.

Why BFF for both surfaces #

The OAuth spec explicitly endorses BFF for mobile + web combinations:

"It is acceptable for a web service to act as a public client, and conversely it is possible for mobile apps and browser apps to coordinate with a token-mediating backend service and for the combination to form a confidential client. Mobile apps and browser apps can also adopt a 'backend-for-frontend' (BFF) architecture with a web service backend acting as the OAuth client." — atproto.com/specs/oauth

Tangled (an ATProto-native git platform) runs this pattern on web today via a Caddy-based identity-aware proxy issuing signed session cookies.

The backend-write requirement decides it for Rove:

Activity records need server-side computation before they're written — metric derivation, privacy-zone trimming, static map generation. The realistic options are (a) backend is the OAuth client and writes directly (BFF), (b) the atproto-proxy header + service-auth dance (constrained by lxm-binding), or (c) a client-writes-back hop after the backend prepares the record. (a) is cleanest; (b) and (c) are operationally clunkier.

Concrete benefits of BFF on both surfaces:

  • Spec-supported confidential session lifetimes. Confidential clients get 180-day refresh tokens and unlimited overall sessions; public clients are capped at 2 weeks for both. Our users won't get bumped to login every fortnight.
  • Single OAuth grant per user. One consent screen, one client metadata document, one set of scopes.
  • Same auth code path for mobile and web. Solo-dev win.
  • DPoP keys on the backend. Spec only requires "tokens must not be shared or reused across client devices" — per-session backend keys satisfy that.
  • Tokens never leave the backend. Fewer rotation/revocation edge cases.

The cost: every PDS interaction flows through the backend. At Rove's scale that's fine — the AppView already serves all hot reads from local SQLite.

This is an auth-architecture choice, not a data-architecture one. Records still live on the user's PDS; lexicons are still public; the data is still ATProto-native and portable. Only the credential-handling pattern changes — the user can revoke our OAuth grant from their PDS at any time.

OAuth client setup #

Item Value / location
client_id https://rove.social/oauth/client-metadata.json (the URL itself is the ID)
token_endpoint_auth_method private_key_jwt
Signing key (private, ES256) Fly secret → env var on backend
JWKS (public) https://rove.social/.well-known/jwks.json
DPoP keys Generated per user, stored alongside their tokens
Scopes Granular per-NSID — see below

private_key_jwt is how the backend proves its identity to the PDS token endpoint without a shared client_secret: it signs a short-lived JWT assertion with its private key on every token request, and the PDS verifies the signature via the public JWKS.

Scopes & permission set #

ATProto OAuth uses granular per-NSID scopes. Per the permission spec, partial wildcards are not supported (no repo:social.rove.*), so each collection has to be enumerated. Rove's scope string:

atproto
include:social.rove.authBasicFeatures

Where social.rove.authBasicFeatures is a permission-set Lexicon we publish under our own NSID. Resolved permissions:

  • repo:social.rove.activity?action=create&action=update&action=delete — manage activity records
  • repo:social.rove.activity.like?action=create&action=delete — manage likes
  • blob:application/gpx+xml, blob:image/jpeg, blob:image/png — upload activity media

We deliberately do not request:

  • repo:app.bsky.graph.follow — we only read the user's bsky follow graph from the firehose, never create follow records on their behalf.
  • Any rpc: scopes — those grant the user's PDS permission to forward calls to a third-party service via the atproto-proxy header. In BFF, our clients call our backend directly with a Rove session, and our backend calls the user's PDS directly with OAuth tokens. No proxy header in the request path → no rpc: needed.

Permission sets are themselves Lexicons (type: permission-set) with a human-readable title and detail, so the consent screen reads "Rove wants permission to: post activities, like activities, upload activity media" instead of a raw scope list. We publish the permission-set Lexicon once and reference it via include: in our client metadata's scope field.

Token storage — two SQLite files #

File Contents Backed up? Encryption
appview.db Indexed activities, follow graph cache, derived metrics, ingest cursor — reconstructible from the firehose Yes (snapshots + Litestream → Tigris) None — content is public anyway
sessions.db OAuth access + refresh tokens, DPoP keys, browser session rows Yes (snapshots + Litestream → Tigris) App-level (libsodium / age) before insert; key in Fly secret

Why two files:

  • Clean backup and concurrency boundaries — the auth DB is small and write-heavy on login/refresh; the AppView DB is large and read-heavy.
  • Easy to swap durability strategies independently later (e.g. take sessions.db out of Litestream if you ever want to).
  • Obvious separation of "encrypted-at-rest matters" from "doesn't really."

Why the auth DB is still in Litestream backups: losing all sessions on a disaster restore means every user has to re-auth — much worse than the marginal risk of an extra encrypted blob in Tigris. Layered defense: Tigris encrypts at rest, and the token rows are encrypted at the app layer with a key that lives in Fly secrets, not in either DB. An attacker who stole the bucket without the key gets nothing.

Crash recovery: SQLite WAL replays cleanly on restart. The one sharp edge is in-flight refresh-token rotation — if the server crashes after the PDS issued a new refresh token but before the row commits, the user gets bumped to login on next request. Annoying but rare and recoverable; "force re-login" is a path we want anyway for security.

Refresh-token concurrency: the OAuth spec says refresh tokens are single-use and "client implementations may need locking primitives to prevent concurrent token refresh requests." Two requests from the same user racing to refresh will conflict — one wins, one fails and gets the user logged out. We use golang.org/x/sync/singleflight keyed by user DID: the first caller performs the refresh, all concurrent callers share its result. Cleaner than a per-DID mutex — no wasted second refresh after the first wins, and all callers receive the new tokens directly.

Session model on the client #

  • Web — HTTP-only signed cookie, CSRF tokens for mutations, sliding expiry.
  • Mobile — long-lived session token issued by the Rove backend, stored in Keychain (iOS) / Keystore (Android), refreshed on use.
  • Neither client ever sees PDS tokens.

Backend API surface (XRPC) #

Lexicons under social.rove.* define our endpoints; the Go backend serves them XRPC-style at https://rove.social/xrpc/<NSID>. Examples:

Endpoint Type Description
social.rove.feed.getActivityFeed query Indexed feed, paginated by cursor
social.rove.feed.getActivity query Single activity by URI
social.rove.actor.getProfile query Profile + activity stats
social.rove.activity.create procedure Submit activity (raw GPX, photos, metadata)
social.rove.activity.delete procedure Delete activity

Lexicon-first: define each schema as a Lexicon JSON file under lexicons/, then run lexgen (from bluesky-social/indigo) to generate Go server stubs and @atproto/lex-cli to generate TypeScript client types for the shared packages/api. One source of truth, both ends type-safe.

Auth on these endpoints: Rove session (cookie or token), not OAuth bearer. XRPC's standard auth is OAuth, but in BFF the client doesn't have a PDS token — only a Rove session — so our endpoints check session validity instead. If we ever expose endpoints externally to other ATProto services, we'd add bearer/service-auth support alongside.

Future: backend-issued service auth for direct blob upload #

For the Submit-a-post flow, the client currently sends raw GPX/photos to our backend, which uploads them to the user's PDS — a double-hop on bandwidth. The optimization, when bandwidth becomes an issue: backend mints a short-lived service-auth JWT via com.atproto.server.getServiceAuth bound to lxm = com.atproto.repo.uploadBlob, hands it to the client, client uploads blobs directly to PDS, then sends the resulting blob CIDs back to the backend for record assembly. Defer until measured bandwidth pain.

Frontend split #

Two codebases (mobile + web) with shared TS-only packages:

rove/
├── packages/
│   ├── api/      # lexicon-generated types, XRPC client wrappers
│   ├── core/     # domain types, business logic, formatters, validators
│   └── hooks/    # TanStack Query hooks; no platform imports
├── apps/
│   ├── mobile/   # Expo (iOS + Android)
│   └── web/      # Vite + React + TanStack Router

Roughly 25–35% of frontend code is genuinely shareable: lexicon types & API client (~15%), business logic & formatters (~10%), data-fetching hooks (~10%). Components, styling, navigation, and maps diverge by platform — and that divergence accelerates as the map UX matures (web hover/viewport interactions vs native gesture/offline-tile patterns).

A packages/tokens for design tokens (colors, spacing, typography scale) is worth doing — both apps consume the same values and render them in their native styling system.

Tooling to explore #

Pick before starting; defaults below are what I'd reach for solo, but the trade-offs matter.

Monorepo plumbing

  • pnpm — workspaces are built-in. Fast, disk-efficient (content-addressable store), the default I'd start with.
    • Alternatives: npm workspaces (works but slower), yarn (fine if you already use it).
  • turbo — task caching across the monorepo. Worth adding once pnpm build gets slow in CI; skip until then.
    • Alternative: nx — heavier, more opinionated, multi-team-flavored. Overkill solo.

Data fetching (universal — works in RN and web)

  • @tanstack/react-query — canonical choice. Caching, retries, optimistic updates. Use this in packages/hooks.
    • Alternative: swr — simpler, less powerful. Probably too thin for our needs.

State (most state should live in TanStack Query — only reach for these for UI state)

  • zustand — minimal, no boilerplate, both platforms. My default.
    • Alternative: jotai — atomic state, slightly different mental model.

Forms

  • react-hook-form + zod — universal, lightweight, type-safe validation. Validation logic lives in packages/core and is reused on both surfaces.

Mobile-specific

  • expo — committed.
  • expo-router — file-system routing for RN.
  • nativewind — Tailwind for RN. If web uses Tailwind too, the same class strings work on both surfaces (rendering still differs).

Web-specific

SEO and link previews without SSR The Go backend serves a small, server-rendered HTML shell at public URLs (/activity/<id>, /u/<handle>) with the right og:image / og:title / og:description meta tags. The static-map blob already lives in Tigris — point og:image at it. For everything else the SPA loads as normal and hydrates the page client-side. This sidesteps the need for a JS framework with SSR/RSC.

Cross-platform UI (escape hatch if you change your mind)

  • tamagui — universal styling system that targets RN + web from one component tree. If you ever decide a few high-value components (buttons, inputs, cards) are worth sharing, this is the route — but commit to it up front; mixing in late is painful.

Lexicon principle #

social.rove.activity records are fat: GPX blob CID, photo blob CIDs, computed metrics, polyline summary, static-map blob CID — all in the record on the user's PDS. If Rove vanishes, another AppView can index the data without us.

Likes: define our own social.rove.activity.like. The app.bsky.feed.like schema technically accepts a strongRef to anything, but no AppView outside bsky cross-indexes by subject collection — there's no real "free interop" benefit, and our own lexicon keeps semantics clean.

Follows: read the user's existing app.bsky.graph.follow records to power Rove's social graph. The schema is {subject: did, createdAt} — a generic DID-to-DID edge, not tied to bsky posts. Reusing it gives users a populated social graph from day one. Defer a Rove-specific follow lexicon unless user signal demands it.

NSIDs are forever; reverse-DNS the domain and don't rename them later.

Hosting #

Component Choice Cost
Backend VM Fly Machine ~$5/mo
Block storage (SQLite) Fly Volume + daily snapshots included
Object storage (Litestream backups, map cache, static assets) Fly Tigris (S3-compatible, colocated with the machine) ~$1–3/mo
CDN Cloudflare (free tier) $0
DNS Cloudflare $0 (with the registrar fee paid elsewhere)
Map tiles MapTiler — free tier through alpha/beta; Startup plan at launch $0 → $25/mo
Domain Any registrar ~$15/yr

Durability & availability #

Two distinct concerns. Fly's docs warn about both in one breath, but they have different answers and different timelines for Rove.

Durability — solved from day one #

Belt + suspenders, both cheap on Fly:

  • Fly Volume snapshots — daily, included; coarse-grained but easy point-in-time restores via the Fly UI.
  • Litestream → Fly Tigris — continuous WAL streaming to the same-region S3-compatible bucket. ~1s RPO, runs as a sidecar process in the same machine. Tigris itself has S3-style 11-nines internal durability. Tigris is already in the stack for map cache + static assets, so the marginal cost is ~$0.

Restore order in a disaster: try Litestream first (smaller RPO), fall back to volume snapshot if Litestream state is corrupt. The AppView's primary content is also reconstructible from the firehose by replaying Jetstream from a stored cursor — so the backups exist mainly to preserve non-replayable state (OAuth refresh tokens, the cursor itself, derived caches) and to avoid a full PDS crawl if the outage exceeds Jetstream's replay window.

If the volume + machine die simultaneously, we spin up a fresh machine, litestream restore from Tigris, and we're back. Maximum data loss: ~1 second.

Availability — single-machine until launch, then LiteFS #

Fly's volumes/overview doc warns: "Always provision at least two volumes per app... Volumes don't have built-in replication." That warning is about uptime, not data — and it explicitly carves out our case: "In a few cases, you can run a single Machine with an attached volume. For example, if your app is in development... or if you're running an app that can handle downtime and has a custom backup procedure."

For Alpha/Beta, single-machine is fine. The realistic downtime budget:

Event Impact
Routine deploy ~30s–2min while the machine restarts
Host hardware failure (rare) ~10–30min while Fly relocates and we restore from Litestream
Tigris-region outage Tigris is replicated; full outage is rare, but possible

A free-to-use social adventure app pre-launch can absorb this comfortably.

At launch, add HA via LiteFS — Fly's purpose-built SQLite replication layer:

  • Primary machine takes writes; replicas get the WAL streamed via FUSE
  • Read replicas serve queries with a few-ms lag
  • Automatic failover via consensus; or manual flip
  • Layers cleanly with Litestream — LiteFS handles live replication, Litestream still owns long-term object-storage backup
  • Cost: another ~$5/mo per replica machine

LiteFS adds non-trivial operational complexity (FUSE, primary/replica routing, lease semantics, careful migration handling). The reason to defer it: solo-dev time. The roadmap's "Backend infrastructure improvements to handle real traffic" line item at Launch is where this lands.

Defer until post-launch #

  • HA via LiteFS — single-machine through alpha/beta; add a replica + LiteFS at launch
  • Running our own PDS or relay
  • Postgres / managed DB / replicas
  • Multi-region anything
  • Video (capture, upload, transcoding) — punt to post-launch entirely

Core protocol & specs #

Auth (canonical) #

Reference architectures #

Go (backend) ecosystem #

React Native (frontend) ecosystem #

  • Expo — strongly recommended for solo dev; handles native build + OTA updates
  • @atproto/api — main JS client
  • @atproto/oauth-client — OAuth core; bring your own storage/redirect handlers for RN
  • Bluesky social-app — Bluesky's own RN+RN-Web app; reference for OAuth flow, agent setup, blob upload

Maps #

Hosting & infra #