oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "context"
5 "io"
6 "log/slog"
7 "net/http"
8 "os"
9 "os/signal"
10 "syscall"
11 "time"
12
13 "amadan.net/rastrillo/rastrillo"
14 "amadan.net/rastrillo/rastrillo/db"
15
16 "amadan.net/oficina/equipo/internal/equipo"
17)
18
19func main() {
20 os.Exit(commandMain(os.Args[1:], os.Stdout, os.Stderr, nil))
21}
22
23func commandMain(args []string, out, diagnostics io.Writer, transport http.RoundTripper) int {
24 if handled, code := runDBCheckCommand(args, out); handled {
25 return code
26 }
27 if handled, code := runCheckCommand(args, out, transport); handled {
28 return code
29 }
30 started := time.Now()
31 logger := slog.New(slog.NewTextHandler(diagnostics, nil))
32 if err := run(logger, started); err != nil {
33 logger.Error(equipo.BootLog, "err", err)
34 return 1
35 }
36 return 0
37}
38
39// Returning to main lets the database close on failed app construction too.
40func run(logger *slog.Logger, started time.Time) error {
41 cfg, err := equipo.LoadConfig(os.Getenv)
42 if err != nil {
43 return err
44 }
45 // Before the database opens, so a half-set submission refuses first.
46 mailer, err := equipo.Mailer(cfg, logger)
47 if err != nil {
48 return err
49 }
50 // Pushes too: only here, where Equipo serves.
51 if cfg.Push, err = equipo.Pusher(cfg, logger, os.Getenv); err != nil {
52 return err
53 }
54 opts, err := rastrillo.Resolve(rastrillo.Options{DBPath: cfg.DatabaseName, Logger: logger})
55 if err != nil {
56 return err
57 }
58 d, err := db.Open(opts.DBPath, logger)
59 if err != nil {
60 return err
61 }
62 defer d.Close()
63 store := equipo.NewStore(d)
64 // On SIGTERM, end person streams and send runners drain before the
65 // framework's own shutdown, so clients reconnect to the next process.
66 draining, stopDraining := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
67 defer stopDraining()
68 go func() {
69 <-draining.Done()
70 store.DrainStreams()
71 }()
72 execution, err := equipo.OpenExecution(context.Background(), d, opts.DBPath, cfg, store)
73 if err != nil {
74 return err
75 }
76 defer execution.Close()
77 mux, err := equipo.AppWithExecution(d, cfg, mailer, logger, execution)
78 if err != nil {
79 return err
80 }
81 equipo.Configure(&opts, mux, started)
82 if opts.Background != nil {
83 defer opts.Background.Stop()
84 }
85 opts.DBPath = ""
86 return rastrillo.Serve(opts)
87}
88