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

40 lines
746 B
Go
Raw Normal View History

2024-01-22 21:25:34 +00:00
package main
import (
"fmt"
2024-01-22 21:25:34 +00:00
"net/http"
"strconv"
2024-01-22 21:25:34 +00:00
)
// 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)
2024-01-22 21:25:34 +00:00
}
// 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!"))
}