Skip to content

Commit

Permalink
Add healthcheck endpoint (thomiceli#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Jan 4, 2024
1 parent 246f12c commit 47869a7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 24 additions & 0 deletions internal/web/healthcheck.go
Original file line number Diff line number Diff line change
@@ -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),
})
}
2 changes: 2 additions & 0 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/web/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
}

Expand Down

0 comments on commit 47869a7

Please sign in to comment.