This repository was archived by the owner on May 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (73 loc) · 2.07 KB
/
main.go
File metadata and controls
92 lines (73 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"errors"
"log" //nolint:depguard // non-o11y log is allowed for a top-level fatal
"time"
"github.com/alecthomas/kong"
"github.com/circleci/ex/httpserver"
"github.com/circleci/ex/httpserver/healthcheck"
"github.com/circleci/ex/o11y"
"github.com/circleci/ex/system"
"github.com/circleci/ex/termination"
"github.com/circleci/ex-service-template/api/api"
"github.com/circleci/ex-service-template/books"
"github.com/circleci/ex-service-template/cmd"
"github.com/circleci/ex-service-template/cmd/setup"
)
type cli struct {
setup.CLI
ShutdownDelay time.Duration `env:"SHUTDOWN_DELAY" default:"5s" help:"Delay shutdown by this amount" hidden:""`
APIAddr string `env:"API_ADDR" default:":8000" help:"The address for the API to listen on"`
/* other specific env vars in here*/
}
func main() {
err := run(cmd.Version, cmd.Date)
if err != nil && !errors.Is(err, termination.ErrTerminated) {
log.Fatal("Unexpected Error: ", err)
}
log.Println("exited 0")
}
func run(version, date string) (err error) {
cli := cli{}
kong.Parse(&cli)
ctx, o11yCleanup, err := setup.LoadO11y(version, "api", cli.CLI)
if err != nil {
return err
}
defer o11yCleanup(ctx)
ctx, runSpan := o11y.StartSpan(ctx, "main: run")
defer o11y.End(runSpan, &err)
o11y.Log(ctx, "starting api",
o11y.Field("version", version),
o11y.Field("date", date),
)
sys := system.New()
defer sys.Cleanup(ctx)
/* load other things in here that the API might need*/
err = loadAPI(ctx, cli, sys)
if err != nil {
return err
}
// Should be last so it collects all the health checks
_, err = healthcheck.Load(ctx, cli.AdminAddr, sys)
if err != nil {
return err
}
return sys.Run(ctx, cli.ShutdownDelay)
}
func loadAPI(ctx context.Context, cli cli, sys *system.System) error {
txm, err := setup.LoadTxManager(ctx, cli.CLI, sys)
if err != nil {
return err
}
a := api.New(ctx, api.Options{
Store: books.NewStore(txm),
})
_, err = httpserver.Load(ctx, httpserver.Config{
Name: "api",
Addr: cli.APIAddr,
Handler: a.Handler(),
}, sys)
return err
}