oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// Package mapa is the Oficina suite's team knowledge base: linked notes,
2// daily notes, fast search. The design is
3// docs/superpowers/specs/2026-11-12-mapa-design.md.
4package mapa
5
6import (
7 "context"
8 "log/slog"
9 "net/http"
10
11 "github.com/go-chi/chi/v5"
12
13 "amadan.net/oficina/estilo.git/chrome"
14 "amadan.net/oficina/estilo.git/sky"
15 "amadan.net/rastrillo/rastrillo"
16 "amadan.net/rastrillo/rastrillo/auth"
17 "amadan.net/rastrillo/rastrillo/csrf"
18 "amadan.net/rastrillo/rastrillo/db"
19 "amadan.net/rastrillo/rastrillo/mail"
20 "amadan.net/rastrillo/rastrillo/migrate"
21
22 "amadan.net/oficina/mapa/internal/mapa/locale"
23)
24
25// app holds what every handler needs.
26type app struct {
27 database *db.DB
28 store *Store
29 index *Index
30 config Config
31 auth *auth.Auth
32 logger *slog.Logger
33 routes chi.Router
34 // locales is rastrillo's locale set, the team's first (locales.go).
35 locales *rastrillo.Locales
36 // firsts is the new scratch pages' first-commit tokens (D12).
37 firsts firsts
38 // notion is how the Notion import reaches Notion, images where it
39 // hands Notion's images, and running its runs (import_*.go).
40 notion NotionAccess
41 images ImageStore
42 running imports
43 // testIdentity and allowUnauthenticated let a test act as a person
44 // without a magic-link round trip. Only AppForTest sets them.
45 testIdentity string
46 allowUnauthenticated bool
47}
48
49// Built is what App returns: the handler, and the seams main and the
50// tests need.
51type Built struct {
52 Mux *http.ServeMux
53 Store *Store
54 Index *Index
55 app *app
56}
57
58// UseAuth attaches the sign-in flow. Without it requireSession refuses
59// every request.
60func (built Built) UseAuth(signins *auth.Auth) { built.app.auth = signins }
61
62// WriteRoutes is every state-changing route, as "METHOD pattern": gate
63// 8 exercises each one.
64func (built Built) WriteRoutes() []string {
65 var out []string
66 _ = chi.Walk(built.app.routes, func(method, route string,
67 _ http.Handler, _ ...func(http.Handler) http.Handler) error {
68 if method != http.MethodGet && method != http.MethodHead {
69 out = append(out, method+" "+route)
70 }
71 return nil
72 })
73 return out
74}
75
76// Admit is the admission predicate, for NewAuth.
77func (built Built) Admit(address string) bool {
78 return built.app.Admit(address)
79}
80
81// App applies the schema and wires the router.
82func App(database *db.DB, config Config, logger *slog.Logger) (Built,
83 error) {
84 migrated, err := migrate.Apply(context.Background(), database,
85 BootSchema)
86 if err != nil {
87 return Built{}, err
88 }
89 logger.Info("migrate", "applied", migrated.Applied,
90 "skipped", migrated.Skipped, "adopted", migrated.Adopted)
91 store := NewStore(database.G, config.Sealer)
92 store.SetLogger(logger)
93 store.UseImages(config.Images)
94 LogImageSetup(logger, config.Images)
95 // Retention runs on wake: the platform runs no timers (§5).
96 if err := store.Sweep(context.Background()); err != nil {
97 return Built{}, err
98 }
99 // No import run can be live at boot (Notion import spec §8.3).
100 if err := store.InterruptRuns(context.Background()); err != nil {
101 return Built{}, err
102 }
103 // The bucket is emptied off the boot path: an outage there delays
104 // deletion and nothing else (images §3.3).
105 go store.EmptyImageTrash(context.Background())
106 app := &app{database: database, store: store, index: NewIndex(store),
107 config: config, logger: logger, notion: liveNotion{},
108 images: storeImages{store: store}}
109 if app.locales, err = locale.Negotiation(firstOf(config.Locale,
110 locale.Default)); err != nil {
111 return Built{}, err
112 }
113 go app.index.Watch(context.Background(), watchEvery)
114 mux := http.NewServeMux()
115 mux.Handle("GET /static/", assets.Handler())
116 // The suite's sky behind the door, drawn by estilo.
117 mux.Handle("GET "+sky.Prefix, sky.Handler())
118 mux.HandleFunc("GET "+speculationPath, serveSpeculationRules)
119 app.routes = app.router()
120 mux.Handle("/", app.routes)
121 return Built{Mux: mux, Store: store, Index: app.index, app: app}, nil
122}
123
124func (app *app) router() chi.Router {
125 router := chi.NewRouter()
126 // App-wide, from day one: every state-changing route is born
127 // covered.
128 router.Use(csrf.Protect(app.config.Origin))
129 // Bodies are bounded, and multipart is an upload route's alone.
130 router.Use(bounded)
131 // Before every route: dates and numbers in the reader's locale.
132 router.Use(app.negotiate)
133 router.NotFound(notFound)
134 router.MethodNotAllowed(notFound)
135 app.doorRoutes(router)
136 // Everything else needs a session AND admission. Grouped, so a
137 // route added below is born guarded.
138 router.Group(func(guarded chi.Router) {
139 guarded.Use(app.requireSession)
140 // The person's own date format over the browser's (note 85).
141 guarded.Use(app.ownLocale)
142 app.pageRoutes(guarded)
143 app.importRoutes(guarded)
144 })
145 return router
146}
147
148// doorRoutes are outside the session guard, necessarily: they are how a
149// session is obtained.
150func (app *app) doorRoutes(router chi.Router) {
151 router.Get(signinPath, app.signin)
152 // Admission before the classifier can dial, then the continuation
153 // around Begin so a keymail redirect survives the CSP.
154 router.Post("/auth/begin", app.beginGuard(app.keymailContinuation(
155 app.authRoute((*auth.Auth).Begin))))
156 router.Get("/auth/callback", app.authRoute((*auth.Auth).Callback))
157 router.Get("/auth/verify", app.authRoute((*auth.Auth).Verify))
158 router.Post("/auth/verify", app.authRoute((*auth.Auth).Verify))
159 router.Post("/signout", app.authRoute((*auth.Auth).Signout))
160 // Light, dark or system: estilo's form and cookie, outside the
161 // session guard so an expired session cannot loop.
162 router.Post("/appearance", chrome.HandleAppearance)
163 // Help → Colophon, signed in or out (the door does not link it: Paul,
164 // 2026-11-19). Signed in, it enters the guard itself (colophon.go).
165 router.Get(colophonPath, app.colophon)
166}
167
168// AppForTest builds the app already signed in as one address. Named for
169// what it does: memoria's earlier seam was a nil-auth fall-through whose
170// safety rested on a test that did not exist.
171func AppForTest(database *db.DB, config Config, logger *slog.Logger,
172 as string) (Built, error) {
173 built, err := App(database, config, logger)
174 if err != nil {
175 return Built{}, err
176 }
177 built.app.testIdentity = normaliseAddress(as)
178 built.app.allowUnauthenticated = true
179 signins, err := NewAuth(database.Writer(), AuthEnv{
180 Origin: config.Origin, InstanceKey: config.InstanceKey,
181 Mailer: mail.Logged(logger)}, logger, built.app.Admit)
182 if err != nil {
183 return Built{}, err
184 }
185 built.UseAuth(signins)
186 return built, nil
187}
188