func()

in golang/go-guestbook/src/frontend/main.go [116:140]


func (f *frontendServer) postHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("received request: %s %s", r.Method, r.URL.Path)
	if r.Method != http.MethodPost {
		http.Error(w, "only POST requests are supported", http.StatusMethodNotAllowed)
		return
	}

	author := r.FormValue("name")
	message := r.FormValue("message")
	if author == "" {
		http.Error(w, `"name" not specified in the form`, http.StatusBadRequest)
		return
	}
	if message == "" {
		http.Error(w, `"message" not specified in the form`, http.StatusBadRequest)
		return
	}

	if err := f.saveMessage(r.FormValue("name"), r.FormValue("message")); err != nil {
		http.Error(w, fmt.Sprintf("failed to save message: %+v", err), http.StatusInternalServerError)
		return
	}

	http.Redirect(w, r, "/", http.StatusFound) // redirect to homepage
}