-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
53 lines (46 loc) · 1.06 KB
/
health.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
package main
import (
"log"
"net/http"
"os"
)
// HealthServer implements a *http.Server as server
type HealthServer struct {
server *http.Server
}
// NewHealthServer returns a HealthServer
func NewHealthServer() *HealthServer {
mux := http.NewServeMux()
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Pong"))
})
port := "8080"
envHealthPort := os.Getenv("SW_HEALTH_PORT")
if len(envHealthPort) != 0 {
port = envHealthPort
}
Addr := ":" + port
log.Printf("SET health server port to %v", Addr)
return &HealthServer{
server: &http.Server{
Addr: Addr,
Handler: mux,
},
}
}
func (s *HealthServer) start() {
log.Println("Starting health server")
err := s.server.ListenAndServe()
if err != nil {
log.Fatalf("Failed to serve health server on %v - %v", s.server.Addr, err)
}
}
func (s *HealthServer) close() {
err := s.server.Close()
if err != nil {
log.Printf("Error closing down health server - %v", err)
} else {
log.Println("Successfully closed health server")
}
}