mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
initial version of healthz endpoint
This commit is contained in:
parent
e8b7508b13
commit
b78dc396ac
2 changed files with 50 additions and 0 deletions
|
@ -163,3 +163,12 @@ func PingAMQP() error {
|
||||||
err := SendActionAMQP(a, "")
|
err := SendActionAMQP(a, "")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CheckConnection() error {
|
||||||
|
|
||||||
|
if client.connection.IsClosed() {
|
||||||
|
return fmt.Errorf("connection to broker is closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
41
routes/healthz/healthz_endpoint.go
Normal file
41
routes/healthz/healthz_endpoint.go
Normal 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{})
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue