diff --git a/internal/db/db.go b/internal/db/db.go index 99573c6..7d8254f 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -80,3 +80,12 @@ func IsUniqueConstraintViolation(err error) bool { } return false } + +func Ping() error { + sql, err := db.DB() + if err != nil { + return err + } + + return sql.Ping() +} diff --git a/internal/web/healthcheck.go b/internal/web/healthcheck.go new file mode 100644 index 0000000..e7c2058 --- /dev/null +++ b/internal/web/healthcheck.go @@ -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), + }) +} diff --git a/internal/web/server.go b/internal/web/server.go index c5989c6..0c563c1 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -208,6 +208,8 @@ func NewServer(isDev bool) *Server { g1.GET("/", create, logged) g1.POST("/", processCreate, logged) + g1.GET("/healthcheck", healthcheck) + g1.GET("/register", register) g1.POST("/register", processRegister) g1.GET("/login", login) diff --git a/internal/web/util.go b/internal/web/util.go index d558ab4..4fbcfed 100644 --- a/internal/web/util.go +++ b/internal/web/util.go @@ -168,7 +168,7 @@ func validateReservedKeywords(fl validator.FieldLevel) bool { name := fl.Field().String() 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{}{} }