forked from callistaenterprise/goblog
/
main.go
75 lines (63 loc) · 1.81 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
package main
import (
"github.com/aclk/goblog/accountservice/cmd"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/aclk/goblog/accountservice/internal/app/service"
cb "github.com/aclk/goblog/common/circuitbreaker"
"github.com/aclk/goblog/common/messaging"
"github.com/aclk/goblog/common/tracing"
"github.com/alexflint/go-arg"
"github.com/sirupsen/logrus"
)
var appName = "accountservice"
func main() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.Infof("Starting %v\n", appName)
// Initialize config struct and populate it froms env vars and flags.
cfg := cmd.DefaultConfiguration()
arg.MustParse(cfg)
initializeTracing(cfg)
mc := initializeMessaging(cfg)
cb.ConfigureHystrix([]string{"account-to-data", "account-to-image", "account-to-quotes"}, mc)
client := &http.Client{}
var transport http.RoundTripper = &http.Transport{
DisableKeepAlives: true,
}
client.Transport = transport
cb.Client = client
h := service.NewHandler(mc, client)
qlResolvers := service.NewLiveGraphQLResolvers(h)
s := service.NewServer(cfg, h, qlResolvers)
s.SetupRoutes()
handleSigterm(func() {
cb.Deregister(mc)
mc.Close()
s.Close()
})
s.Start()
}
func initializeTracing(cfg *cmd.Config) {
tracing.InitTracing(cfg.ZipkinServerUrl, appName)
}
func initializeMessaging(cfg *cmd.Config) *messaging.AmqpClient {
if cfg.AmqpConfig.ServerUrl == "" {
panic("No 'amqp_server_url' set in configuration, cannot start")
}
mc := &messaging.AmqpClient{}
mc.ConnectToBroker(cfg.AmqpConfig.ServerUrl)
return mc
}
// Handles Ctrl+C or most other means of "controlled" shutdown gracefully. Invokes the supplied func before exiting.
func handleSigterm(handleExit func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
handleExit()
os.Exit(1)
}()
}