2026-04-05 01:30:08 +00:00
|
|
|
// Package api mounts the monitoring endpoints on an HTTP mux.
|
2026-04-04 11:16:28 +01:00
|
|
|
//
|
2026-04-05 00:15:01 +00:00
|
|
|
// mux := http.NewServeMux()
|
2026-04-05 02:37:56 +00:00
|
|
|
// api.RegisterRoutes(mux, proxyInstance)
|
2026-04-04 11:16:28 +01:00
|
|
|
package api
|
|
|
|
|
|
2026-04-04 10:29:02 +00:00
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2026-04-04 16:10:33 +01:00
|
|
|
"dappco.re/go/proxy"
|
2026-04-04 10:29:02 +00:00
|
|
|
)
|
|
|
|
|
|
2026-04-05 01:15:03 +00:00
|
|
|
// RouteRegistrar accepts HTTP handler registrations.
|
|
|
|
|
//
|
|
|
|
|
// mux := http.NewServeMux()
|
2026-04-05 02:37:56 +00:00
|
|
|
// api.RegisterRoutes(mux, proxyInstance)
|
2026-04-05 01:15:03 +00:00
|
|
|
type RouteRegistrar interface {
|
2026-04-04 10:29:02 +00:00
|
|
|
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:48:33 +00:00
|
|
|
// mux := http.NewServeMux()
|
2026-04-05 02:37:56 +00:00
|
|
|
// api.RegisterRoutes(mux, proxyInstance)
|
2026-04-05 02:09:46 +00:00
|
|
|
// _ = mux
|
|
|
|
|
//
|
2026-04-05 02:01:38 +00:00
|
|
|
// The mounted routes are GET /1/summary, /1/workers, and /1/miners.
|
2026-04-05 01:15:03 +00:00
|
|
|
func RegisterRoutes(router RouteRegistrar, p *proxy.Proxy) {
|
2026-04-04 23:23:13 +00:00
|
|
|
if router == nil || p == nil {
|
2026-04-04 10:29:02 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-04-05 03:30:40 +00:00
|
|
|
registerJSONGetRoute(router, p, proxy.MonitoringRouteSummary, func() any { return p.SummaryDocument() })
|
|
|
|
|
registerJSONGetRoute(router, p, proxy.MonitoringRouteWorkers, func() any { return p.WorkersDocument() })
|
|
|
|
|
registerJSONGetRoute(router, p, proxy.MonitoringRouteMiners, func() any { return p.MinersDocument() })
|
2026-04-04 10:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 02:37:56 +00:00
|
|
|
func registerJSONGetRoute(router RouteRegistrar, proxyInstance *proxy.Proxy, pattern string, renderDocument func() any) {
|
2026-04-04 23:23:13 +00:00
|
|
|
router.HandleFunc(pattern, func(w http.ResponseWriter, request *http.Request) {
|
2026-04-05 02:37:56 +00:00
|
|
|
if status, ok := allowMonitoringRequest(proxyInstance, request); !ok {
|
2026-04-05 00:49:26 +00:00
|
|
|
switch status {
|
|
|
|
|
case http.StatusMethodNotAllowed:
|
|
|
|
|
w.Header().Set("Allow", http.MethodGet)
|
|
|
|
|
case http.StatusUnauthorized:
|
|
|
|
|
w.Header().Set("WWW-Authenticate", "Bearer")
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(status)
|
2026-04-04 23:23:13 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-04-05 00:15:01 +00:00
|
|
|
writeJSON(w, renderDocument())
|
2026-04-04 23:23:13 +00:00
|
|
|
})
|
2026-04-04 10:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 02:37:56 +00:00
|
|
|
func allowMonitoringRequest(proxyInstance *proxy.Proxy, request *http.Request) (int, bool) {
|
|
|
|
|
if proxyInstance == nil {
|
|
|
|
|
return http.StatusServiceUnavailable, false
|
|
|
|
|
}
|
|
|
|
|
return proxyInstance.AllowMonitoringRequest(request)
|
2026-04-05 00:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 10:29:02 +00:00
|
|
|
func writeJSON(w http.ResponseWriter, payload any) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
_ = json.NewEncoder(w).Encode(payload)
|
|
|
|
|
}
|