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

53 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"`
}
// @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 init() {
web.ModuleRegister(func(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)
})
})
}