| 1 | // Command mapa wires the app: resolve the platform's activation argv, |
| 2 | // open the database, build the app, serve. |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo" |
| 10 | "amadan.net/rastrillo/rastrillo/db" |
| 11 | |
| 12 | "amadan.net/oficina/mapa/internal/mapa" |
| 13 | ) |
| 14 | |
| 15 | func main() { |
| 16 | logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) |
| 17 | if err := run(logger); err != nil { |
| 18 | logger.Error("mapa", "err", err) |
| 19 | os.Exit(1) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // run is main's whole story. Resolve, not Run: Mapa opens its own |
| 24 | // database handle, so Options.DBPath is blanked before Serve, or Serve |
| 25 | // would open a second connection to the same file. |
| 26 | func run(logger *slog.Logger) error { |
| 27 | options, err := rastrillo.Resolve(rastrillo.Options{DBPath: "mapa.db", |
| 28 | Logger: logger}) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | database, err := db.Open(options.DBPath, logger) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | defer database.Close() |
| 37 | config, err := loadConfig(options.DBPath) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | if config.UsedDefaultOrigin() { |
| 42 | logger.Warn("MAPA_ORIGIN not set; defaulting", "origin", |
| 43 | config.Origin) |
| 44 | } |
| 45 | built, err := mapa.App(database, config, logger) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | // The mailer before auth: without a real sender rastrillo writes the |
| 50 | // magic link, a live credential, to the log. LoadConfig refuses that |
| 51 | // unless MAPA_ALLOW_LOGGED_MAIL says so on loopback. |
| 52 | mailer, err := mapa.Mailer(config, logger) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | signins, err := mapa.NewAuth(database.Writer(), mapa.AuthEnv{ |
| 57 | Origin: config.Origin, InstanceKey: config.InstanceKey, |
| 58 | Mailer: mailer}, logger, built.Admit) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | built.UseAuth(signins) |
| 63 | options.Mux, options.DBPath = built.Mux, "" |
| 64 | options.ErrorPage = built.ErrorPage |
| 65 | return rastrillo.Serve(options) |
| 66 | } |
| 67 | |
| 68 | // loadConfig is the platform's config, with development's images. |
| 69 | func loadConfig(dbPath string) (mapa.Config, error) { |
| 70 | config, err := mapa.LoadConfig(os.Getenv) |
| 71 | if err != nil { |
| 72 | return config, err |
| 73 | } |
| 74 | return config, localImages(&config, dbPath) |
| 75 | } |
| 76 | |
| 77 | // localImages keeps development's images in a directory beside the |
| 78 | // database (images decision 4); production with no store bound answers |
| 79 | // 503 to uploads. |
| 80 | func localImages(config *mapa.Config, dbPath string) error { |
| 81 | local, err := config.LocalImages(dbPath + ".images") |
| 82 | if local != nil { |
| 83 | config.Images.Objects = local |
| 84 | } |
| 85 | return err |
| 86 | } |
| 87 | |