From 1ba2ab1d891a1625a455984353125e70ebd5260d Mon Sep 17 00:00:00 2001 From: lfleischmann <67686424+lfleischmann@users.noreply.github.com> Date: Mon, 14 Aug 2023 10:17:23 +0200 Subject: [PATCH] feat: add status page --- backend/Dockerfile | 1 + backend/Dockerfile.debug | 1 + backend/handler/admin_router.go | 6 ++++++ backend/handler/public_router.go | 6 ++++++ backend/handler/status.go | 27 ++++++++++++++++++++++++++ backend/template/template.go | 25 ++++++++++++++++++++++++ backend/template/templates/status.tmpl | 17 ++++++++++++++++ docs/static/spec/admin.yaml | 22 ++++++++++++++++++++- docs/static/spec/public.yaml | 22 ++++++++++++++++++++- 9 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 backend/handler/status.go create mode 100644 backend/template/template.go create mode 100644 backend/template/templates/status.tmpl diff --git a/backend/Dockerfile b/backend/Dockerfile index 0edebe44c..4440c01be 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -25,6 +25,7 @@ COPY rate_limiter rate_limiter/ COPY thirdparty thirdparty/ COPY build_info build_info/ COPY middleware middleware/ +COPY template template/ # Build RUN go generate ./... diff --git a/backend/Dockerfile.debug b/backend/Dockerfile.debug index d8e48f7d5..c763c9d8d 100644 --- a/backend/Dockerfile.debug +++ b/backend/Dockerfile.debug @@ -26,6 +26,7 @@ COPY rate_limiter rate_limiter/ COPY thirdparty thirdparty/ COPY build_info build_info/ COPY middleware middleware/ +COPY template template/ # Build RUN go generate ./... diff --git a/backend/handler/admin_router.go b/backend/handler/admin_router.go index afb2e5cb6..21f611230 100644 --- a/backend/handler/admin_router.go +++ b/backend/handler/admin_router.go @@ -8,10 +8,12 @@ import ( "github.com/teamhanko/hanko/backend/dto" hankoMiddleware "github.com/teamhanko/hanko/backend/middleware" "github.com/teamhanko/hanko/backend/persistence" + "github.com/teamhanko/hanko/backend/template" ) func NewAdminRouter(cfg *config.Config, persister persistence.Persister, prometheus echo.MiddlewareFunc) *echo.Echo { e := echo.New() + e.Renderer = template.NewTemplateRenderer() e.HideBanner = true g := e.Group("") @@ -30,6 +32,10 @@ func NewAdminRouter(cfg *config.Config, persister persistence.Persister, prometh e.GET("/metrics", echoprometheus.NewHandler()) } + statusHandler := NewStatusHandler(persister) + + e.GET("/", statusHandler.Status) + healthHandler := NewHealthHandler() health := e.Group("/health") diff --git a/backend/handler/public_router.go b/backend/handler/public_router.go index b7df30461..fcc3ae379 100644 --- a/backend/handler/public_router.go +++ b/backend/handler/public_router.go @@ -13,10 +13,14 @@ import ( hankoMiddleware "github.com/teamhanko/hanko/backend/middleware" "github.com/teamhanko/hanko/backend/persistence" "github.com/teamhanko/hanko/backend/session" + "github.com/teamhanko/hanko/backend/template" ) func NewPublicRouter(cfg *config.Config, persister persistence.Persister, prometheus echo.MiddlewareFunc) *echo.Echo { e := echo.New() + + e.Renderer = template.NewTemplateRenderer() + e.HideBanner = true e.HTTPErrorHandler = dto.NewHTTPErrorHandler(dto.HTTPErrorHandlerConfig{Debug: false, Logger: e.Logger}) @@ -77,7 +81,9 @@ func NewPublicRouter(cfg *config.Config, persister persistence.Persister, promet } userHandler := NewUserHandler(cfg, persister, sessionManager, auditLogger) + statusHandler := NewStatusHandler(persister) + e.GET("/", statusHandler.Status) e.GET("/me", userHandler.Me, sessionMiddleware) user := e.Group("/users") diff --git a/backend/handler/status.go b/backend/handler/status.go new file mode 100644 index 000000000..d840bf2de --- /dev/null +++ b/backend/handler/status.go @@ -0,0 +1,27 @@ +package handler + +import ( + "github.com/labstack/echo/v4" + "github.com/teamhanko/hanko/backend/persistence" + "net/http" +) + +type StatusHandler struct { + persister persistence.Persister +} + +func NewStatusHandler(persister persistence.Persister) *StatusHandler { + return &StatusHandler{ + persister: persister, + } +} + +func (h *StatusHandler) Status(c echo.Context) error { + // random query to check DB connectivity + _, err := h.persister.GetJwkPersister().GetAll() + if err != nil { + return c.Render(http.StatusInternalServerError, "status", map[string]bool{"dbError": true}) + } + + return c.Render(http.StatusOK, "status", nil) +} diff --git a/backend/template/template.go b/backend/template/template.go new file mode 100644 index 000000000..56fa0fc99 --- /dev/null +++ b/backend/template/template.go @@ -0,0 +1,25 @@ +package template + +import ( + "embed" + "github.com/labstack/echo/v4" + "html/template" + "io" +) + +//go:embed templates/* +var templateFS embed.FS + +type Template struct { + templates *template.Template +} + +func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + return t.templates.ExecuteTemplate(w, name, data) +} + +func NewTemplateRenderer() *Template { + return &Template{ + templates: template.Must(template.ParseFS(templateFS, "templates/*.tmpl")), + } +} diff --git a/backend/template/templates/status.tmpl b/backend/template/templates/status.tmpl new file mode 100644 index 000000000..93926f882 --- /dev/null +++ b/backend/template/templates/status.tmpl @@ -0,0 +1,17 @@ +{{define "status"}} + + +
+❌ API is not functioning properly due to database connectivity problems.
+{{else}} +✅ API is running.
+Check integration guides on docs.hanko.io
+{{end}} + + +{{end}} diff --git a/docs/static/spec/admin.yaml b/docs/static/spec/admin.yaml index 2ceb24c72..3308123dd 100644 --- a/docs/static/spec/admin.yaml +++ b/docs/static/spec/admin.yaml @@ -22,8 +22,28 @@ externalDocs: description: More about Hanko url: https://github.com/teamhanko/hanko servers: - - url: 'localhost:8000' + - url: 'localhost:8001' paths: + /: + get: + summary: Status page + description: Return information about the API status. Returns a 500 if there are issues with database connectivity. + operationId: status + tags: + - Status + responses: + '200': + description: 'API is running' + content: + text/html: + schema: + type: string + '500': + description: 'API is not functioning properly' + content: + text/html: + schema: + type: string /users: get: summary: 'Get a list of users' diff --git a/docs/static/spec/public.yaml b/docs/static/spec/public.yaml index e9adc5ef3..acf985ca4 100644 --- a/docs/static/spec/public.yaml +++ b/docs/static/spec/public.yaml @@ -52,8 +52,28 @@ externalDocs: description: More about Hanko url: https://github.com/teamhanko/hanko servers: - - url: 'localhost:8001' + - url: 'localhost:8000' paths: + /: + get: + summary: Status page + description: Return information about the API status. Returns a 500 if there are issues with database connectivity. + operationId: status + tags: + - Status + responses: + '200': + description: 'API is running' + content: + text/html: + schema: + type: string + '500': + description: 'API is not functioning properly' + content: + text/html: + schema: + type: string /passcode/login/initialize: post: summary: 'Initialize passcode login'