forked from deliveroo/pgbouncer-healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
112 lines (99 loc) · 2.58 KB
/
router.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"os/exec"
"sync"
"time"
)
type requestHandler func(context.Context, http.ResponseWriter) error
func (h requestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if err := h(ctx, w); err != nil {
errMsg := fmt.Sprintf("An error occured: %s", err)
http.Error(w, errMsg, http.StatusInternalServerError)
return
}
}
func returnJSON(w http.ResponseWriter, item interface{}) error {
resp, err := json.Marshal(item)
if err != nil {
return errors.Wrap(err, "Error converting response to JSON")
}
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
return nil
}
type Mux struct {
initOnce sync.Once
mux *http.ServeMux
}
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.mux.ServeHTTP(w, r)
}
func (m *Mux) init() {
m.initOnce.Do(func() {
m.mux = http.NewServeMux()
})
}
func (m *Mux) GET(pattern string, handler http.Handler) {
m.init()
h := handler
h = AllowedMethod(h, http.MethodGet)
h = RequestWithTimeout(h, 10*time.Second)
m.mux.Handle(pattern, h)
}
func (m *Mux) OK(pattern string) {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != pattern {
http.NotFound(w, r)
return
}
}
m.GET(pattern, http.HandlerFunc(handler))
}
func (m *Mux) File(pattern string, desc string, path string) {
handler := func(ctx context.Context, w http.ResponseWriter) error {
output, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "Error fetching %s", desc)
}
w.Write(output)
return nil
}
m.GET(pattern, requestHandler(handler))
}
func (m *Mux) Command(pattern string, desc string, cmd string, args ...string) {
handler := func(ctx context.Context, w http.ResponseWriter) error {
cmd := exec.CommandContext(ctx, cmd, args...)
output, err := cmd.Output()
if err != nil {
return errors.Wrapf(err, "Error fetching %s", desc)
}
w.Write(output)
return nil
}
m.GET(pattern, requestHandler(handler))
}
func AllowedMethod(h http.Handler, method string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != method {
code := http.StatusMethodNotAllowed
http.Error(w, http.StatusText(code), code)
return
}
h.ServeHTTP(w, r)
})
}
func RequestWithTimeout(h http.Handler, timeout time.Duration) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
})
}