healthz.go 548 B

1234567891011121314151617181920212223242526272829303132
  1. package api
  2. import (
  3. "net/http"
  4. "sync/atomic"
  5. "github.com/ncarlier/webhookd/pkg/config"
  6. )
  7. var (
  8. healthy int32
  9. )
  10. // Shutdown set API as stopped
  11. func Shutdown() {
  12. atomic.StoreInt32(&healthy, 0)
  13. }
  14. // Start set API as started
  15. func Start() {
  16. atomic.StoreInt32(&healthy, 1)
  17. }
  18. func healthz(conf *config.Config) http.Handler {
  19. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. if atomic.LoadInt32(&healthy) == 1 {
  21. w.WriteHeader(http.StatusNoContent)
  22. return
  23. }
  24. w.WriteHeader(http.StatusServiceUnavailable)
  25. })
  26. }