From eb21357bbb184dc7bdff13632dfc6548d87a1a9b Mon Sep 17 00:00:00 2001 From: Alex Palaistras Date: Tue, 22 Oct 2024 19:55:19 +0100 Subject: [PATCH] service: Add health-check handler on `/_health` --- .gitignore | 8 ++++++++ pkg/service/service.go | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82073df --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Generic junk files. +.DS_Store +.idea +*.log +tmp/ + +# Local configuration. +config.toml diff --git a/pkg/service/service.go b/pkg/service/service.go index cd210db..36605d0 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -80,6 +80,11 @@ func (s *Service) Init(ctx context.Context) error { return fmt.Errorf("no gateway configuration found") } + // Set up request handlers. + if err := s.handler.Handle(s.handleHealth()); err != nil { + return fmt.Errorf("failed setting up request handler for health-checks: %w", err) + } + for _, g := range s.gateway { if err := g.Init(ctx); err != nil { return fmt.Errorf("failed initializing gateway: %w", err) @@ -136,3 +141,10 @@ func (s *Service) UnmarshalTOML(data any) error { return nil } + +// HandleHealth is an HTTP handler for health-checks. +func (s *Service) handleHealth() (string, http.HandlerFunc) { + return "/_health", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + } +}