aboutsummaryrefslogtreecommitdiff
path: root/middleware/health
diff options
context:
space:
mode:
Diffstat (limited to 'middleware/health')
-rw-r--r--middleware/health/health.go23
-rw-r--r--middleware/health/health_test.go8
-rw-r--r--middleware/health/setup.go6
3 files changed, 19 insertions, 18 deletions
diff --git a/middleware/health/health.go b/middleware/health/health.go
index 1d47e409e..513791688 100644
--- a/middleware/health/health.go
+++ b/middleware/health/health.go
@@ -10,32 +10,33 @@ import (
var once sync.Once
-type Health struct {
+type health struct {
Addr string
ln net.Listener
mux *http.ServeMux
}
-func health(w http.ResponseWriter, r *http.Request) {
- io.WriteString(w, ok)
-}
-
-func (h *Health) Startup() error {
+func (h *health) Startup() error {
if h.Addr == "" {
h.Addr = defAddr
}
once.Do(func() {
- if ln, err := net.Listen("tcp", h.Addr); err != nil {
+ ln, err := net.Listen("tcp", h.Addr)
+ if err != nil {
log.Printf("[ERROR] Failed to start health handler: %s", err)
return
- } else {
- h.ln = ln
}
+
+ h.ln = ln
+
h.mux = http.NewServeMux()
- h.mux.HandleFunc(path, health)
+ h.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+ io.WriteString(w, ok)
+ })
+
go func() {
http.Serve(h.ln, h.mux)
}()
@@ -43,7 +44,7 @@ func (h *Health) Startup() error {
return nil
}
-func (h *Health) Shutdown() error {
+func (h *health) Shutdown() error {
if h.ln != nil {
return h.ln.Close()
}
diff --git a/middleware/health/health_test.go b/middleware/health/health_test.go
index c5ef9cc42..de95eb103 100644
--- a/middleware/health/health_test.go
+++ b/middleware/health/health_test.go
@@ -10,14 +10,14 @@ import (
func TestHealth(t *testing.T) {
// We use a random port instead of a fixed port like 8080 that may have been
// occupied by some other process.
- health := Health{Addr: ":0"}
- if err := health.Startup(); err != nil {
+ h := health{Addr: ":0"}
+ if err := h.Startup(); err != nil {
t.Fatalf("Unable to startup the health server: %v", err)
}
- defer health.Shutdown()
+ defer h.Shutdown()
// Reconstruct the http address based on the port allocated by operating system.
- address := fmt.Sprintf("http://%s%s", health.ln.Addr().String(), path)
+ address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), path)
response, err := http.Get(address)
if err != nil {
diff --git a/middleware/health/setup.go b/middleware/health/setup.go
index ca8c6684f..a396d2902 100644
--- a/middleware/health/setup.go
+++ b/middleware/health/setup.go
@@ -19,9 +19,9 @@ func setup(c *caddy.Controller) error {
return middleware.Error("health", err)
}
- health := &Health{Addr: addr}
- c.OnStartup(health.Startup)
- c.OnShutdown(health.Shutdown)
+ h := &health{Addr: addr}
+ c.OnStartup(h.Startup)
+ c.OnShutdown(h.Shutdown)
// Don't do AddMiddleware, as health is not *really* a middleware just a separate
// webserver running.