# Docwell — Architecture and Build Plan Companion to [FEATURES.md](FEATURES.md), which defines the feature priorities (MVP / v1 / v2) used below. ## 1. Product statement Docwell is an open-source, self-hostable knowledge base and collaborative document editor. It is wiki-first like Docmost and Outline, and grows into Notion-style databases later, like AppFlowy and AFFiNE. It ships as three standalone services plus native desktop and mobile clients. ## 2. System architecture ``` ┌──────────────────────┐ ┌───────────────────┐ ┌───────────────────┐ Clients │ Web App (browser) │ │ Desktop (Wails) │ │ Mobile (Flutter) │ └──────────┬───────────┘ └─────────┬─────────┘ └─────────┬─────────┘ │ HTTPS: REST + WebSocket (all clients use the same API) ─────────────────────────┼────────────────────────────────────────────────────────── Standalone ┌──────────▼───────────┐ ┌────────────────────────────────────────┐ services │ docwell-web (Go) │ │ docwell-server (Go) │ │ • serves Vue SPA │──▶│ • REST API /api/v1 │ │ • runtime config │ │ • WebSocket collab /collab (Yjs relay) │ │ • optional /api proxy│ │ • auth, permissions, search index │ └──────────────────────┘ │ • jobs: import/export, email, webhooks │ │ • Admin panel (Vue SPA) /admin │ └──────────────────┬─────────────────────┘ │ private network only ┌──────────────────▼─────────────────────┐ │ docwell-db (PocketBase) │ │ • SQLite + collections + migrations │ │ • file storage (local or S3) │ │ • realtime SSE (server subscribes) │ └────────────────────────────────────────┘ ``` ### 2.1 Responsibilities | Service | Owns | Does NOT do | |---|---|---| | **docwell-db** (PocketBase, unmodified official binary + `pb_migrations/` + optional `pb_hooks/`) | Persistence, schema migrations, file blobs, backups, low-level realtime events | Business rules. Its collection API rules allow **only the server's superuser token**, so it is never exposed publicly. | | **docwell-server** (Go) | Single public API, authentication and sessions, authorization (workspace/space/page ACLs), Yjs collaboration relay and persistence, search index, background jobs, email, webhooks, API tokens. Hosts the **admin panel** (embedded Vue build). | Rendering the end-user UI | | **docwell-web** (Go) | Serves the end-user Vue SPA (embedded via `embed.FS`), injects runtime config (`/config.json` with API URL, branding, enabled auth providers), sets security headers and caching, can optionally reverse-proxy `/api` + `/collab` for single-origin deployments | Business logic. It is a thin delivery service, so the SPA can also be hosted on any CDN. | **Why the server is the only gateway to PocketBase:** permission rules in a wiki depend on space membership, group membership and page-level overrides. Those rules are awkward to express as PocketBase API-rule strings, and splitting auth logic across two places is a security risk. Keeping PocketBase private also lets us swap it for Postgres later behind a Go `Store` interface. ### 2.2 Key technical decisions (ADR summaries) | # | Decision | Rationale / alternatives | |---|---|---| | ADR-1 | **Editor = Tiptap 2/3 (ProseMirror) for web and desktop**, with `@tiptap/vue-3` | This is the stack Docmost uses and close to what Outline uses. It has a large extension ecosystem (tables, math, mentions, collaboration) and is MIT licensed. | | ADR-2 | **CRDT = Yjs** (`y-prosemirror`, `y-indexeddb`, Awareness for cursors) | Industry standard, and both Docmost and Outline use it. | | ADR-3 | **The Go server relays the y-websocket / Hocuspocus-compatible protocol** and stores incremental updates. Compaction (merging updates into a snapshot, and extracting plain text and JSON for search and exports) runs **Yrs compiled to WASM inside Go via `wazero`**, so there is no cgo and no Node sidecar. | There is no mature pure-Go Yjs port. Options considered: (a) a Node Hocuspocus sidecar, which breaks the "Go only" goal; (b) cgo + yffi, which complicates cross-compiling; (c) client-side compaction, which you can't trust. **(d) wazero + yrs.wasm** was chosen — **spike passed** ([ADR-3](adr/0003-yrs-wasm-wazero.md)): output matches y-prosemirror, steady-state compaction takes milliseconds, WebAssembly costs ≤2× native. Compact incrementally (snapshot + new updates); never `merge_updates` on long logs, which is quadratic. | | ADR-4 | **Mobile editor = the same Tiptap editor bundle running inside a Flutter WebView** (`webview_flutter` or `flutter_inappwebview`), bridged over JS channels. Navigation, lists, search, settings and comments are native Flutter. | Writing a second rich-text engine in Dart with identical schema and Yjs semantics is months of work and a constant source of divergence. AppFlowy built its own Flutter editor, but its data model is native to that editor. Revisit a native Dart editor in v2+. | | ADR-5 | **Search = Bleve** (pure Go, embedded in the server) with an optional **Meilisearch** adapter | PocketBase does not expose SQLite FTS5 through its API. Bleve keeps the "no extra service" promise. The search code sits behind an interface so you can swap the backend. | | ADR-6 | **Authentication = PocketBase's built-in `users` auth collection** — **decided**. PocketBase stores the credentials and issues the tokens, and also handles password, OTP email codes, MFA, OAuth2, verification and password-reset emails (via its own SMTP settings). PocketBase stays private: clients call `/api/v1/auth/*` on docwell-server, which forwards to PocketBase. The server checks bearer tokens through a small hook route (`pb_hooks/docwell.pb.js`, cached 30 s) and adds Docwell's rules: signup policy and domain allow-list, `status = disabled` blocks sign-in (PocketBase `authRule`), and the first account becomes the superadmin (see ADR-12). **Authorization** (workspace, space and page permissions) stays in docwell-server. | Less security-critical code to write and maintain, and auth features (MFA, OTP, OAuth2) come out of the box. Trade-offs: tokens are PocketBase JWTs, so sign-out is client-side (to revoke every session, rotate the user's `tokenKey`), and swapping PocketBase for Postgres later would need a token migration plan. | | ADR-7 | **Frontend monorepo with shared packages** (npm workspaces): `@docwell/editor`, `@docwell/ui`, `@docwell/api-client` (generated from OpenAPI), `@docwell/i18n` | Web app, admin panel, desktop app and mobile WebView all reuse the same editor and UI kit. | | ADR-8 | **API contract = OpenAPI 3.1 written first** → Go server stubs (`oapi-codegen`) + TS client (`openapi-typescript`) + Dart client (`openapi-generator`) | Keeps four clients in sync. | | ADR-9 | **Background jobs = in-process worker pool** with a PocketBase `jobs` collection as a durable queue (no Redis) | Fits a single-node deployment. For multi-node later, add NATS or Redis behind an interface. | | ADR-10 | **License = AGPL-3.0** for the whole repository (services, apps and packages) — **decided** | Same license as Docmost and AppFlowy. Anyone who runs a modified Docwell as a hosted service must publish their changes. | | ADR-11 | **Desktop = Wails v3** — **decided** | v3 adds multi-window support, a system tray, a better bindings generator and a cleaner app/service API than v2. Pin an exact v3 version and upgrade on purpose. | | ADR-12 | **Organizations above workspaces, with cascading rights (same model as DriverVault)** — **decided**. Each user belongs to at most one organization (`users.organization`) and has an instance role (`users.role`): `superadmin` manages everything, `admin` manages only their own organization, `user` is a plain member. Workspaces belong to an organization. Rights cascade: a superadmin owns every workspace, an org admin owns every workspace of their organization, and anyone else gets their explicit workspace role (owner > admin > member > guest), and only while they belong to that organization. Nobody grants a role above their own, acts on someone ranked above them, or changes their own role. A user without an organization can create one and becomes its admin; an admin can delete their organization only as its last member (with no workspaces left) and is demoted to user. All rules live in `Server/internal/access` as pure, tested functions. | Same mental model as DriverVault, so both products behave alike. One organization per user keeps permission checks simple (one role, one tenant); multi-organization membership can come later through a join table without changing the rule functions. | ### 2.3 Scaling note (PocketBase = SQLite) SQLite in WAL mode can handle a few thousand active users on a single node, and that covers the self-host market. The limits to know about: one writer at a time, and a single node, because the collab relay keeps doc rooms in memory. Mitigations: batch Yjs update writes (debounce 1–2 s per doc), store large blobs as files, keep the `Store` interface DB-agnostic, and plan a "Postgres store" plus a "Redis/NATS pub-sub for collab rooms" for horizontal scale in v2+. ## 3. Repository layout (monorepo) Scaffolded in Phase 0. Folders marked *(planned)* do not exist yet. ``` Docwell/ ├── api/openapi.yaml # API contract (source of truth for all clients) ├── Database/ # docwell-db: official PocketBase + pb_migrations/ + pb_hooks/ ├── Server/ # docwell-server (Go module docwell/server) │ ├── cmd/docwell-server/ │ ├── internal/ │ │ ├── httpapi/ # router (net/http ServeMux), CORS, logging, handlers │ │ ├── config/ │ │ ├── store/pocketbase # superuser REST client │ │ ├── admin/ # embeds the admin SPA build (dist/) │ │ └── auth, acl, collab, ycrdt, search, jobs, importer, exporter, mail (planned) │ └── admin/ # admin panel: Vue 3 + Vite + Tailwind (@docwell/admin) ├── Web-App/ # docwell-web (Go module docwell/web) │ ├── cmd/docwell-web/ │ ├── internal/spa/ # embeds the web app build, /config.json, optional proxy │ └── frontend/ # end-user app: Vue 3 + Vite + Tailwind (@docwell/web-app) ├── Desktop-App/ # Wails v3 (Go module docwell/desktop) │ └── frontend/ # thin host that mounts @docwell/web-app with a desktop platform ├── Phone-App/ # Flutter (android + ios) ├── Packages/ │ ├── editor/ # @docwell/editor – Tiptap schema + Vue component │ ├── ui/ # @docwell/ui – Tailwind theme tokens + components │ ├── api-client/ # @docwell/api-client – TS client, types generated from api/ │ └── editor-bridge/, i18n/ (planned) ├── Docker/ # docker-compose: db + server + web as separate services ├── Docker-AIO/ # single all-in-one image (PocketBase + server + web) ├── Design/ # design assets ├── docs/ ├── go.work # Go workspace: Server, Web-App, Desktop-App └── package.json # npm workspaces ``` ## 4. Tech stack details | Layer | Choice | |---|---| | Go | Go 1.26, stdlib `net/http` router (method + wildcard patterns), `nhooyr.io/websocket` (now `coder/websocket`), `slog`, `koanf` config, `oapi-codegen`, `wazero`, `bleve/v2`, `go-oidc`, `pquerna/otp`, `testify` + `testcontainers-go` (spins up PocketBase) | | PocketBase | Official binary pinned to a version, used standalone, with JS migrations. The server talks to it through a thin typed REST client and authenticates with a superuser token | | Vue (web, admin, desktop) | Vue 3.5 + `