From fd76640d69d070edaab01d39907452ed373dcad4 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 23:42:26 +0000 Subject: [PATCH] refactor(proxy): clarify monitoring HTTP helpers Co-Authored-By: Virgil --- http_auth_test.go | 12 ++++++------ state_impl.go | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/http_auth_test.go b/http_auth_test.go index 034b089..76b69d6 100644 --- a/http_auth_test.go +++ b/http_auth_test.go @@ -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") } } diff --git a/state_impl.go b/state_impl.go index 1cc975f..c7c321d 100644 --- a/state_impl.go +++ b/state_impl.go @@ -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) }