Mobile app (Android):
- Sign-in to any server, with authenticator codes. The server address and
token are kept in secure storage; a dropped connection never signs out.
- Workspaces, recent pages, spaces and a foldable page tree; search; the
inbox with mark-read.
- Pages open read-only in the shared editor from their saved content and
join the live document to edit (with the native toolbar). Page links in
the text open in the app (new "navigate" bridge message).
- Comments: threads, replies, resolve.
- "Save to Docwell" in the Android share sheet makes a page from shared
text, with web addresses as links.
- Offline reading: every answer the app reads is kept on the phone, and
used when the server can't be reached.
- Push through UnifiedPush (ntfy, NextPush, ...). The app uses the Android
connector directly; the umbrella package pulls in a native crypto build
for Linux that breaks `dart run` and tests.
Server:
- /me/devices registers a phone's UnifiedPush endpoint and Web Push keys
(new push_devices collection). Each notification is also pushed,
encrypted with RFC 8291 aes128gcm so the distributor can't read it; the
app drops messages that don't decrypt. Endpoints answering 404/410 are
forgotten. Endpoints follow the webhook address rules.
- /collab accepts the apps' own origins ("null" for the mobile editor page,
wails:// for the desktop app). Sign-in is a token in the first message,
not a cookie, so this opens no cross-site hijacking.
Tried on an Android 15 emulator: sign-in, browsing, reading, live editing,
sharing and offline reading. Not covered: push end to end (no distributor
on the emulator), iOS, and YouTube embeds, which refuse to play from the
app's local editor page (error 153).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
4.7 KiB
Go
158 lines
4.7 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// Package httpapi wires the public HTTP surface of docwell-server.
|
|
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"slices"
|
|
"time"
|
|
|
|
"docwell/server/internal/auth"
|
|
)
|
|
|
|
// Pinger reports whether a dependency is reachable.
|
|
type Pinger interface {
|
|
Health(ctx context.Context) error
|
|
}
|
|
|
|
type Deps struct {
|
|
Version string
|
|
CORSOrigins []string
|
|
DB Pinger
|
|
Auth AuthService
|
|
Directory DirectoryService
|
|
Settings SettingsService
|
|
// FileTokens signs the ?token= of file links; nil allows only bearer tokens.
|
|
FileTokens *auth.FileTokens
|
|
// Instance serves the admin dashboard, health, jobs and backups.
|
|
Instance InstanceService
|
|
// MaxUpload caps one attachment in bytes.
|
|
MaxUpload int64
|
|
// Collab serves /collab; nil disables live editing.
|
|
Collab CollabHub
|
|
// Push registers phones for push notifications; nil turns them off.
|
|
Push PushService
|
|
// Previews reads pages for link cards; nil turns link previews off.
|
|
Previews LinkPreviewer
|
|
// Audit records changes and serves the audit log; nil turns both off.
|
|
Audit AuditService
|
|
Admin http.Handler
|
|
Log *slog.Logger
|
|
}
|
|
|
|
// routeMux is a ServeMux that remembers its patterns, so a test can compare
|
|
// them with api/openapi.yaml.
|
|
type routeMux struct {
|
|
*http.ServeMux
|
|
patterns []string
|
|
}
|
|
|
|
func (m *routeMux) Handle(pattern string, h http.Handler) {
|
|
m.patterns = append(m.patterns, pattern)
|
|
m.ServeMux.Handle(pattern, h)
|
|
}
|
|
|
|
func (m *routeMux) HandleFunc(pattern string, h func(http.ResponseWriter, *http.Request)) {
|
|
m.Handle(pattern, http.HandlerFunc(h))
|
|
}
|
|
|
|
func NewRouter(d Deps) http.Handler {
|
|
return logRequests(d.Log, cors(d.CORSOrigins, newMux(d)))
|
|
}
|
|
|
|
func newMux(d Deps) *routeMux {
|
|
mux := &routeMux{ServeMux: http.NewServeMux()}
|
|
a := &api{auth: d.Auth, dir: d.Directory, settings: d.Settings, files: d.FileTokens, instance: d.Instance, collab: d.Collab, log: d.Log}
|
|
if d.Audit != nil {
|
|
a.audit = d.Audit
|
|
}
|
|
if d.Previews != nil {
|
|
a.previews = d.Previews
|
|
}
|
|
if d.Push != nil {
|
|
a.push = d.Push
|
|
}
|
|
a.routes(mux)
|
|
a.fileRoutes(mux, max(d.MaxUpload, 1<<20))
|
|
a.collabRoute(mux, d.CORSOrigins)
|
|
|
|
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
|
})
|
|
mux.HandleFunc("GET /readyz", func(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
|
|
defer cancel()
|
|
if err := d.DB.Health(ctx); err != nil {
|
|
d.Log.Warn("readiness check failed", "err", err)
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"status": "unavailable", "db": err.Error()})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok", "db": "ok"})
|
|
})
|
|
mux.HandleFunc("GET /api/v1/version", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusOK, map[string]string{"name": "docwell-server", "version": d.Version})
|
|
})
|
|
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not_found"})
|
|
})
|
|
|
|
if d.Admin != nil {
|
|
mux.Handle("GET /admin/", d.Admin)
|
|
mux.Handle("GET /admin", http.RedirectHandler("/admin/", http.StatusMovedPermanently))
|
|
}
|
|
|
|
return mux
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
|
|
func cors(origins []string, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
origin := r.Header.Get("Origin")
|
|
if origin != "" && slices.Contains(origins, origin) {
|
|
h := w.Header()
|
|
h.Set("Access-Control-Allow-Origin", origin)
|
|
h.Set("Access-Control-Allow-Credentials", "true")
|
|
h.Add("Vary", "Origin")
|
|
if r.Method == http.MethodOptions {
|
|
h.Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
|
h.Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
|
h.Set("Access-Control-Max-Age", "600")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
type statusRecorder struct {
|
|
http.ResponseWriter
|
|
status int
|
|
}
|
|
|
|
// Unwrap lets the WebSocket upgrade reach the connection's Hijacker.
|
|
func (s *statusRecorder) Unwrap() http.ResponseWriter { return s.ResponseWriter }
|
|
|
|
func (s *statusRecorder) WriteHeader(code int) {
|
|
s.status = code
|
|
s.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
func logRequests(log *slog.Logger, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
|
|
next.ServeHTTP(rec, r)
|
|
log.Info("http", "method", r.Method, "path", r.URL.Path, "status", rec.status, "dur", time.Since(start))
|
|
})
|
|
}
|