This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package systemstatusapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
"github.com/itworx/pulse/internal/systemstatus"
|
||||
"net/http"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Snapshot func(context.Context) (systemstatus.Snapshot, error)
|
||||
Diagnostics func(context.Context) (Diagnostics, error)
|
||||
}
|
||||
|
||||
type Diagnostics struct {
|
||||
Status systemstatus.Snapshot `json:"status"`
|
||||
Config ConfigSummary `json:"config"`
|
||||
Runtime RuntimeSummary `json:"runtime"`
|
||||
Metrics string `json:"metrics"`
|
||||
}
|
||||
|
||||
type ConfigSummary struct {
|
||||
Environment string `json:"environment"`
|
||||
Timezone string `json:"timezone"`
|
||||
Locale string `json:"locale"`
|
||||
AuthMode string `json:"authMode"`
|
||||
PublicURLConfigured bool `json:"publicUrlConfigured"`
|
||||
DatabaseConfigured bool `json:"databaseConfigured"`
|
||||
PrometheusConfigured bool `json:"prometheusConfigured"`
|
||||
UnraidConfigured bool `json:"unraidConfigured"`
|
||||
OIDCConfigured bool `json:"oidcConfigured"`
|
||||
}
|
||||
|
||||
type RuntimeSummary struct {
|
||||
Goroutines int `json:"goroutines"`
|
||||
GoVersion string `json:"goVersion"`
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
problem.Write(w, r, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Method not allowed", "Only GET is supported.", nil)
|
||||
return
|
||||
}
|
||||
var value any
|
||||
var err error
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/system/status":
|
||||
if h.Snapshot == nil {
|
||||
err = http.ErrServerClosed
|
||||
} else {
|
||||
value, err = h.Snapshot(r.Context())
|
||||
}
|
||||
case "/api/v1/system/diagnostics":
|
||||
if h.Diagnostics == nil {
|
||||
err = http.ErrServerClosed
|
||||
} else {
|
||||
value, err = h.Diagnostics(r.Context())
|
||||
}
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
problem.Write(w, r, http.StatusServiceUnavailable, "STATUS_UNAVAILABLE", "Status unavailable", "The system status could not be read.", nil)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func Runtime() RuntimeSummary {
|
||||
return RuntimeSummary{Goroutines: runtime.NumGoroutine(), GoVersion: runtime.Version()}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package systemstatusapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/itworx/pulse/internal/systemstatus"
|
||||
)
|
||||
|
||||
func TestHandlerServesStatusAndRedactedDiagnostics(t *testing.T) {
|
||||
snapshot := systemstatus.Snapshot{Version: "test", OverallState: systemstatus.StateUnknown}
|
||||
handler := Handler{
|
||||
Snapshot: func(context.Context) (systemstatus.Snapshot, error) { return snapshot, nil },
|
||||
Diagnostics: func(context.Context) (Diagnostics, error) {
|
||||
return Diagnostics{Status: snapshot, Config: ConfigSummary{DatabaseConfigured: true}, Metrics: "pulse_up 1\n"}, nil
|
||||
},
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/system/status", nil))
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"overallState":"unknown"`) {
|
||||
t.Fatalf("status response = %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
response = httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/system/diagnostics", nil))
|
||||
if response.Code != http.StatusOK || strings.Contains(response.Body.String(), "password") {
|
||||
t.Fatalf("diagnostics response = %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRejectsUnsupportedMethodAndPath(t *testing.T) {
|
||||
handler := Handler{Snapshot: func(context.Context) (systemstatus.Snapshot, error) { return systemstatus.Snapshot{}, nil }}
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, httptest.NewRequest(http.MethodPost, "/api/v1/system/status", nil))
|
||||
if response.Code != http.StatusMethodNotAllowed {
|
||||
t.Fatalf("method status = %d", response.Code)
|
||||
}
|
||||
response = httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/system/unknown", nil))
|
||||
if response.Code != http.StatusNotFound {
|
||||
t.Fatalf("path status = %d", response.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user