-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (48 loc) · 1.38 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
package main
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/ONSdigital/dp-frontend-release-calendar/config"
"github.com/ONSdigital/dp-frontend-release-calendar/service"
"github.com/ONSdigital/log.go/v2/log"
)
func main() {
log.Namespace = "dp-frontend-release-calendar"
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(ctx, "application unexpectedly failed", err)
}
os.Exit(0)
}
func run(ctx context.Context) error {
// Create error channel for os signals
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
// Create service initialiser and an error channel for service errors
svcList := service.NewServiceList(&service.Init{})
svcErrors := make(chan error, 1)
// Read config
cfg, err := config.Get()
if err != nil {
log.Error(ctx, "unable to retrieve service configuration", err)
return err
}
log.Info(ctx, "got service configuration", log.Data{"config": cfg})
// Run service
svc := service.New()
if err := svc.Init(ctx, cfg, svcList); err != nil {
log.Error(ctx, "failed to initialise service", err)
return err
}
svc.Run(ctx, svcErrors)
// Blocks until an os interrupt or a fatal error occurs
select {
case err := <-svcErrors:
log.Error(ctx, "service error received", err)
case sig := <-signals:
log.Info(ctx, "os signal received", log.Data{"signal": sig})
}
return svc.Close(ctx)
}