From 7bbf733d98f21e76bfa2118983a3f6d080fa275b Mon Sep 17 00:00:00 2001 From: Maximilian Kalus Date: Tue, 26 Nov 2019 13:27:20 +0100 Subject: [PATCH] Add /health endpoint for health checks. Can be enabled using --enable-health. --- README.md | 2 ++ main.go | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index ad9a112..8d47120 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ Usage of /goStatic: Define the user (default "gopher") -enable-basic-auth Enable basic auth. By default, password are randomly generated. Use --set-basic-auth to set it. + -enable-health + Enable health check endpoint. You can call /health to get a 200 response. Useful for Kubernetes, OpenFaas, etc. -fallback string Default fallback file. Either absolute for a specific asset (/index.html), or relative to recursively resolve (index.html). -password-length int diff --git a/main.go b/main.go index 2c4698c..11a9a66 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ package main import ( "flag" + "fmt" "log" "net/http" "strconv" @@ -19,6 +20,7 @@ var ( fallbackPath = flag.String("fallback", "", "Default fallback file. Either absolute for a specific asset (/index.html), or relative to recursively resolve (index.html)") headerFlag = flag.String("append-header", "", "HTTP response header, specified as `HeaderName:Value` that should be added to all responses.") basicAuth = flag.Bool("enable-basic-auth", false, "Enable basic auth. By default, password are randomly generated. Use --set-basic-auth to set it.") + healthCheck = flag.Bool("enable-health", false, "Enable health check endpoint. You can call /health to get a 200 response. Useful for Kubernetes, OpenFaas, etc.") setBasicAuth = flag.String("set-basic-auth", "", "Define the basic auth. Form must be user:password") defaultUsernameBasicAuth = flag.String("default-user-basic-auth", "gopher", "Define the user") sizeRandom = flag.Int("password-length", 16, "Size of the randomized password") @@ -90,6 +92,12 @@ func main() { } } + if *healthCheck { + http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Ok") + }) + } + http.Handle(pathPrefix, handler) log.Printf("Listening at 0.0.0.0%v %v...", port, pathPrefix)