Skip to content

Commit

Permalink
feat: add status page
Browse files Browse the repository at this point in the history
  • Loading branch information
lfleischmann authored Aug 14, 2023
1 parent 8fa9117 commit 1ba2ab1
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
1 change: 1 addition & 0 deletions backend/Dockerfile.debug
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
6 changes: 6 additions & 0 deletions backend/handler/admin_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand All @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions backend/handler/public_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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")
Expand Down
27 changes: 27 additions & 0 deletions backend/handler/status.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions backend/template/template.go
Original file line number Diff line number Diff line change
@@ -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")),
}
}
17 changes: 17 additions & 0 deletions backend/template/templates/status.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{define "status"}}
<!DOCTYPE html>
<html>
<head>
<title>API Integration Status</title>
</head>
<body>
<h1>API Status</h1>
{{if .dbError}}
<p>❌ API is not functioning properly due to database connectivity problems.</p>
{{else}}
<p>✅ API is running.</p>
<p>Check integration guides on <a href="https://docs.hanko.io" target="_blank">docs.hanko.io</a></p>
{{end}}
</body>
</html>
{{end}}
22 changes: 21 additions & 1 deletion docs/static/spec/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
22 changes: 21 additions & 1 deletion docs/static/spec/public.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 1ba2ab1

Please sign in to comment.