2026-04-04 11:16:28 +01:00
|
|
|
// Package api implements the HTTP monitoring endpoints for the proxy.
|
|
|
|
|
//
|
|
|
|
|
// Registered routes:
|
|
|
|
|
//
|
|
|
|
|
// GET /1/summary — aggregated proxy stats
|
|
|
|
|
// GET /1/workers — per-worker hashrate table
|
|
|
|
|
// GET /1/miners — per-connection state table
|
|
|
|
|
//
|
|
|
|
|
// proxyapi.RegisterRoutes(apiRouter, p)
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Router matches the standard http.ServeMux registration shape.
|
|
|
|
|
type Router interface {
|
|
|
|
|
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterRoutes wires the monitoring endpoints onto the supplied router.
|
2026-04-04 20:53:49 +00:00
|
|
|
//
|
2026-04-04 23:28:35 +00:00
|
|
|
// mux := http.NewServeMux()
|
2026-04-04 20:53:49 +00:00
|
|
|
// proxyapi.RegisterRoutes(mux, p)
|
|
|
|
|
// // GET /1/summary, /1/workers, and /1/miners are now live.
|
2026-04-04 23:23:13 +00:00
|
|
|
func RegisterRoutes(router Router, p *proxy.Proxy) {
|
|
|
|
|
if router == nil || p == nil {
|
2026-04-04 10:29:02 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-04-04 23:23:13 +00:00
|
|
|
registerGETRoute(router, "/1/summary", func() any { return p.SummaryDocument() })
|
|
|
|
|
registerGETRoute(router, "/1/workers", func() any { return p.WorkersDocument() })
|
|
|
|
|
registerGETRoute(router, "/1/miners", func() any { return p.MinersDocument() })
|
2026-04-04 10:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:23:13 +00:00
|
|
|
func registerGETRoute(router Router, pattern string, document func() any) {
|
|
|
|
|
router.HandleFunc(pattern, func(w http.ResponseWriter, request *http.Request) {
|
|
|
|
|
if request.Method != http.MethodGet {
|
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, document())
|
|
|
|
|
})
|
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)
|
|
|
|
|
}
|