mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-06 20:10:11 +02:00
4a78989d9d
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly - only use sentinel errors when it has to be checked using `errors.Is` - replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error - exclude the sentinel error definition requirement from .golangci.yml - update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
45 lines
925 B
Go
45 lines
925 B
Go
package healthcheck
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"sync"
|
|
)
|
|
|
|
type handler struct {
|
|
healthErr error
|
|
healthErrMu sync.RWMutex
|
|
logger Logger
|
|
}
|
|
|
|
func newHandler(logger Logger) *handler {
|
|
return &handler{
|
|
healthErr: errors.New("healthcheck did not run yet"),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (h *handler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
|
|
if request.Method != http.MethodGet {
|
|
http.Error(responseWriter, "method not supported for healthcheck", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := h.getErr(); err != nil {
|
|
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
responseWriter.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func (h *handler) setErr(err error) {
|
|
h.healthErrMu.Lock()
|
|
defer h.healthErrMu.Unlock()
|
|
h.healthErr = err
|
|
}
|
|
|
|
func (h *handler) getErr() (err error) {
|
|
h.healthErrMu.RLock()
|
|
defer h.healthErrMu.RUnlock()
|
|
return h.healthErr
|
|
}
|