Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add healthcheck endpoint #170

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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