From 80d3119965e165e21ef12f5960dfddcacbe6b940 Mon Sep 17 00:00:00 2001 From: Fran Bull Date: Mon, 17 Mar 2025 08:59:14 -0700 Subject: [PATCH] use mux patterns to check method --- tsconsensus/http.go | 12 ++---------- tsconsensus/monitor.go | 8 ++++---- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/tsconsensus/http.go b/tsconsensus/http.go index 709bcb86b..d4715612c 100644 --- a/tsconsensus/http.go +++ b/tsconsensus/http.go @@ -113,11 +113,7 @@ func (h authedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (c *Consensus) makeCommandMux() *http.ServeMux { mux := http.NewServeMux() - mux.HandleFunc("/join", func(w http.ResponseWriter, r *http.Request) { - if r.Method != httpm.POST { - http.Error(w, "Method must be POST", http.StatusMethodNotAllowed) - return - } + mux.HandleFunc("POST /join", func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1024*1024)) var jr joinRequest @@ -146,11 +142,7 @@ func (c *Consensus) makeCommandMux() *http.ServeMux { return } }) - mux.HandleFunc("/executeCommand", func(w http.ResponseWriter, r *http.Request) { - if r.Method != httpm.POST { - http.Error(w, "Bad Request", http.StatusBadRequest) - return - } + mux.HandleFunc("POST /executeCommand", func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() decoder := json.NewDecoder(r.Body) var cmd Command diff --git a/tsconsensus/monitor.go b/tsconsensus/monitor.go index e1644348a..a475b1e71 100644 --- a/tsconsensus/monitor.go +++ b/tsconsensus/monitor.go @@ -47,10 +47,10 @@ func serveMonitor(c *Consensus, ts *tsnet.Server, listenAddr string) (*http.Serv } m := &monitor{con: c, ts: ts} mux := http.NewServeMux() - mux.HandleFunc("/full", m.handleFullStatus) - mux.HandleFunc("/", m.handleSummaryStatus) - mux.HandleFunc("/netmap", m.handleNetmap) - mux.HandleFunc("/dial", m.handleDial) + mux.HandleFunc("GET /full", m.handleFullStatus) + mux.HandleFunc("GET /{$}", m.handleSummaryStatus) + mux.HandleFunc("GET /netmap", m.handleNetmap) + mux.HandleFunc("POST /dial", m.handleDial) srv := &http.Server{Handler: mux} go func() { err := srv.Serve(ln)