golang-lib/web/api/status/main.go

52 lines
1.1 KiB
Go
Raw Normal View History

2021-06-01 10:51:35 +02:00
package status
import (
"net/http"
"github.com/gin-gonic/gin"
"dev.sum7.eu/genofire/golang-lib/web"
)
var (
2021-06-01 17:41:05 +02:00
// VERSION string on status API
VERSION string = ""
// UP function to detect, if API is healthy
UP func() bool = func() bool {
2021-06-01 10:51:35 +02:00
return true
}
2021-06-01 17:41:05 +02:00
// EXTRAS show more informations in status
2021-06-01 10:51:35 +02:00
EXTRAS interface{} = nil
)
2021-06-01 17:41:05 +02:00
// Status API response
2021-06-01 10:51:35 +02:00
type Status struct {
Version string `json:"version"`
Up bool `json:"up"`
Extras interface{} `json:"extras,omitempty"`
}
2021-07-19 18:35:44 +02:00
// Register status module
2021-06-01 10:51:35 +02:00
// @Summary Show Status of current API
// @Description Show version and status
2021-06-30 15:47:24 +02:00
// @Tags status
2021-06-01 10:51:35 +02:00
// @Produce json
// @Success 200 {object} Status
// @Failure 400 {object} web.HTTPError
// @Failure 404 {object} web.HTTPError
// @Router /api/status [get]
func Register(r *gin.Engine, ws *web.Service) {
r.GET("/api/status", func(c *gin.Context) {
status := &Status{
Version: VERSION,
Up: UP(),
Extras: EXTRAS,
}
if !status.Up {
c.JSON(http.StatusInternalServerError, status)
return
}
c.JSON(http.StatusOK, status)
2021-06-01 10:51:35 +02:00
})
}