diff --git a/snippetbox/main.go b/snippetbox/cmd/web/handlers.go similarity index 62% rename from snippetbox/main.go rename to snippetbox/cmd/web/handlers.go index fd26685..debf020 100644 --- a/snippetbox/main.go +++ b/snippetbox/cmd/web/handlers.go @@ -1,8 +1,9 @@ package main import ( - "log" + "fmt" "net/http" + "strconv" ) // home ... @@ -16,7 +17,13 @@ func home(w http.ResponseWriter, r *http.Request) { // snippetView ... func snippetView(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("It's a snippet!")) + 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 ... @@ -30,15 +37,3 @@ func snippetCreate(w http.ResponseWriter, r *http.Request) { 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) -} diff --git a/snippetbox/cmd/web/main.go b/snippetbox/cmd/web/main.go new file mode 100644 index 0000000..0c4da5d --- /dev/null +++ b/snippetbox/cmd/web/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "log" + "net/http" +) + +// 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) +}