use mux patterns to check method

This commit is contained in:
Fran Bull 2025-03-17 08:59:14 -07:00
parent a0c39ea016
commit 80d3119965
2 changed files with 6 additions and 14 deletions

View File

@ -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

View File

@ -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)