mirror of
https://github.com/thomiceli/opengist.git
synced 2024-12-22 20:42:40 +00:00
Add healthcheck endpoint (#170)
This commit is contained in:
parent
246f12c8cb
commit
47869a77c9
4 changed files with 36 additions and 1 deletions
|
@ -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()
|
||||
}
|
||||
|
|
24
internal/web/healthcheck.go
Normal file
24
internal/web/healthcheck.go
Normal 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),
|
||||
})
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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{}{}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue