refactor(api): clarify route registration usage
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
bbdff60580
commit
460aae14fb
1 changed files with 10 additions and 9 deletions
|
|
@ -1,7 +1,7 @@
|
|||
// Package api implements the HTTP monitoring endpoints for the proxy.
|
||||
//
|
||||
// mux := http.NewServeMux()
|
||||
// RegisterRoutes(mux, p)
|
||||
// mux := http.NewServeMux()
|
||||
// api.RegisterRoutes(mux, p)
|
||||
package api
|
||||
|
||||
import (
|
||||
|
|
@ -11,29 +11,30 @@ import (
|
|||
"dappco.re/go/proxy"
|
||||
)
|
||||
|
||||
// http.NewServeMux()
|
||||
// mux := http.NewServeMux()
|
||||
type Router interface {
|
||||
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
|
||||
}
|
||||
|
||||
// mux := http.NewServeMux()
|
||||
// RegisterRoutes(mux, p)
|
||||
// api.RegisterRoutes(mux, p)
|
||||
func RegisterRoutes(router Router, p *proxy.Proxy) {
|
||||
if router == nil || p == nil {
|
||||
return
|
||||
}
|
||||
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() })
|
||||
registerJSONGetRoute(router, "/1/summary", func() any { return p.SummaryDocument() })
|
||||
registerJSONGetRoute(router, "/1/workers", func() any { return p.WorkersDocument() })
|
||||
registerJSONGetRoute(router, "/1/miners", func() any { return p.MinersDocument() })
|
||||
}
|
||||
|
||||
func registerGETRoute(router Router, pattern string, document func() any) {
|
||||
func registerJSONGetRoute(router Router, pattern string, renderDocument func() any) {
|
||||
router.HandleFunc(pattern, func(w http.ResponseWriter, request *http.Request) {
|
||||
if request.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
writeJSON(w, document())
|
||||
writeJSON(w, renderDocument())
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue