Add healthcheck endpoint (#170)

This commit is contained in:
Thomas Miceli 2023-12-16 01:27:00 +01:00
parent 246f12c8cb
commit 47869a77c9
4 changed files with 36 additions and 1 deletions

View file

@ -80,3 +80,12 @@ func IsUniqueConstraintViolation(err error) bool {
} }
return false return false
} }
func Ping() error {
sql, err := db.DB()
if err != nil {
return err
}
return sql.Ping()
}

View file

@ -0,0 +1,24 @@
package web
import (
"github.com/labstack/echo/v4"
"github.com/thomiceli/opengist/internal/db"
"time"
)
func healthcheck(ctx echo.Context) error {
// Check database connection
dbOk := "ok"
httpStatus := 200
err := db.Ping()
if err != nil {
dbOk = "ko"
httpStatus = 503
}
return ctx.JSON(httpStatus, map[string]interface{}{
"database": dbOk,
"time": time.Now().Format(time.RFC3339),
})
}

View file

@ -208,6 +208,8 @@ func NewServer(isDev bool) *Server {
g1.GET("/", create, logged) g1.GET("/", create, logged)
g1.POST("/", processCreate, logged) g1.POST("/", processCreate, logged)
g1.GET("/healthcheck", healthcheck)
g1.GET("/register", register) g1.GET("/register", register)
g1.POST("/register", processRegister) g1.POST("/register", processRegister)
g1.GET("/login", login) g1.GET("/login", login)

View file

@ -168,7 +168,7 @@ func validateReservedKeywords(fl validator.FieldLevel) bool {
name := fl.Field().String() name := fl.Field().String()
restrictedNames := map[string]struct{}{} restrictedNames := map[string]struct{}{}
for _, restrictedName := range []string{"assets", "register", "login", "logout", "settings", "admin-panel", "all", "search", "init"} { for _, restrictedName := range []string{"assets", "register", "login", "logout", "settings", "admin-panel", "all", "search", "init", "healthcheck"} {
restrictedNames[restrictedName] = struct{}{} restrictedNames[restrictedName] = struct{}{}
} }