| 1 | // Package appboot starts an app Base installs. Every such app boots the same |
| 2 | // way: read the BASE_APP_* settings, open and migrate its database, build its |
| 3 | // router, and serve it with rastrillo. Only the inputs in App differ. |
| 4 | package appboot |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "log/slog" |
| 10 | "net/http" |
| 11 | "os" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | _ "amadan.net/rastrillo/rastrillo/locales/en" |
| 16 | "amadan.net/rastrillo/rastrillo/migrate" |
| 17 | |
| 18 | "amadan.net/oficina/base/internal/consumer" |
| 19 | ) |
| 20 | |
| 21 | // App is what one installed app brings to the boot. |
| 22 | type App struct { |
| 23 | Name string // for log lines, such as "scratchpad" |
| 24 | DBFile string // the database file name when the platform passes no -db |
| 25 | Schema *migrate.Set // everything the app's database needs |
| 26 | // Build returns the app's router. d has Schema applied, and cfg.Logger |
| 27 | // is set. |
| 28 | Build func(d *db.DB, cfg consumer.Config, logger *slog.Logger) (http.Handler, error) |
| 29 | } |
| 30 | |
| 31 | // Main runs app with the process's environment and exits 1 if it cannot |
| 32 | // start. |
| 33 | func Main(app App) { |
| 34 | logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) |
| 35 | if err := Run(app, os.Getenv, logger); err != nil { |
| 36 | logger.Error(app.Name+" failed to start", "err", err) |
| 37 | os.Exit(1) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Run reads the settings with getenv, opens and migrates the database, |
| 42 | // builds the app and serves it until rastrillo stops. The database is |
| 43 | // closed whenever Run returns. |
| 44 | func Run(app App, getenv func(string) string, logger *slog.Logger) error { |
| 45 | if app.Name == "" || app.DBFile == "" || app.Schema == nil || app.Build == nil { |
| 46 | return errors.New("appboot: an App needs a Name, DBFile, Schema and Build") |
| 47 | } |
| 48 | cfg, err := consumer.Load(getenv) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | opts, err := rastrillo.Resolve(rastrillo.Options{DBPath: app.DBFile, Logger: logger}) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | handler, closeDB, err := open(app, cfg, opts.DBPath, logger) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | defer closeDB() |
| 61 | // rastrillo.Serve wants a *http.ServeMux; the app's router takes every path. |
| 62 | mux := http.NewServeMux() |
| 63 | mux.Handle("/", handler) |
| 64 | opts.Mux = mux |
| 65 | opts.DBPath = "" // the database is already open |
| 66 | return rastrillo.Serve(opts) |
| 67 | } |
| 68 | |
| 69 | // open opens and migrates the database at path and builds the app on it. On |
| 70 | // any error the database is already closed. |
| 71 | func open(app App, cfg consumer.Config, path string, logger *slog.Logger) (http.Handler, func() error, error) { |
| 72 | d, err := db.Open(path, logger) |
| 73 | if err != nil { |
| 74 | return nil, nil, err |
| 75 | } |
| 76 | if _, err := migrate.Apply(context.Background(), d, app.Schema); err != nil { |
| 77 | d.Close() |
| 78 | return nil, nil, err |
| 79 | } |
| 80 | cfg.Logger = logger |
| 81 | handler, err := app.Build(d, cfg, logger) |
| 82 | if err != nil { |
| 83 | d.Close() |
| 84 | return nil, nil, err |
| 85 | } |
| 86 | return handler, d.Close, nil |
| 87 | } |
| 88 | |