-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
140 lines (112 loc) · 3.93 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/mux"
cli "github.com/jawher/mow.cli"
metrics "github.com/rcrowley/go-metrics"
fthealth "github.com/Financial-Times/go-fthealth/v1_1"
logger "github.com/Financial-Times/go-logger/v2"
"github.com/Financial-Times/http-handlers-go/v2/httphandlers"
status "github.com/Financial-Times/service-status-go/httphandlers"
)
const (
appDescription = ""
// TODO: how long we would like to wait for response?
httpServerReadTimeout = 10 * time.Second
httpServerWriteTimeout = 15 * time.Second
httpServerIdleTimeout = 20 * time.Second
httpHandlersTimeout = 14 * time.Second
)
func main() {
app := cli.App("cm-go-service", appDescription)
appSystemCode := app.String(cli.StringOpt{
Name: "app-system-code",
Value: "cm-go-service",
Desc: "system Code of the application",
EnvVar: "APP_SYSTEM_CODE",
})
appName := app.String(cli.StringOpt{
Name: "app-name",
Value: "cm-go-service",
Desc: "application name",
EnvVar: "APP_NAME",
})
port := app.String(cli.StringOpt{
Name: "port",
Value: "8080",
Desc: "port to listen on",
EnvVar: "APP_PORT",
})
logLevel := app.String(cli.StringOpt{
Name: "log-level",
Value: "INFO",
Desc: "logging level (DEBUG, INFO, WARN, ERROR)",
EnvVar: "LOG_LEVEL",
})
log := logger.NewUPPLogger(*appName, *logLevel)
app.Action = func() {
log.Infof("starting with system code: %s, app name: %s, port: %s", *appSystemCode, *appName, *port)
healthService := NewHealthService(*appSystemCode, *appName, appDescription)
router := registerEndpoints(healthService, log)
server := newHTTPServer(*port, router)
go startHTTPServer(server, log)
waitForSignal()
stopHTTPServer(server, log)
}
err := app.Run(os.Args)
if err != nil {
log.Errorf("app could not start: %v", err)
return
}
}
func registerEndpoints(healthService *HealthService, log *logger.UPPLogger) http.Handler {
serveMux := http.NewServeMux()
// register supervisory endpoint that does not require logging and metrics collection
serveMux.HandleFunc("/__health", fthealth.Handler(healthService.Health()))
serveMux.HandleFunc(status.GTGPath, status.NewGoodToGoHandler(healthService.GTG))
serveMux.HandleFunc(status.BuildInfoPath, status.BuildInfoHandler)
// add services router and register endpoints specific to this service only
servicesRouter := mux.NewRouter()
//TODO: add real handlers
servicesRouter.HandleFunc("/test", TestHandler).Methods("GET")
// wrap the handler with certain middlewares providing logging of the requests,
// sending metrics and handler time out on certain time interval
var wrappedServicesRouter http.Handler = servicesRouter
wrappedServicesRouter = httphandlers.TransactionAwareRequestLoggingHandler(log, wrappedServicesRouter)
wrappedServicesRouter = httphandlers.HTTPMetricsHandler(metrics.DefaultRegistry, wrappedServicesRouter)
wrappedServicesRouter = http.TimeoutHandler(wrappedServicesRouter, httpHandlersTimeout, "")
serveMux.Handle("/", wrappedServicesRouter)
return serveMux
}
func newHTTPServer(port string, router http.Handler) *http.Server {
return &http.Server{
Addr: ":" + port,
Handler: router,
ReadTimeout: httpServerReadTimeout,
WriteTimeout: httpServerWriteTimeout,
IdleTimeout: httpServerIdleTimeout,
}
}
func startHTTPServer(server *http.Server, log *logger.UPPLogger) {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("http server failed to start: %v", err)
}
}
func stopHTTPServer(server *http.Server, log *logger.UPPLogger) {
log.Info("http server is shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("failed to gracefully shutdown the server: %v", err)
}
}
func waitForSignal() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
}