-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
74 lines (61 loc) · 1.78 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/braintree/manners"
"github.com/askcarter/io16/app/handlers"
"github.com/askcarter/io16/app/health"
)
const version = "1.0.0"
func main() {
var (
httpAddr = flag.String("http", "0.0.0.0:80", "HTTP service address.")
healthAddr = flag.String("health", "0.0.0.0:81", "Health service address.")
)
flag.Parse()
log.Println("Starting server...")
log.Printf("Health service listening on %s", *healthAddr)
log.Printf("HTTP service listening on %s", *httpAddr)
errChan := make(chan error, 10)
hmux := http.NewServeMux()
hmux.HandleFunc("/healthz", health.HealthzHandler)
hmux.HandleFunc("/readiness", health.ReadinessHandler)
hmux.HandleFunc("/healthz/status", health.HealthzStatusHandler)
hmux.HandleFunc("/readiness/status", health.ReadinessStatusHandler)
healthServer := manners.NewServer()
healthServer.Addr = *healthAddr
healthServer.Handler = handlers.LoggingHandler(hmux)
go func() {
errChan <- healthServer.ListenAndServe()
}()
mux := http.NewServeMux()
mux.HandleFunc("/", handlers.HelloHandler)
mux.Handle("/secure", handlers.JWTAuthHandler(handlers.HelloHandler))
mux.Handle("/version", handlers.VersionHandler(version))
httpServer := manners.NewServer()
httpServer.Addr = *httpAddr
httpServer.Handler = handlers.LoggingHandler(mux)
go func() {
errChan <- httpServer.ListenAndServe()
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case err := <-errChan:
if err != nil {
log.Fatal(err)
}
case s := <-signalChan:
log.Println(fmt.Sprintf("Captured %v. Exiting...", s))
health.SetReadinessStatus(http.StatusServiceUnavailable)
httpServer.BlockingClose()
os.Exit(0)
}
}
}