/
services.go
65 lines (52 loc) · 1.51 KB
/
services.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
package server
import (
"encoding/json"
"net/http"
"github.com/Shopify/goose/srvutil"
"github.com/gorilla/mux"
)
var branch string
var revision string
func NewServicesServlet() srvutil.Servlet {
s := &servicesServlet{}
return srvutil.PrefixServlet(s, "/services")
}
type servicesServlet struct{}
type version struct {
Branch string `json:"branch"`
Revision string `json:"revision"`
}
func (s *servicesServlet) RegisterRouting(r *mux.Router) {
r.HandleFunc("/ping", s.ping)
r.HandleFunc("/present", s.exposurePresence)
r.HandleFunc("/version.json", s.version)
}
func (s *servicesServlet) exposurePresence(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-store")
w.WriteHeader(http.StatusNoContent)
}
func (s *servicesServlet) ping(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
if _, err := w.Write([]byte("OK\n")); err != nil {
log(ctx, err).Info("error writing response")
}
}
func (s *servicesServlet) version(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Content-Type", "application/json; charset=utf-8")
version := version{
Branch: branch,
Revision: revision,
}
js, err := json.Marshal(version)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := w.Write(js); err != nil {
log(ctx, err).Info("error writing response")
}
}