Use method const in package http instead of literal

This commit is contained in:
Haines Chan 2021-07-08 20:00:03 +08:00
parent 58a61a2cd3
commit 36bb8d293c

View File

@ -32,8 +32,8 @@ type httpKVAPI struct {
func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
key := r.RequestURI key := r.RequestURI
defer r.Body.Close() defer r.Body.Close()
switch { switch r.Method {
case r.Method == "PUT": case http.MethodPut:
v, err := ioutil.ReadAll(r.Body) v, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
log.Printf("Failed to read on PUT (%v)\n", err) log.Printf("Failed to read on PUT (%v)\n", err)
@ -46,13 +46,13 @@ func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Optimistic-- no waiting for ack from raft. Value is not yet // Optimistic-- no waiting for ack from raft. Value is not yet
// committed so a subsequent GET on the key may return old value // committed so a subsequent GET on the key may return old value
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
case r.Method == "GET": case http.MethodGet:
if v, ok := h.store.Lookup(key); ok { if v, ok := h.store.Lookup(key); ok {
w.Write([]byte(v)) w.Write([]byte(v))
} else { } else {
http.Error(w, "Failed to GET", http.StatusNotFound) http.Error(w, "Failed to GET", http.StatusNotFound)
} }
case r.Method == "POST": case http.MethodPost:
url, err := ioutil.ReadAll(r.Body) url, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
log.Printf("Failed to read on POST (%v)\n", err) log.Printf("Failed to read on POST (%v)\n", err)
@ -73,10 +73,9 @@ func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Context: url, Context: url,
} }
h.confChangeC <- cc h.confChangeC <- cc
// As above, optimistic that raft will apply the conf change // As above, optimistic that raft will apply the conf change
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
case r.Method == "DELETE": case http.MethodDelete:
nodeId, err := strconv.ParseUint(key[1:], 0, 64) nodeId, err := strconv.ParseUint(key[1:], 0, 64)
if err != nil { if err != nil {
log.Printf("Failed to convert ID for conf change (%v)\n", err) log.Printf("Failed to convert ID for conf change (%v)\n", err)
@ -93,10 +92,10 @@ func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// As above, optimistic that raft will apply the conf change // As above, optimistic that raft will apply the conf change
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
default: default:
w.Header().Set("Allow", "PUT") w.Header().Set("Allow", http.MethodPut)
w.Header().Add("Allow", "GET") w.Header().Add("Allow", http.MethodGet)
w.Header().Add("Allow", "POST") w.Header().Add("Allow", http.MethodPost)
w.Header().Add("Allow", "DELETE") w.Header().Add("Allow", http.MethodDelete)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
} }
} }