mirror of
https://github.com/coredns/coredns.git
synced 2025-12-07 10:55:17 -05:00
A health middleware
Start http handler on port 8080 and return OK. Also add some documentation fixes for the prometheus middleware.
This commit is contained in:
21
middleware/health/README.md
Normal file
21
middleware/health/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# health
|
||||
|
||||
This module enables a simple health check.
|
||||
|
||||
By default it will listen on port 9180.
|
||||
|
||||
Restarting CoreDNS will stop the health checking. This is a bug. Also [this upstream
|
||||
Caddy bug](https://github.com/mholt/caddy/issues/675).
|
||||
|
||||
## Syntax
|
||||
|
||||
~~~
|
||||
health
|
||||
~~~
|
||||
|
||||
It optionally takes an address, the default is `:8080`. The health path is fixed to `/health`. It
|
||||
will just return "OK", when CoreDNS is healthy.
|
||||
|
||||
This middleware only needs to be enabled once.
|
||||
|
||||
## Examples
|
||||
38
middleware/health/health.go
Normal file
38
middleware/health/health.go
Normal file
@@ -0,0 +1,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"
|
||||
)
|
||||
Reference in New Issue
Block a user