in golang/go-guestbook/src/backend/main.go [93:117]
func (s *guestbookServer) postMessageHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var v guestbookEntry
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body into json: %+v", err), http.StatusBadRequest)
return
}
if v.Author == "" {
http.Error(w, "empty 'author' value", http.StatusBadRequest)
return
}
if v.Message == "" {
http.Error(w, "empty 'message' value", http.StatusBadRequest)
return
}
v.Date = time.Now()
if err := s.db.addEntry(r.Context(), v); err != nil {
http.Error(w, fmt.Sprintf("failed to save entry: %+v", err), http.StatusInternalServerError)
return
}
log.Printf("entry saved: author=%q message=%q", v.Author, v.Message)
}