oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Command sheets is the Sheets instance binary.
2//
3// The platform activation contract has no $PORT: the platform (or a
4// developer) invokes this binary as
5//
6// sheets --socket <path> --db <path>
7//
8// and rastrillo.Resolve/Serve own that contract end to end — activation
9// argv, LISTEN_FDS, $STATE_DIRECTORY, GET /healthz, GET /api/version,
10// SIGTERM drain, baseline security headers. This file only wires the
11// app's own *gorm.DB handle (via rastrillo's db package) into
12// sheets.New, because the app needs GORM, not the raw *sql.DB
13// rastrillo.Options.Router would hand back. It also runs migrate.Apply
14// against sheets.BootSchema before anything else touches the database —
15// see internal/sheets/migrations.go.
16//
17// rastrillo.Resolve/Run's shape at v0.21.0 differs from a zero-argument
18// Resolve()/Run(handler) pair: Resolve takes and returns an Options, and
19// an app that opens its own database handle (as this one does, for
20// gorm) uses Resolve+Serve directly rather than Run — see
21// $(go env GOMODCACHE)/github.com/carlosframework/rastrillo@v0.21.0/SKILL.md
22// and run.go's doc comment on Resolve.
23package main
24
25import (
26 "context"
27 "log/slog"
28 "os"
29
30 "amadan.net/rastrillo/rastrillo"
31 rdb "amadan.net/rastrillo/rastrillo/db"
32 "amadan.net/rastrillo/rastrillo/migrate"
33
34 "amadan.net/oficina/sheets/internal/sheets"
35)
36
37func main() {
38 logger := slog.Default()
39
40 opts, err := rastrillo.Resolve(rastrillo.Options{Logger: logger})
41 if err != nil {
42 logger.Error("resolve", "err", err)
43 os.Exit(1)
44 }
45
46 // Sign-in configuration is validated before anything serves. Every
47 // failure here is one where continuing would produce an instance
48 // that looks like it works: no mailer means magic links get written
49 // to platform-held logs instead of emailed, and an empty allowlist
50 // means anyone who can receive mail can hold a session. Both answer
51 // /healthz perfectly well, which is why this is a hard exit and not
52 // a warning.
53 authEnv, err := sheets.LoadAuthEnv(logger)
54 if err != nil {
55 logger.Error("sign-in", "err", err)
56 os.Exit(1)
57 }
58
59 d, err := rdb.Open(opts.DBPath, logger)
60 if err != nil {
61 logger.Error("open db", "err", err)
62 os.Exit(1)
63 }
64 defer d.Close()
65
66 team, err := sheets.TeamSettings(authEnv.Team, authEnv.Home, authEnv.Origin)
67 if err != nil {
68 logger.Error("team mode unavailable")
69 os.Exit(1)
70 }
71
72 if authEnv.AgentIssuer != "" && team != nil && authEnv.AgentTeam != team.Bindings().Team {
73 logger.Error("agent team does not match provisioned team")
74 os.Exit(1)
75 }
76
77 // Ledgered, once-at-boot, never re-run or reversed — see
78 // internal/sheets/migrations.go's Schema/BootSchema/Models and
79 // AGENTS.md's additive-only constraint. A boot that can't reconcile
80 // its schema (a structural diff migrate.Apply doesn't recognise as
81 // its own pending migrations) refuses rather than guessing, which
82 // is why this is a hard exit, same as a failed db.Open above.
83 //
84 // Team mode's marker is read first, before anything migrates: a
85 // database once activated in team mode refuses to boot without
86 // SHEETS_TEAM_*, or with a different binding, so a dropped variable
87 // cannot hand standing back to SHEETS_OWNERS
88 // (internal/sheets/team_config.go). Without team configuration on a
89 // database never activated, the schema is BootSchema exactly as
90 // before.
91 schema, err := sheets.BootSchemaFor(context.Background(), d.Writer(), team)
92 if err != nil {
93 logger.Error("team mode", "err", err)
94 os.Exit(1)
95 }
96 if _, err := migrate.Apply(context.Background(), d, schema); err != nil {
97 logger.Error("migrate", "err", err)
98 os.Exit(1)
99 }
100
101 // Same shape as the sign-in refusals above, and for the same
102 // reason: an instance with no DEK answers /healthz perfectly well
103 // and then fails on the first cell anybody types. The key arrives
104 // as platform config and is never written to the database — see
105 // internal/sheets/keys.go and spec §2.1.
106 sealer, err := sheets.LoadSealer()
107 if err != nil {
108 logger.Error("sealing", "err", err)
109 os.Exit(1)
110 }
111
112 // The app is built first and auth attached second, because the two
113 // are mutually dependent: auth's admission gate has to read this
114 // app's grants table so a guest somebody shared a sheet with can
115 // sign in at all. See App.Admits.
116 // Google Drive import: all three public SHEETS_GOOGLE_* settings or
117 // none. Some but not all, or a malformed one, refuses boot rather
118 // than serve a chooser that cannot work. The error carries no
119 // setting's value.
120 google, err := sheets.LoadGoogleImport()
121 if err != nil {
122 logger.Error("google import", "err", err)
123 os.Exit(1)
124 }
125
126 app := sheets.New(sheets.Config{
127 DB: d.G,
128 Sealer: sealer,
129 Origin: authEnv.Origin,
130 Admit: sheets.Admits(authEnv.Owners),
131 Team: team,
132 GoogleImport: google,
133 })
134
135 signin, err := sheets.NewAuth(d.Writer(), authEnv, logger, app)
136 if err != nil {
137 logger.Error("sign-in", "err", err)
138 os.Exit(1)
139 }
140 app.UseAuth(signin)
141 if err := app.ConfigureHome(authEnv.Home); err != nil {
142 logger.Error("home configuration unavailable")
143 os.Exit(1)
144 }
145 // After Home, before serving: commits the team marker and starts the
146 // roster cache. Without team settings it is a no-op; anything else
147 // that fails refuses boot.
148 if err := app.ConfigureTeam(); err != nil {
149 logger.Error("team mode unavailable")
150 os.Exit(1)
151 }
152
153 if err := app.ConfigureAgentAccess(authEnv.AgentIssuer, authEnv.AgentTeam, authEnv.Home); err != nil {
154 logger.Error("agent configuration unavailable")
155 os.Exit(1)
156 }
157
158 if err := app.ConfigureNative(); err != nil {
159 logger.Error("native configuration unavailable")
160 os.Exit(1)
161 }
162
163 // The router, the error page and the CSP (sheets.ServeOptions).
164 opts = app.ServeOptions(opts)
165
166 // The app opened its own database handle above (db.Open already
167 // Pings eagerly, which satisfies Serve's boot-materialization duty
168 // for a hibernate route's activator), so DBPath is blanked here —
169 // otherwise Serve would open a second connection to the same file.
170 opts.DBPath = ""
171
172 err = rastrillo.Serve(opts)
173 // Sign-in links are sent after the response (mailqueue.go); send
174 // what is still queued before the process goes.
175 sheets.DrainSigninMail()
176 if err != nil {
177 logger.Error("serve", "err", err)
178 os.Exit(1)
179 }
180}
181