/
healthcheck.go
39 lines (31 loc) 路 983 Bytes
/
healthcheck.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
package controllers
import (
"net/http"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"github.com/vareversat/chabo-api/internal/db"
"github.com/vareversat/chabo-api/internal/models"
"go.mongodb.org/mongo-driver/mongo"
)
// Healthcheck godoc
//
// @Summary Get the status of the API
// @Tags Misc
// @Produce json
// @Success 200 {object} models.OKResponse{} "The api is healthy"
// @Failure 503 {object} models.ErrorResponse{} "The api is unhealthy"
// @Router /healthcheck [get]
func Healthcheck(mongoClient *mongo.Client) gin.HandlerFunc {
fn := func(c *gin.Context) {
if hub := sentrygin.GetHubFromContext(c); hub != nil {
hub.Scope().SetTag("controller", "healthcheck")
}
err := db.Ping(mongoClient)
if err != nil {
c.JSON(http.StatusServiceUnavailable, models.ErrorResponse{Error: err.Error()})
return
}
c.JSON(http.StatusOK, models.OKResponse{Message: "chabo-api is healthy"})
}
return gin.HandlerFunc(fn)
}