Correct HTTP responses and semantic codes
This commit is contained in:
parent
b90fe59765
commit
7599daf34a
18
messenger.go
18
messenger.go
@ -280,25 +280,37 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
|
||||
err := json.Unmarshal(body, &rec)
|
||||
if err != nil {
|
||||
fmt.Println("could not decode response:", err)
|
||||
fmt.Fprintln(w, `{status: 'not ok'}`)
|
||||
respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if rec.Object != "page" {
|
||||
fmt.Println("Object is not page, undefined behaviour. Got", rec.Object)
|
||||
respond(w, http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
|
||||
if m.verify {
|
||||
if err := m.checkIntegrity(r); err != nil {
|
||||
fmt.Println("could not verify request:", err)
|
||||
fmt.Fprintln(w, `{status: 'not ok'}`)
|
||||
respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
m.dispatch(rec)
|
||||
|
||||
fmt.Fprintln(w, `{status: 'ok'}`)
|
||||
respond(w, http.StatusAccepted) // We do not return any meaningful response immediately so it should be 202
|
||||
}
|
||||
|
||||
func respond(w http.ResponseWriter, code int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
if code < 400 {
|
||||
fmt.Fprintln(w, `{"status": "ok"}`)
|
||||
} else {
|
||||
fmt.Fprintln(w, `{"status": "not ok"}`)
|
||||
}
|
||||
}
|
||||
|
||||
// checkIntegrity checks the integrity of the requests received
|
||||
|
Loading…
Reference in New Issue
Block a user