// Homework 33, problem 3: A rating form.
//
// The form is finished. Write handleRating: read "rating" with r.FormValue,
// convert it with strconv.Atoi, and reply with status 400 if it is not a whole
// number from 1 to 5. Otherwise print the rating to the terminal, and redirect
// to /thanks with status 303.
//
// Run with: go run exercises/33/homework/03-rating-form.go
package main
import (
"fmt"
"net/http"
)
const formPage = `
Rate this book
Rate this book
`
func handleForm(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, formPage)
}
func handleRating(w http.ResponseWriter, r *http.Request) {
// TODO: write your code here, in place of this placeholder line:
fmt.Fprintln(w, "TODO: replace this line")
}
func handleThanks(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Thank you for your rating.")
}
func main() {
http.HandleFunc("GET /{$}", handleForm)
http.HandleFunc("POST /rating", handleRating)
http.HandleFunc("GET /thanks", handleThanks)
fmt.Println("Server running at http://localhost:8080")
fmt.Println("Press Ctrl+C to stop it.")
err := http.ListenAndServe("localhost:8080", nil)
if err != nil {
fmt.Println("Server stopped:", err)
}
}