-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
84 lines (70 loc) · 2.6 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
81
82
83
84
package main
import (
"context"
"sync"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/geomyidia/go-svc-conventions/internal/util"
"github.com/geomyidia/go-svc-conventions/pkg/components"
"github.com/geomyidia/go-svc-conventions/pkg/components/config"
"github.com/geomyidia/go-svc-conventions/pkg/components/db"
"github.com/geomyidia/go-svc-conventions/pkg/components/grpcd"
"github.com/geomyidia/go-svc-conventions/pkg/components/httpd"
"github.com/geomyidia/go-svc-conventions/pkg/components/logging"
"github.com/geomyidia/go-svc-conventions/pkg/components/msgbus"
)
func main() {
// Create the application object and assign components to it
a := new(components.Application)
a.Config = config.NewConfig()
a.Logger = logging.Load(a.Config)
a.Bus = msgbus.NewMsgBus()
a.DB = db.NewDB(a.Config, a.Bus)
a.HTTPD = httpd.NewHTTPServer(a.Config, a.Bus, a.DB)
a.GRPCD = grpcd.NewGRPCServer(a.Config, a.Bus, a.DB)
// Set up subscriptions
a.Bus.Subscribe("ping", func(event *msgbus.Event) { log.Warnf("Got event: %#v", event) })
a.Bus.Subscribe("version", func(event *msgbus.Event) { log.Warnf("Got event: %#v", event) })
a.Bus.Subscribe("*", func(event *msgbus.Event) { log.Warnf("Auditor got event: %#v", event) })
// Create context that listens for the interrupt signal from the OS.
ctx, cancel := util.SignalWithContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
var wg sync.WaitGroup
// Initialise the HTTP server in its own goroutine and wire to wait group
wg.Add(1)
go func() {
defer wg.Done()
a.DB.Connect()
}()
// Initialise the HTTP server in its own goroutine and wire to wait group
wg.Add(1)
go func() {
defer wg.Done()
a.HTTPD.Serve()
}()
// Initialise the gRPC server in its own goroutine and wire to wait group
// XXX this has been dsiable since it blocks (due to gRPC server-shutdown
// not using context / cancelation) Is there are way to cancel gRPC
// servers with a context?
// ticket: https://github.com/geomyidia/go-svc-conventions/issues/11
//wg.Add(1)
go func() {
//defer wg.Done()
a.GRPCD.Serve()
}()
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
cancel()
log.Info("Shutting down gracefully, press Ctrl+C again to force")
// The HTTP server requires a context to shutdown
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
// Shutdown running components
a.HTTPD.Shutdown(ctx)
a.GRPCD.Shutdown()
a.DB.Shutdown()
log.Info("Waiting for wait groups to finish ...")
wg.Wait()
log.Info("Application shutdown complete.")
}