/
readiness_handler.go
68 lines (57 loc) · 1.57 KB
/
readiness_handler.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
package rest_handlers
import (
"context"
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
)
type ReadinessAPIStatus struct {
DataBase string `json:"database"`
Collections string `json:"collections"`
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
// ReadinessHandler returns current status of this application
func ReadinessHandler(mongoClient *mongo.Client, mongodbOperator mongodb.MongoOperator) gin.HandlerFunc {
return func(c *gin.Context) {
var (
dbFlag = "up"
colFlag = "up"
)
dbs, err := mongodbOperator.ListDataBase(context.Background(), mongoClient)
if err != nil {
log.Error(err)
dbFlag = "down"
}
if !contains(dbs, "litmus") {
dbFlag = "down"
}
cols, err := mongodbOperator.ListCollection(context.Background(), mongoClient)
if err != nil {
log.Error(err)
colFlag = "down"
}
if !contains(cols, "gitops-collection") || !contains(cols, "server-config-collection") || !contains(cols, "workflow-collection") {
colFlag = "down"
}
var status = ReadinessAPIStatus{Collections: colFlag, DataBase: dbFlag}
statusByte, err := json.Marshal(status)
if err != nil {
log.Error(err)
utils.WriteHeaders(&c.Writer, http.StatusBadRequest)
return
}
utils.WriteHeaders(&c.Writer, http.StatusOK)
c.Writer.Write(statusByte)
}
}