learn-go/snippetbox/cmd/web/handlers.go

40 lines
746 B
Go

package main
import (
"fmt"
"net/http"
"strconv"
)
// home ...
func home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Write([]byte("Hello from snippetbox.chaosfem.tw"))
}
// snippetView ...
func snippetView(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil || id < 1 {
http.NotFound(w, r)
return
}
fmt.Fprintf(w, "It's snippet id: %d", id)
}
// snippetCreate ...
func snippetCreate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Allow", "POST")
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
w.Write([]byte("Creating a snippet!"))
}