Fix path matching and debug messages

This commit is contained in:
Gregor Riepl 2021-01-31 18:05:33 +01:00
parent c0a0e7714d
commit 191deb7db3

View file

@ -30,13 +30,13 @@ func NewServer(can *Can) *Server {
func (s *Server) registerHandlers() { func (s *Server) registerHandlers() {
s.HandleFunc("/", http.NotFound) s.HandleFunc("/", http.NotFound)
s.HandleFunc("/dispense/", func(rw http.ResponseWriter, r *http.Request) { 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) slot, err := strconv.ParseUint(path.Base(r.URL.Path), 10, 32)
if err != nil { if err != nil {
log.Printf("Error decoding slot number: %v", err) log.Printf("Error decoding slot number: %v", err)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else if slot < 0 || slot > maxSlot { } 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) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else { } else {
err = s.can.Dispense(int(slot)) err = s.can.Dispense(int(slot))
@ -44,14 +44,15 @@ func (s *Server) registerHandlers() {
log.Printf("Error sending dispense command: %v", err) log.Printf("Error sending dispense command: %v", err)
rw.Header().Set("Content-Type", "text/plain") rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusServiceUnavailable) rw.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(rw, "%u error", slot) fmt.Fprintf(rw, "%d error", slot)
} else { } else {
rw.Header().Set("Content-Type", "text/plain") rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusOK)
fmt.Fprintf(rw, "%u dispense", slot) fmt.Fprintf(rw, "%d dispense", slot)
} }
} }
} else { } else {
log.Printf("Mismatched path: %s", r.URL.Path)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) 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) { 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) slot, err := strconv.ParseUint(path.Base(r.URL.Path), 10, 32)
if err != nil { if err != nil {
log.Printf("Error decoding slot number: %v", err) log.Printf("Error decoding slot number: %v", err)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else if slot < 0 || slot > maxSlot { } 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) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else { } else {
rw.Header().Set("Content-Type", "text/plain") rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusOK)
if s.can.IsEmpty(int(slot)) { if s.can.IsEmpty(int(slot)) {
fmt.Fprintf(rw, "%u empty", slot) fmt.Fprintf(rw, "%d empty", slot)
} else { } else {
fmt.Fprintf(rw, "%u full", slot) fmt.Fprintf(rw, "%d full", slot)
} }
} }
} else { } else {
log.Printf("Mismatched path: %s", r.URL.Path)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} }
}) })
s.HandleFunc("/active/", func(rw http.ResponseWriter, r *http.Request) { 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) slot, err := strconv.ParseUint(path.Base(r.URL.Path), 10, 32)
if err != nil { if err != nil {
log.Printf("Error decoding slot number: %v", err) log.Printf("Error decoding slot number: %v", err)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else if slot < 0 || slot > maxSlot { } 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) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else { } else {
rw.Header().Set("Content-Type", "text/plain") rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusOK)
if s.can.IsDispensing(int(slot)) { if s.can.IsDispensing(int(slot)) {
fmt.Fprintf(rw, "%u dispensing", slot) fmt.Fprintf(rw, "%d dispensing", slot)
} else { } else {
fmt.Fprintf(rw, "%u off", slot) fmt.Fprintf(rw, "%d off", slot)
} }
} }
} else { } else {
log.Printf("Mismatched path: %s", r.URL.Path)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} }
}) })