-
Notifications
You must be signed in to change notification settings - Fork 658
/
misc_handlers.go
92 lines (79 loc) · 2.08 KB
/
misc_handlers.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
package rest
import (
"net/http"
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/entities"
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/services"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
type ReadinessAPIStatus struct {
DataBase string `json:"database"`
Collections string `json:"collections"`
}
// Status godoc
//
// @Description Status will request users list and return, if successful, an http code 200.
// @Tags MiscRouter
// @Accept json
// @Produce json
// @Failure 500 {object} response.ErrServerError
// @Success 200 {object} response.Response{}
// @Router /status [get]
//
// Status will request users list and return, if successful,
// an http code 200
func Status(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
_, err := service.GetUsers()
if err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, entities.APIStatus{Status: "down"})
return
}
c.JSON(http.StatusOK, entities.APIStatus{Status: "up"})
}
}
// Readiness godoc
//
// @Description Return list of tags.
// @Tags MiscRouter
// @Accept json
// @Produce json
// @Failure 500 {object} response.ErrServerError
// @Success 200 {object} response.Response{}
// @Router /readiness [get]
func Readiness(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
var (
db_flag = "up"
col_flag = "up"
)
dbs, err := service.ListDataBase()
if !contains(dbs, "auth") {
db_flag = "down"
}
if err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, ReadinessAPIStatus{"down", "unknown"})
return
}
cols, err := service.ListCollection()
if !contains(cols, "project") || !contains(cols, "users") {
col_flag = "down"
}
if err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, ReadinessAPIStatus{db_flag, "down"})
return
}
c.JSON(http.StatusOK, ReadinessAPIStatus{db_flag, col_flag})
}
}