mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-25 15:53:09 -06:00
- Remove early return
- Use json.Encoder directly
This commit is contained in:
parent
bbf2c1376e
commit
0e6d4610a1
|
|
@ -32,9 +32,6 @@ type healthResponse struct {
|
||||||
|
|
||||||
// HealthCheckHandler adds a /health endpoint to the internal api mux
|
// HealthCheckHandler adds a /health endpoint to the internal api mux
|
||||||
func HealthCheckHandler(dbConfig ...config.DatabaseOptions) http.HandlerFunc {
|
func HealthCheckHandler(dbConfig ...config.DatabaseOptions) http.HandlerFunc {
|
||||||
if len(dbConfig) == 0 {
|
|
||||||
return func(_ http.ResponseWriter, _ *http.Request) {}
|
|
||||||
}
|
|
||||||
conns := make([]*sql.DB, len(dbConfig))
|
conns := make([]*sql.DB, len(dbConfig))
|
||||||
// populate sql connections
|
// populate sql connections
|
||||||
for i, conf := range dbConfig {
|
for i, conf := range dbConfig {
|
||||||
|
|
@ -45,36 +42,28 @@ func HealthCheckHandler(dbConfig ...config.DatabaseOptions) http.HandlerFunc {
|
||||||
conns[i] = c
|
conns[i] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(resp http.ResponseWriter, _ *http.Request) {
|
return func(rw http.ResponseWriter, _ *http.Request) {
|
||||||
var (
|
resp := &healthResponse{
|
||||||
errMsg string
|
Code: http.StatusOK,
|
||||||
code = http.StatusOK
|
FirstError: "",
|
||||||
)
|
|
||||||
err := dbPingCheck(conns, &errMsg, &code)
|
|
||||||
if err != nil {
|
|
||||||
resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(healthResponse{
|
err := dbPingCheck(conns, resp)
|
||||||
Code: code,
|
|
||||||
FirstError: errMsg,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("Unable to encode response")
|
rw.WriteHeader(resp.Code)
|
||||||
resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, err = resp.Write(data); err != nil {
|
|
||||||
logrus.WithError(err).Error("Unable to write health response")
|
if err := json.NewEncoder(rw).Encode(resp); err != nil {
|
||||||
|
logrus.WithError(err).Error("unable to encode health response")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbPingCheck(conns []*sql.DB, errMsg *string, code *int) error {
|
func dbPingCheck(conns []*sql.DB, resp *healthResponse) error {
|
||||||
// check every database connection
|
// check every database connection
|
||||||
for _, conn := range conns {
|
for _, conn := range conns {
|
||||||
if err := conn.Ping(); err != nil {
|
if err := conn.Ping(); err != nil {
|
||||||
*errMsg = err.Error()
|
resp.Code = http.StatusInternalServerError
|
||||||
*code = http.StatusInternalServerError
|
resp.FirstError = err.Error()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue