Public source validation / validate (push) Failing after 3m8s
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package hostapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/itworx/pulse/internal/auth"
|
|
"github.com/itworx/pulse/internal/host"
|
|
"github.com/itworx/pulse/internal/problem"
|
|
)
|
|
|
|
type Handler struct{ Provider host.Provider }
|
|
|
|
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/host" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if _, ok := auth.PrincipalFromContext(r.Context()); !ok {
|
|
problem.Write(w, r, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required", "Authentication is required to read host telemetry.", nil)
|
|
return
|
|
}
|
|
if err := r.Context().Err(); err != nil {
|
|
return
|
|
}
|
|
var snapshot host.Snapshot
|
|
var err error
|
|
if h.Provider == nil {
|
|
snapshot = host.UnknownSnapshot(time.Now().UTC(), "host", "agent", "source_unavailable")
|
|
} else {
|
|
snapshot, err = h.Provider.Snapshot(r.Context())
|
|
}
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.Is(r.Context().Err(), context.Canceled) {
|
|
return
|
|
}
|
|
problem.Write(w, r, http.StatusServiceUnavailable, "HOST_UNAVAILABLE", "Hosttelemetrie niet beschikbaar", "De hosttelemetrie kon niet worden gelezen.", nil)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Cache-Control", "private, max-age=5")
|
|
_ = json.NewEncoder(w).Encode(snapshot)
|
|
}
|