Files
ITWorx Pulse release export bd774932d5
Public source validation / validate (push) Failing after 3m8s
Publish ITWorx Pulse source
2026-09-03 02:09:19 +02:00

53 lines
1.5 KiB
Go

package arrayapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"time"
"github.com/itworx/pulse/internal/array"
"github.com/itworx/pulse/internal/auth"
"github.com/itworx/pulse/internal/problem"
)
type Handler struct{ Provider array.Provider }
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/array" {
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 array status.", nil)
return
}
if err := r.Context().Err(); err != nil {
return
}
var snapshot array.Snapshot
var err error
if h.Provider == nil {
snapshot = array.UnknownSnapshot(time.Now().UTC(), "array", "unraid", "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, "ARRAY_UNAVAILABLE", "Array status unavailable", "De arraystatus kon niet worden gelezen.", nil)
return
}
if snapshot.Members == nil {
snapshot.Members = []array.Member{}
}
if snapshot.History == nil {
snapshot.History = []array.Check{}
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "private, max-age=5")
_ = json.NewEncoder(w).Encode(snapshot)
}