1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-19 22:19:23 +02:00
pocketbase/apis/health.go

54 lines
1.5 KiB
Go
Raw Normal View History

2022-12-11 16:27:46 +01:00
package apis
import (
2022-12-11 17:32:43 +02:00
"net/http"
2024-09-29 19:23:19 +03:00
"slices"
2022-12-11 17:32:43 +02:00
2022-12-11 16:27:46 +01:00
"github.com/pocketbase/pocketbase/core"
2024-09-29 19:23:19 +03:00
"github.com/pocketbase/pocketbase/tools/router"
2022-12-11 16:27:46 +01:00
)
// bindHealthApi registers the health api endpoint.
2024-09-29 19:23:19 +03:00
func bindHealthApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) {
2022-12-11 16:27:46 +01:00
subGroup := rg.Group("/health")
2024-09-29 19:23:19 +03:00
subGroup.GET("", healthCheck)
2023-05-13 22:10:14 +03:00
}
2022-12-11 16:27:46 +01:00
// healthCheck returns a 200 OK response if the server is healthy.
2024-09-29 19:23:19 +03:00
func healthCheck(e *core.RequestEvent) error {
resp := struct {
Message string `json:"message"`
Code int `json:"code"`
Data map[string]any `json:"data"`
}{
Code: http.StatusOK,
Message: "API is healthy.",
}
2024-09-29 19:23:19 +03:00
if e.HasSuperuserAuth() {
resp.Data = make(map[string]any, 3)
resp.Data["canBackup"] = !e.App.Store().Has(core.StoreKeyActiveBackup)
resp.Data["realIP"] = e.RealIP()
// loosely check if behind a reverse proxy
// (usually used in the dashboard to remind superusers in case deployed behind reverse-proxy)
possibleProxyHeader := ""
headersToCheck := append(
slices.Clone(e.App.Settings().TrustedProxy.Headers),
// common proxy headers
"CF-Connecting-IP", "Fly-Client-IP", "X‑Forwarded-For",
)
for _, header := range headersToCheck {
if e.Request.Header.Get(header) != "" {
possibleProxyHeader = header
break
}
}
resp.Data["possibleProxyHeader"] = possibleProxyHeader
} else {
resp.Data = map[string]any{} // ensure that it is returned as object
}
2022-12-11 17:32:43 +02:00
2024-09-29 19:23:19 +03:00
return e.JSON(http.StatusOK, resp)
2022-12-11 16:27:46 +01:00
}