-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (69 loc) · 1.47 KB
/
main.go
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
package main
import (
"context"
_ "embed"
"errors"
"flag"
"log/slog"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
)
var (
//go:embed resources.tar.gz
resourcesTar []byte
bindAddr = "127.0.0.1:8080"
fg = flag.NewFlagSet("fhir-test-server", flag.ContinueOnError)
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
log := slog.New(slog.NewTextHandler(
os.Stdout,
&slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key != "source" {
return a
}
return slog.Attr{
Key: a.Key,
Value: slog.StringValue(
strings.Replace(filepath.Base(strings.Trim(a.Value.String(), "{}")), " ", ":", 1),
),
}
},
},
))
fg.StringVar(&bindAddr, "bind", bindAddr, "Address, including port, to bind.")
if err := fg.Parse(os.Args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
}
log.Error("Error parsing flags", "err", err)
os.Exit(1)
}
if err := extractSeedResources(ctx, log); err != nil {
if errors.Is(err, context.Canceled) {
log.Info("Stopping.")
os.Exit(0)
}
log.Error("Error extracting resources", "err", err)
os.Exit(1)
}
ch := make(chan error)
go func() {
ch <- runWebserver(log)
}()
select {
case <-ctx.Done():
log.Info("Shutting down...")
os.Exit(0)
case err := <-ch:
log.Error("Server stopped", "err", err)
os.Exit(1)
}
}