| 1 | // Command fichas wires the app: resolve the platform's activation |
| 2 | // argv, open the database, build the app, serve. |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "log/slog" |
| 8 | "os" |
| 9 | |
| 10 | "amadan.net/rastrillo/rastrillo" |
| 11 | "amadan.net/rastrillo/rastrillo/db" |
| 12 | |
| 13 | fichas "amadan.net/oficina/fichas/internal/fichas" |
| 14 | ) |
| 15 | |
| 16 | func main() { |
| 17 | logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) |
| 18 | |
| 19 | // Resolve, not Run: this app opens its own database handle via |
| 20 | // db.Open — a *gorm.DB with a split reader/writer pool, not the |
| 21 | // bare *sql.DB Options.Router would hand back — so Options.DBPath |
| 22 | // must be empty again by the Serve call below, or Serve would open |
| 23 | // a second, unused connection to the same file. |
| 24 | opts, err := rastrillo.Resolve(rastrillo.Options{DBPath: "fichas.db", Logger: logger}) |
| 25 | if err != nil { |
| 26 | logger.Error("resolve activation", "err", err) |
| 27 | os.Exit(1) |
| 28 | } |
| 29 | |
| 30 | d, err := db.Open(opts.DBPath, logger) |
| 31 | if err != nil { |
| 32 | logger.Error("open database", "err", err) |
| 33 | os.Exit(1) |
| 34 | } |
| 35 | defer d.Close() |
| 36 | |
| 37 | // Every refusal in LoadConfig is a safety property, and it lives in |
| 38 | // a tested function rather than inline here — config.go records the |
| 39 | // false-pass guard that made the case for moving it. |
| 40 | // |
| 41 | // Origin decides the CSRF same-origin check (and, when the app grows |
| 42 | // accounts, the Secure/__Host- cookie attributes), so defaulting it |
| 43 | // silently in production would be a real bug. Loud on purpose, |
| 44 | // harmless in local dev. |
| 45 | cfg, err := fichas.LoadConfig(os.Getenv) |
| 46 | if err != nil { |
| 47 | logger.Error("configuration", "err", err) |
| 48 | os.Exit(1) |
| 49 | } |
| 50 | if cfg.UsedDefaultOrigin() { |
| 51 | logger.Warn("FICHAS_ORIGIN not set; defaulting", "origin", cfg.Origin) |
| 52 | } |
| 53 | |
| 54 | mux, app, err := fichas.App(d, cfg, logger) |
| 55 | if err != nil { |
| 56 | logger.Error("build app", "err", err) |
| 57 | os.Exit(1) |
| 58 | } |
| 59 | // The value window's sweep (level-up Q11), off the boot path: it |
| 60 | // runs once now and hourly, and never stops the app. |
| 61 | go fichas.Sweep(context.Background(), d.G, logger) |
| 62 | |
| 63 | // Wire the mailer BEFORE auth. Without a real sender rastrillo |
| 64 | // substitutes mail.Logged, which writes the magic link — a live |
| 65 | // credential — into the process log; LoadConfig refuses to boot in |
| 66 | // that state unless FICHAS_ALLOW_LOGGED_MAIL says so out loud. But |
| 67 | // refusing was only half of memoria's fix: nothing was building a |
| 68 | // real sender either. fichas.Mailer: CARLOS mail submission or SMTP |
| 69 | // under the suite's CARLOS prefix, through mail.FromCarlos. |
| 70 | mailer, err := fichas.Mailer(cfg, logger) |
| 71 | if err != nil { |
| 72 | logger.Error("configure mail", "err", err) |
| 73 | os.Exit(1) |
| 74 | } |
| 75 | au, err := fichas.NewAuth(d.Writer(), fichas.AuthEnv{ |
| 76 | Origin: cfg.Origin, |
| 77 | InstanceKey: cfg.InstanceKey, |
| 78 | Mailer: mailer, |
| 79 | }, logger, app) |
| 80 | if err != nil { |
| 81 | logger.Error("wire sign-in", "err", err) |
| 82 | os.Exit(1) |
| 83 | } |
| 84 | app.UseAuth(au) |
| 85 | |
| 86 | opts.Mux = mux |
| 87 | opts.DBPath = "" |
| 88 | // The same page a handler's own error path renders (render.go's |
| 89 | // ErrorPage — wire it to Ctx.ErrorPage too as the app grows |
| 90 | // handlers), so a panic and a handled failure look identical. |
| 91 | opts.ErrorPage = fichas.ErrorPage |
| 92 | if err := rastrillo.Serve(opts); err != nil { |
| 93 | logger.Error("serve failed", "err", err) |
| 94 | os.Exit(1) |
| 95 | } |
| 96 | } |
| 97 | |