From cb0a33563696d71b56878f94bbf22ab158a44019 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Mon, 22 Jan 2024 13:25:34 -0800 Subject: [PATCH] lets-go:2.5 --- snippetbox/main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 snippetbox/main.go diff --git a/snippetbox/main.go b/snippetbox/main.go new file mode 100644 index 0000000..fd26685 --- /dev/null +++ b/snippetbox/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "log" + "net/http" +) + +// 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) { + w.Write([]byte("It's a snippet!")) +} + +// 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!")) +} + +// main it's the snippetbox webapp +func main() { + mux := http.NewServeMux() + mux.HandleFunc("/", home) + mux.HandleFunc("/snippet/view", snippetView) + mux.HandleFunc("/snippet/create", snippetCreate) + + log.Print("starting a server on :4000") + err := http.ListenAndServe(":4000", mux) + log.Fatal(err) +}