-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
39 lines (32 loc) · 886 Bytes
/
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
package server
import (
"encoding/json"
"github.com/cwkr/auth-server/internal/httputil"
"github.com/cwkr/auth-server/internal/people"
"log"
"net/http"
)
type healthHandler struct {
peopleStore people.Store
}
func (i *healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var status = struct {
Status string `json:"status"`
}{"UP"}
httputil.NoCache(w)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Content-Type-Options", "nosniff")
if err := i.peopleStore.Ping(); err != nil {
log.Printf("%s %s", r.Method, r.URL)
log.Printf("!!! 503 Service Unavailable - %s", err.Error())
status.Status = err.Error()
w.WriteHeader(http.StatusServiceUnavailable)
}
var bytes, _ = json.Marshal(status)
w.Write(bytes)
}
func HealthHandler(peopleStore people.Store) http.Handler {
return &healthHandler{
peopleStore: peopleStore,
}
}