-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteward.go
42 lines (36 loc) · 1015 Bytes
/
steward.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
// Package steward provides a drop in way of adding a status endpoint to your
// services
package steward
import (
"encoding/json"
"net/http"
)
var (
endpoint = "/status"
resp interface{}
)
func init() {
http.Handle(endpoint, http.HandlerFunc(HandleStatus))
}
// HandleStatus implements the go http handler interface to
// display specified status details
func HandleStatus(w http.ResponseWriter, req *http.Request) {
payload, err := json.MarshalIndent(resp, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(payload) //nolint:errcheck
}
// SetStatusResponse overrides the default response payload
// for the status endpoint
func SetStatusResponse(v interface{}) {
resp = v
}
// SetStatusEndpoint overrides the default endpoint with desired value
func SetStatusEndpoint(e string) {
endpoint = e
http.Handle(endpoint, http.HandlerFunc(HandleStatus))
}