refactor(api): clarify monitoring route guards

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-05 02:37:56 +00:00
parent 30ff013158
commit e518f2df32

View file

@ -1,7 +1,7 @@
// Package api mounts the monitoring endpoints on an HTTP mux. // Package api mounts the monitoring endpoints on an HTTP mux.
// //
// mux := http.NewServeMux() // mux := http.NewServeMux()
// api.RegisterRoutes(mux, p) // api.RegisterRoutes(mux, proxyInstance)
package api package api
import ( import (
@ -14,13 +14,13 @@ import (
// RouteRegistrar accepts HTTP handler registrations. // RouteRegistrar accepts HTTP handler registrations.
// //
// mux := http.NewServeMux() // mux := http.NewServeMux()
// api.RegisterRoutes(mux, p) // api.RegisterRoutes(mux, proxyInstance)
type RouteRegistrar interface { type RouteRegistrar interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
} }
// mux := http.NewServeMux() // mux := http.NewServeMux()
// api.RegisterRoutes(mux, p) // api.RegisterRoutes(mux, proxyInstance)
// _ = mux // _ = mux
// //
// The mounted routes are GET /1/summary, /1/workers, and /1/miners. // The mounted routes are GET /1/summary, /1/workers, and /1/miners.
@ -33,9 +33,9 @@ func RegisterRoutes(router RouteRegistrar, p *proxy.Proxy) {
registerJSONGetRoute(router, p, "/1/miners", func() any { return p.MinersDocument() }) registerJSONGetRoute(router, p, "/1/miners", func() any { return p.MinersDocument() })
} }
func registerJSONGetRoute(router RouteRegistrar, authoriser *proxy.Proxy, pattern string, renderDocument func() any) { func registerJSONGetRoute(router RouteRegistrar, proxyInstance *proxy.Proxy, pattern string, renderDocument func() any) {
router.HandleFunc(pattern, func(w http.ResponseWriter, request *http.Request) { router.HandleFunc(pattern, func(w http.ResponseWriter, request *http.Request) {
if status, ok := allowMonitoringRequest(authoriser, request); !ok { if status, ok := allowMonitoringRequest(proxyInstance, request); !ok {
switch status { switch status {
case http.StatusMethodNotAllowed: case http.StatusMethodNotAllowed:
w.Header().Set("Allow", http.MethodGet) w.Header().Set("Allow", http.MethodGet)
@ -49,14 +49,14 @@ func registerJSONGetRoute(router RouteRegistrar, authoriser *proxy.Proxy, patter
}) })
} }
func allowMonitoringRequest(authoriser *proxy.Proxy, request *http.Request) (int, bool) { func allowMonitoringRequest(proxyInstance *proxy.Proxy, request *http.Request) (int, bool) {
if proxyInstance == nil {
return http.StatusServiceUnavailable, false
}
if request.Method != http.MethodGet { if request.Method != http.MethodGet {
return http.StatusMethodNotAllowed, false return http.StatusMethodNotAllowed, false
} }
if authoriser == nil { return proxyInstance.AllowMonitoringRequest(request)
return http.StatusServiceUnavailable, false
}
return authoriser.AllowMonitoringRequest(request)
} }
func writeJSON(w http.ResponseWriter, payload any) { func writeJSON(w http.ResponseWriter, payload any) {