diff --git a/uncanny/server.go b/uncanny/server.go index db0f8bf..bc1035d 100644 --- a/uncanny/server.go +++ b/uncanny/server.go @@ -30,13 +30,13 @@ func NewServer(can *Can) *Server { func (s *Server) registerHandlers() { s.HandleFunc("/", http.NotFound) s.HandleFunc("/dispense/", func(rw http.ResponseWriter, r *http.Request) { - if matched, _ := path.Match("/dispense/[0-9]?[0-9]*", r.URL.Path); matched { + if matched, _ := path.Match("/dispense/[0-9]*", r.URL.Path); matched { slot, err := strconv.ParseUint(path.Base(r.URL.Path), 10, 32) if err != nil { log.Printf("Error decoding slot number: %v", err) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } else if slot < 0 || slot > maxSlot { - log.Printf("Invalid slot number: %u", slot) + log.Printf("Invalid slot number: %d", slot) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } else { err = s.can.Dispense(int(slot)) @@ -44,14 +44,15 @@ func (s *Server) registerHandlers() { log.Printf("Error sending dispense command: %v", err) rw.Header().Set("Content-Type", "text/plain") rw.WriteHeader(http.StatusServiceUnavailable) - fmt.Fprintf(rw, "%u error", slot) + fmt.Fprintf(rw, "%d error", slot) } else { rw.Header().Set("Content-Type", "text/plain") rw.WriteHeader(http.StatusOK) - fmt.Fprintf(rw, "%u dispense", slot) + fmt.Fprintf(rw, "%d dispense", slot) } } } else { + log.Printf("Mismatched path: %s", r.URL.Path) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } }) @@ -69,46 +70,48 @@ func (s *Server) registerHandlers() { } }) s.HandleFunc("/level/", func(rw http.ResponseWriter, r *http.Request) { - if matched, _ := path.Match("/level/[0-9]?[0-9]*", r.URL.Path); matched { + if matched, _ := path.Match("/level/[0-9]*", r.URL.Path); matched { slot, err := strconv.ParseUint(path.Base(r.URL.Path), 10, 32) if err != nil { log.Printf("Error decoding slot number: %v", err) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } else if slot < 0 || slot > maxSlot { - log.Printf("Invalid slot number: %u", slot) + log.Printf("Invalid slot number: %d", slot) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } else { rw.Header().Set("Content-Type", "text/plain") rw.WriteHeader(http.StatusOK) if s.can.IsEmpty(int(slot)) { - fmt.Fprintf(rw, "%u empty", slot) + fmt.Fprintf(rw, "%d empty", slot) } else { - fmt.Fprintf(rw, "%u full", slot) + fmt.Fprintf(rw, "%d full", slot) } } } else { + log.Printf("Mismatched path: %s", r.URL.Path) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } }) s.HandleFunc("/active/", func(rw http.ResponseWriter, r *http.Request) { - if matched, _ := path.Match("/active/[0-9]?[0-9]*", r.URL.Path); matched { + if matched, _ := path.Match("/active/[0-9]*", r.URL.Path); matched { slot, err := strconv.ParseUint(path.Base(r.URL.Path), 10, 32) if err != nil { log.Printf("Error decoding slot number: %v", err) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } else if slot < 0 || slot > maxSlot { - log.Printf("Invalid slot number: %u", slot) + log.Printf("Invalid slot number: %d", slot) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } else { rw.Header().Set("Content-Type", "text/plain") rw.WriteHeader(http.StatusOK) if s.can.IsDispensing(int(slot)) { - fmt.Fprintf(rw, "%u dispensing", slot) + fmt.Fprintf(rw, "%d dispensing", slot) } else { - fmt.Fprintf(rw, "%u off", slot) + fmt.Fprintf(rw, "%d off", slot) } } } else { + log.Printf("Mismatched path: %s", r.URL.Path) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) } })