refactor(proxy): clarify monitoring HTTP helpers

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 23:42:26 +00:00
parent fb5453c097
commit fd76640d69
2 changed files with 16 additions and 16 deletions

View file

@ -17,7 +17,7 @@ func TestProxy_allowHTTP_Good(t *testing.T) {
},
}
status, ok := p.allowHTTP(&http.Request{
status, ok := p.allowMonitoringRequest(&http.Request{
Method: http.MethodGet,
Header: http.Header{
"Authorization": []string{"Bearer secret"},
@ -40,7 +40,7 @@ func TestProxy_allowHTTP_Bad(t *testing.T) {
},
}
status, ok := p.allowHTTP(&http.Request{Method: http.MethodPost})
status, ok := p.allowMonitoringRequest(&http.Request{Method: http.MethodPost})
if ok {
t.Fatal("expected non-GET request to be rejected")
}
@ -56,7 +56,7 @@ func TestProxy_allowHTTP_MethodRestricted_Bad(t *testing.T) {
},
}
status, ok := p.allowHTTP(&http.Request{Method: http.MethodPost})
status, ok := p.allowMonitoringRequest(&http.Request{Method: http.MethodPost})
if ok {
t.Fatal("expected non-GET request to be rejected")
}
@ -74,7 +74,7 @@ func TestProxy_allowHTTP_Ugly(t *testing.T) {
},
}
status, ok := p.allowHTTP(&http.Request{
status, ok := p.allowMonitoringRequest(&http.Request{
Method: http.MethodGet,
Header: http.Header{
"Authorization": []string{"Bearer wrong"},
@ -100,7 +100,7 @@ func TestProxy_startHTTP_Good(t *testing.T) {
done: make(chan struct{}),
}
if ok := p.startHTTP(); !ok {
if ok := p.startMonitoringServer(); !ok {
t.Fatal("expected HTTP server to start on a free port")
}
p.Stop()
@ -133,7 +133,7 @@ func TestProxy_startHTTP_Bad(t *testing.T) {
done: make(chan struct{}),
}
if ok := p.startHTTP(); ok {
if ok := p.startMonitoringServer(); ok {
t.Fatal("expected HTTP server start to fail when the port is already in use")
}
}

View file

@ -221,7 +221,7 @@ func (p *Proxy) Start() {
p.watcher.Start()
}
if p.config.HTTP.Enabled {
if !p.startHTTP() {
if !p.startMonitoringServer() {
p.Stop()
return
}
@ -589,40 +589,40 @@ func parseTLSVersion(value string) uint16 {
}
}
func (p *Proxy) startHTTP() bool {
func (p *Proxy) startMonitoringServer() bool {
if p == nil || !p.config.HTTP.Enabled {
return true
}
mux := http.NewServeMux()
mux.HandleFunc("/1/summary", func(w http.ResponseWriter, r *http.Request) {
if status, ok := p.allowHTTP(r); !ok {
if status, ok := p.allowMonitoringRequest(r); !ok {
if status == http.StatusUnauthorized {
w.Header().Set("WWW-Authenticate", "Bearer")
}
w.WriteHeader(status)
return
}
p.writeJSON(w, p.SummaryDocument())
p.writeJSONResponse(w, p.SummaryDocument())
})
mux.HandleFunc("/1/workers", func(w http.ResponseWriter, r *http.Request) {
if status, ok := p.allowHTTP(r); !ok {
if status, ok := p.allowMonitoringRequest(r); !ok {
if status == http.StatusUnauthorized {
w.Header().Set("WWW-Authenticate", "Bearer")
}
w.WriteHeader(status)
return
}
p.writeJSON(w, p.WorkersDocument())
p.writeJSONResponse(w, p.WorkersDocument())
})
mux.HandleFunc("/1/miners", func(w http.ResponseWriter, r *http.Request) {
if status, ok := p.allowHTTP(r); !ok {
if status, ok := p.allowMonitoringRequest(r); !ok {
if status == http.StatusUnauthorized {
w.Header().Set("WWW-Authenticate", "Bearer")
}
w.WriteHeader(status)
return
}
p.writeJSON(w, p.MinersDocument())
p.writeJSONResponse(w, p.MinersDocument())
})
addr := net.JoinHostPort(p.config.HTTP.Host, strconv.Itoa(int(p.config.HTTP.Port)))
listener, err := net.Listen("tcp", addr)
@ -639,7 +639,7 @@ func (p *Proxy) startHTTP() bool {
return true
}
func (p *Proxy) allowHTTP(r *http.Request) (int, bool) {
func (p *Proxy) allowMonitoringRequest(r *http.Request) (int, bool) {
if p == nil {
return http.StatusServiceUnavailable, false
}
@ -655,7 +655,7 @@ func (p *Proxy) allowHTTP(r *http.Request) (int, bool) {
return http.StatusOK, true
}
func (p *Proxy) writeJSON(w http.ResponseWriter, payload any) {
func (p *Proxy) writeJSONResponse(w http.ResponseWriter, payload any) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(payload)
}