nurse/api/check_handler.go

31 lines
606 B
Go
Raw Normal View History

2022-05-13 13:38:19 +00:00
package api
import (
"net/http"
"time"
2022-09-22 09:46:36 +00:00
"code.icb4dc0.de/prskr/nurse/check"
2022-05-13 13:38:19 +00:00
)
var _ http.Handler = (*CheckHandler)(nil)
type CheckHandler struct {
Timeout time.Duration
Attempts uint
Check check.SystemChecker
2022-05-13 13:38:19 +00:00
}
func (c CheckHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
ctx, cancel := check.AttemptsContext(request.Context(), c.Attempts, c.Timeout)
defer cancel()
2022-05-13 13:38:19 +00:00
if err := c.Check.Execute(ctx); err != nil {
writer.WriteHeader(http.StatusServiceUnavailable)
_, _ = writer.Write([]byte(err.Error()))
return
}
2022-06-09 20:40:32 +00:00
writer.WriteHeader(http.StatusOK)
2022-05-13 13:38:19 +00:00
return
}