2022-05-13 13:38:19 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-08-02 18:45:09 +00:00
|
|
|
"code.1533b4dc0.de/prskr/nurse/check"
|
2022-05-13 13:38:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ http.Handler = (*CheckHandler)(nil)
|
|
|
|
|
|
|
|
type CheckHandler struct {
|
2022-08-07 10:22:21 +00:00
|
|
|
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) {
|
2022-08-07 10:22:21 +00:00
|
|
|
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
|
|
|
|
}
|