This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
api.go
163 lines (137 loc) · 5.19 KB
/
api.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package api
import (
"encoding/json"
"fmt"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/tedsuo/rata"
"github.com/cloudfoundry-incubator/galera-healthcheck/api/middleware"
"github.com/cloudfoundry-incubator/galera-healthcheck/config"
"github.com/cloudfoundry-incubator/galera-healthcheck/domain"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ReqHealthChecker
type ReqHealthChecker interface {
CheckReq(*http.Request) (string, error)
}
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . HealthChecker
type HealthChecker interface {
Check() (string, error)
}
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . StateSnapshotter
type StateSnapshotter interface {
State() (domain.DBState, error)
}
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . MonitClient
type MonitClient interface {
StartServiceBootstrap(req *http.Request) (string, error)
StartServiceJoin(req *http.Request) (string, error)
StartServiceSingleNode(req *http.Request) (string, error)
StopService(req *http.Request) (string, error)
GetStatus(req *http.Request) (string, error)
}
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . SequenceNumberChecker
type SequenceNumberChecker interface {
Check(req *http.Request) (string, error)
}
type RunFunc func(req *http.Request) (string, error)
type router struct {
logger lager.Logger
rootConfig *config.Config
monitClient MonitClient
sequenceNumberChecker SequenceNumberChecker
reqHealthChecker ReqHealthChecker
healthchecker HealthChecker
stateSnapshotter StateSnapshotter
}
func NewRouter(
logger lager.Logger,
rootConfig *config.Config,
monitClient MonitClient,
sequenceNumberChecker SequenceNumberChecker,
reqHealthChecker ReqHealthChecker,
healthchecker HealthChecker,
stateSnapshotter StateSnapshotter,
) (http.Handler, error) {
r := router{
logger: logger,
rootConfig: rootConfig,
monitClient: monitClient,
sequenceNumberChecker: sequenceNumberChecker,
reqHealthChecker: reqHealthChecker,
healthchecker: healthchecker,
stateSnapshotter: stateSnapshotter,
}
routes := rata.Routes{
{Name: "v1_status", Method: "GET", Path: "/api/v1/status"},
{Name: "mysql_status", Method: "GET", Path: "/mysql_status"},
{Name: "stop_mysql", Method: "POST", Path: "/stop_mysql"},
{Name: "start_mysql_bootstrap", Method: "POST", Path: "/start_mysql_bootstrap"},
{Name: "start_mysql_join", Method: "POST", Path: "/start_mysql_join"},
{Name: "start_mysql_single_node", Method: "POST", Path: "/start_mysql_single_node"},
{Name: "sequence_number", Method: "GET", Path: "/sequence_number"},
{Name: "galera_status", Method: "GET", Path: "/galera_status"},
{Name: "root", Method: "GET", Path: "/"},
}
handlers := rata.Handlers{
"v1_status": r.v1Status(),
"mysql_status": r.getSecureHandler(r.monitClient.GetStatus),
"stop_mysql": r.getSecureHandler(r.monitClient.StopService),
"start_mysql_bootstrap": r.getSecureHandler(r.monitClient.StartServiceBootstrap),
"start_mysql_join": r.getSecureHandler(r.monitClient.StartServiceJoin),
"start_mysql_single_node": r.getSecureHandler(r.monitClient.StartServiceSingleNode),
"sequence_number": r.getSecureHandler(r.sequenceNumberChecker.Check),
"galera_status": r.getInsecureHandler(r.reqHealthChecker.CheckReq),
"root": r.getInsecureHandler(r.reqHealthChecker.CheckReq),
}
handler, err := rata.NewRouter(routes, handlers)
if err != nil {
logger.Error("Error initializing router", err)
return nil, err
}
return handler, nil
}
func (r router) getSecureHandler(run RunFunc) http.Handler {
basicAuth := middleware.NewBasicAuth(
r.rootConfig.SidecarEndpoint.Username,
r.rootConfig.SidecarEndpoint.Password,
)
handler := r.getInsecureHandler(run)
return basicAuth.Wrap(handler)
}
func (r router) getInsecureHandler(run RunFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
body, err := run(req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
r.logger.Error("Failed to process request", err)
w.Write([]byte(err.Error()))
return
}
r.logger.Debug(fmt.Sprintf("Response body: %s", body))
w.Write([]byte(body))
})
}
func (r router) v1Status() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
s, err := r.stateSnapshotter.State()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
r.logger.Error("Failed to process request", err)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(V1StatusResponse{
WsrepLocalState: uint(s.WsrepLocalState),
WsrepLocalStateComment: string(s.WsrepLocalState.Comment()),
WsrepLocalIndex: s.WsrepLocalIndex,
Healthy: r.rootConfig.IsHealthy(s),
})
})
}
type V1StatusResponse struct {
WsrepLocalState uint `json:"wsrep_local_state"`
WsrepLocalStateComment string `json:"wsrep_local_state_comment"`
WsrepLocalIndex uint `json:"wsrep_local_index"`
Healthy bool `json:"healthy"`
}