oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "context"
5 "log/slog"
6 "net/http"
7 "os"
8 "time"
9
10 "amadan.net/oficina/home/connect"
11 "amadan.net/rastrillo/rastrillo"
12 "amadan.net/rastrillo/rastrillo/db"
13 _ "amadan.net/rastrillo/rastrillo/locales/en"
14 "amadan.net/rastrillo/rastrillo/mail"
15 "amadan.net/rastrillo/rastrillo/migrate"
16
17 "amadan.net/oficina/base/internal/config"
18 "amadan.net/oficina/base/internal/install"
19 "amadan.net/oficina/base/internal/platform"
20 "amadan.net/oficina/base/internal/store"
21 "amadan.net/oficina/base/internal/web"
22)
23
24func main() {
25 logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
26 if err := run(logger); err != nil {
27 logger.Error("base failed to start", "err", err)
28 os.Exit(1)
29 }
30}
31
32// Returning to main lets the database close on failed construction too.
33func run(logger *slog.Logger) error {
34 cfg, err := config.Load(os.Getenv)
35 if err != nil {
36 return err
37 }
38 // Sign-in with Oficina ID: off with no BASE_HOME_* settings, and a
39 // refusal to start with only some of them.
40 if cfg.Home, err = config.LoadHome(connect.CredentialsFromEnv("BASE"), cfg.Origin); err != nil {
41 return err
42 }
43 opts, err := rastrillo.Resolve(rastrillo.Options{DBPath: "base.db", Logger: logger})
44 if err != nil {
45 return err
46 }
47 d, err := db.Open(opts.DBPath, logger)
48 if err != nil {
49 return err
50 }
51 defer d.Close()
52 ctx := context.Background()
53 if _, err := migrate.Apply(ctx, d, store.Boot(cfg.Home != nil)); err != nil {
54 return err
55 }
56 if err := store.Bootstrap(ctx, d.Writer(), cfg.Owner); err != nil {
57 return err
58 }
59 var installer web.Installer = noInstaller{}
60 if cfg.PlatformConsole == "" || cfg.PlatformAccount == "" || cfg.InstanceToken == "" || cfg.OperateToken == "" || cfg.AssertionKey == nil || cfg.AppsDomain == "" {
61 logger.Warn("installs are disabled: set BASE_PLATFORM_CONSOLE, BASE_PLATFORM_ACCOUNT, BASE_PLATFORM_INSTANCE_TOKEN, BASE_PLATFORM_OPERATE_TOKEN, BASE_APPS_DOMAIN and BASE_ASSERTION_KEY to enable them")
62 } else {
63 hc := &http.Client{Timeout: 30 * time.Second}
64 in := install.New(d.Writer(), cfg, platform.NewHTTP(cfg.PlatformConsole, cfg.PlatformAccount, cfg.InstanceToken, cfg.OperateToken, hc), install.ProbeVersion(hc), logger)
65 installer = in
66 if err := store.ResetInterrupted(ctx, d.Writer()); err != nil {
67 return err
68 }
69 bg, stop := context.WithCancel(context.Background())
70 defer stop()
71 go func() {
72 if err := in.EnsureDefaults(bg); err != nil {
73 logger.Error("installing default apps", "err", err)
74 }
75 }()
76 }
77 // Static files are served by the app itself.
78 handler, err := web.App(d, cfg, mail.FromEnv("CARLOS", logger), installer, logger)
79 if err != nil {
80 return err
81 }
82 // rastrillo.Serve wants a *http.ServeMux; the chi router takes every path.
83 mux := http.NewServeMux()
84 mux.Handle("/", handler)
85 opts.Mux = mux
86 opts.DBPath = ""
87 return rastrillo.Serve(opts)
88}
89
90// noInstaller stands in when Platform settings are missing (local development).
91type noInstaller struct{}
92
93func (noInstaller) Start(context.Context, string) error { return nil }
94func (noInstaller) Uninstall(context.Context, string) error { return nil }
95