initial version of healthz endpoint

This commit is contained in:
Sonja Happ 2019-11-12 13:36:55 +01:00
parent e8b7508b13
commit b78dc396ac
2 changed files with 50 additions and 0 deletions

View file

@ -163,3 +163,12 @@ func PingAMQP() error {
err := SendActionAMQP(a, "")
return err
}
func CheckConnection() error {
if client.connection.IsClosed() {
return fmt.Errorf("connection to broker is closed")
}
return nil
}

View file

@ -0,0 +1,41 @@
package healthz
import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/gin-gonic/gin"
"net/http"
)
func RegisterHealthzEndpoint(r *gin.RouterGroup) {
r.GET("", getHealth)
}
// getHealth godoc
// @Summary Get health status of backend
// @ID getHealth
// @Produce json
// @Tags healthz
// @Success 200 "Backend is healthy, database and AMQP broker connections are alive"
// @Failure 500 "Backend is NOT healthy"
// @Router /healthz [get]
func getHealth(c *gin.Context) {
// check if DB connection is active
db := database.GetDB()
err := db.DB().Ping()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": err})
}
// check if connection to AMQP broker is alive if backend was started with AMQP client
if len(database.AMQP_URL) != 0 {
err = amqp.CheckConnection()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": err})
}
}
c.JSON(http.StatusOK, gin.H{})
}