-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (58 loc) · 1.52 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"syscall"
"github.com/braintree/manners"
"github.com/gerlacdt/hello/handlers"
"os/signal"
)
func logEnvionmentVariables() {
for _, e := range os.Environ() {
log.Printf("%s\n", e)
}
}
func main() {
log.Printf("Starting hello-app, version: %s\n", handlers.Version)
logEnvionmentVariables()
httpPort := os.Getenv("NOMAD_PORT_http")
if httpPort == "" {
log.Fatal("NOMAD_PORT_http must be set and not-empty")
}
log.Printf("HTTP service port listening on %s", httpPort)
// create web-router
mux := http.NewServeMux()
mux.Handle("/hello", http.HandlerFunc(handlers.HelloHandler))
mux.Handle("/version", http.HandlerFunc(handlers.VersionHandler))
mux.Handle("/health", http.HandlerFunc(handlers.HealthHandler))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\"msg\": \"NOT_FOUND\"}")
})
// start server
httpServer := manners.NewServer()
httpServer.Addr = ":" + httpPort
httpServer.Handler = mux
// handle graceful shutdown
errChan := make(chan error, 10)
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("Caputured %v. Exiting...", s))
httpServer.BlockingClose()
os.Exit(0)
}
}
}