aboutsummaryrefslogtreecommitdiff
path: root/middleware/health/health.go
blob: 8c1665e5702d288229242ca83f8b129d8c9ca256 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package health

import (
	"io"
	"log"
	"net/http"
	"sync"
)

var once sync.Once

type Health struct {
	Addr string
}

func health(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, ok)
}

func (h Health) ListenAndServe() error {
	if h.Addr == "" {
		h.Addr = defAddr
	}
	once.Do(func() {
		http.HandleFunc("/health", health)
		go func() {
			if err := http.ListenAndServe(h.Addr, nil); err != nil {
				log.Printf("[ERROR] Failed to start health handler: %s", err)
			}
		}()
	})
	return nil
}

const (
	ok      = "OK"
	defAddr = ":8080"
)