-
Notifications
You must be signed in to change notification settings - Fork 128
/
main.go
86 lines (69 loc) · 2.16 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
85
86
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"google.golang.org/grpc"
"github.com/G-Research/armada/internal/binoculars"
"github.com/G-Research/armada/internal/binoculars/configuration"
"github.com/G-Research/armada/internal/common"
gateway "github.com/G-Research/armada/internal/common/grpc"
"github.com/G-Research/armada/internal/common/health"
api "github.com/G-Research/armada/pkg/api/binoculars"
)
const CustomConfigLocation string = "config"
func init() {
pflag.StringSlice(CustomConfigLocation, []string{}, "Fully qualified path to application configuration file (for multiple config files repeat this arg or separate paths with commas)")
pflag.Parse()
}
func main() {
common.ConfigureLogging()
common.BindCommandlineArguments()
var config configuration.BinocularsConfig
userSpecifiedConfigs := viper.GetStringSlice(CustomConfigLocation)
common.LoadConfig(&config, "./config/binoculars", userSpecifiedConfigs)
log.Info("Starting...")
stopSignal := make(chan os.Signal, 1)
signal.Notify(stopSignal, syscall.SIGINT, syscall.SIGTERM)
shutdownMetricServer := common.ServeMetrics(config.MetricsPort)
defer shutdownMetricServer()
mux := http.NewServeMux()
startupComplete := health.NewStartupCompleteChecker()
health.SetupHttpMux(mux, startupComplete)
shutdownGateway := serveHttp(
mux,
config.HttpPort,
config.GrpcPort,
config.CorsAllowedOrigins,
api.SwaggerJsonTemplate(),
api.RegisterBinocularsHandler,
)
defer shutdownGateway()
shutdown, wg := binoculars.StartUp(&config)
go func() {
<-stopSignal
shutdown()
}()
startupComplete.MarkComplete()
wg.Wait()
}
func serveHttp(
mux *http.ServeMux,
port uint16,
grpcPort uint16,
corsAllowedOrigins []string,
spec string,
handlers ...func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error) (shutdown func()) {
shutdownGateway := gateway.CreateGatewayHandler(grpcPort, mux, "/", corsAllowedOrigins, spec, handlers...)
cancel := common.ServeHttp(port, mux)
return func() {
shutdownGateway()
cancel()
}
}