oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Command memoria wires the app: resolve the platform's activation argv,
2// open the database, unseal nothing until the DEK is in hand, serve.
3package main
4
5import (
6 "log/slog"
7 "os"
8
9 "amadan.net/rastrillo/rastrillo"
10 "amadan.net/rastrillo/rastrillo/db"
11
12 memoria "amadan.net/oficina/memoria/internal/memoria"
13)
14
15// LoadConfigFromEnv is LoadConfig against the real environment.
16func LoadConfigFromEnv() (memoria.Config, error) {
17 return memoria.LoadConfig(os.Getenv)
18}
19
20func main() {
21 logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
22
23 opts, err := rastrillo.Resolve(rastrillo.Options{DBPath: "memoria.db", Logger: logger})
24 if err != nil {
25 logger.Error("resolve activation", "err", err)
26 os.Exit(1)
27 }
28
29 // Every refusal in LoadConfig is a safety property, and it lives in
30 // a tested function rather than inline here — see config.go for the
31 // false-pass guard that made the case for moving it.
32 cfg, err := LoadConfigFromEnv()
33 if err != nil {
34 logger.Error("configuration", "err", err)
35 os.Exit(1)
36 }
37 if cfg.UsedDefaultOrigin() {
38 logger.Warn("MEMORIA_ORIGIN not set; defaulting", "origin", cfg.Origin)
39 }
40
41 d, err := db.Open(opts.DBPath, logger)
42 if err != nil {
43 logger.Error("open database", "err", err)
44 os.Exit(1)
45 }
46 defer d.Close()
47
48 // AppWith, from the whole configuration: team mode (the five
49 // MEMORIA_TEAM_* variables) is verified, and the database's marker
50 // read, before anything migrates.
51 mux, app, err := memoria.AppWith(d, cfg, logger)
52 if err != nil {
53 logger.Error("build app", "err", err)
54 os.Exit(1)
55 }
56
57 // Wire the mailer. Without this rastrillo substitutes mail.Logged,
58 // which writes the magic link — a live credential — into the process
59 // log. LoadConfig refuses to boot in that state unless asked
60 // explicitly, but refusing was only half the fix: nothing was
61 // building a real sender either.
62 //
63 // memoria.Mailer: CARLOS mail submission or SMTP under the suite's
64 // CARLOS prefix, through mail.FromCarlos.
65 //
66 // The owners list is set FIRST, because app.Admit reads it: it is the
67 // one admission predicate, used by rastrillo at verification, by the
68 // sign-in guard before the classifier dials, and by requireOwner on
69 // the connector routes. Ordering it after NewAuth would work — Admit
70 // reads the list at call time — and would hide the dependency, which
71 // is how the two-copy version got here.
72 //
73 // In slice 0 the connector routes check the same list the admission
74 // gate uses, so every signed-in person is an owner. requireOwner says
75 // that out loud rather than implying a restriction the app does not
76 // yet have.
77 app.SetOwners(cfg.Owners)
78
79 mailer, err := memoria.Mailer(cfg, logger)
80 if err != nil {
81 logger.Error("configure mail", "err", err)
82 os.Exit(1)
83 }
84 a, err := memoria.NewAuth(d.Writer(), memoria.AuthEnv{
85 Origin: cfg.Origin,
86 InstanceKey: cfg.InstanceKey,
87 Owners: cfg.Owners,
88 Mailer: mailer,
89 HomeEnabled: cfg.Home != nil,
90 }, logger, app)
91 if err != nil {
92 logger.Error("wire sign-in", "err", err)
93 os.Exit(1)
94 }
95 app.UseAuth(a)
96 // WireHome also opens team mode's roster runtime, when configured.
97 // Its errors carry no configured value.
98 if err := app.WireHome(mux, cfg); err != nil {
99 logger.Error("Home sign-in unavailable", "err", err)
100 os.Exit(1)
101 }
102 if err := app.WireAgentAccess(mux, cfg); err != nil {
103 logger.Error("Agent access unavailable")
104 os.Exit(1)
105 }
106 app.MountAuth(mux, cfg.Origin)
107
108 // The apps of record memoria may ask about passages (§4.1).
109 //
110 // One call, deliberately: the roster, the custody, the log line and
111 // the wake poll all live in WireSiblings, where they have tests. This
112 // file's guard is a gate over its own text, and a gate over text can
113 // only see that a call is spelled — a review put the same wiring
114 // behind `if len(apps) > 1` and inside an uninvoked closure, and both
115 // read as present. Logic that matters does not live here.
116 //
117 // Fatal on error, like every other refusal in this file: a
118 // half-configured roster does not fail loudly at query time, it
119 // serves a permanently short page blaming an app that is working
120 // fine. An EMPTY roster is not a failure.
121 //
122 // nil dial: the module's team-bound transport does its own host check
123 // before dialling. The hook is for tests.
124 // The model layer (§8.9): off unless MEMORIA_AI=on. One call; the logic
125 // and its tests are in WireModel.
126 if err := memoria.WireModel(app, cfg); err != nil {
127 logger.Error("wire model layer", "err", err)
128 os.Exit(1)
129 }
130
131 if err := memoria.WireSiblings(app, os.Getenv, nil); err != nil {
132 logger.Error("sibling roster", "err", err)
133 os.Exit(1)
134 }
135
136 // The router, the error page, and rastrillo's default CSP
137 // (memoria.ServeOptions says why there is no policy of our own).
138 opts = app.ServeOptions(opts, mux)
139 opts.DBPath = ""
140 if err := rastrillo.Serve(opts); err != nil {
141 logger.Error("serve failed", "err", err)
142 os.Exit(1)
143 }
144}
145