-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
49 lines (42 loc) · 1.14 KB
/
health.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
package server
import (
"encoding/json"
"github.com/codex-team/hawk.collector/pkg/hawk"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
)
type HealthStatus struct {
RedisStatus bool `json:"redis_status"`
MongoDBStatus bool `json:"mongo_db_status"`
}
func (hs *HealthStatus) isAvailable() bool {
return hs.RedisStatus && hs.MongoDBStatus
}
// HandleHealth handles health statuses of redis, rabbitmq and mongodb
func (s *Server) HandleHealth(ctx *fasthttp.RequestCtx) {
healthStatus := HealthStatus{
RedisStatus: s.RedisClient.CheckAvailability(),
MongoDBStatus: s.AccountsMongoDBClient.CheckAvailability(),
}
if healthStatus.isAvailable() {
ctx.Response.SetStatusCode(200)
} else {
ctx.Response.SetStatusCode(500)
}
response, err := json.Marshal(healthStatus)
if err != nil {
log.Errorf("Error during response marshalling: %v", err)
hawk.Catch(err)
ctx.Response.SetStatusCode(500)
ctx.SetConnectionClose()
return
}
_, err = ctx.Write(response)
if err != nil {
log.Errorf("Error during response write: %v", err)
hawk.Catch(err)
ctx.Response.SetStatusCode(500)
ctx.SetConnectionClose()
return
}
}