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()}
|
||||
}
|
||||
Reference in New Issue
Block a user