From 1ab140bdbb469ff40fda37b880462c9c46dfe556 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 30 Sep 2021 11:55:01 -0500 Subject: [PATCH] Changed the HTTP router to chi. --- go.mod | 5 +++- go.sum | 2 ++ main.go | 81 +++++++++++++++------------------------------------------ 3 files changed, 27 insertions(+), 61 deletions(-) diff --git a/go.mod b/go.mod index 5fe7446..58e5a7d 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module copyrightcrusader.org/inv go 1.16 -require github.com/lib/pq v1.10.2 +require ( + github.com/go-chi/chi v1.5.4 // indirect + github.com/lib/pq v1.10.2 +) diff --git a/go.sum b/go.sum index 3708eaf..5909018 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs= +github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg= github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= diff --git a/main.go b/main.go index 59e54b7..dc9ac22 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "strconv" "strings" "time" + + "github.com/go-chi/chi" ) //Go Best practices: https://github.com/golovers/effective-go @@ -27,13 +29,24 @@ type Item struct { } func main() { - http.HandleFunc("/categories", categories) - //Note: seems adding a trailing slash allows routing URLs like /category/ID# - //No need for query parameters. - http.HandleFunc("/category/", category) - http.HandleFunc("/subcategory/", subcategory) + r := chi.NewRouter() - err := http.ListenAndServe(":8088", nil) + r.Route("/categories", func(r chi.Router) { + r.Get("/", getCategoriesHandler) + }) + + r.Route("/category", func(r chi.Router) { + r.Get("/{id:[0-9]+}", getCategoryHandler) + r.Post("/", insertCategoryHandler) + r.Delete("/{id:[0-9]+}", deleteCategoryHandler) + }) + + r.Route("/subcategory", func(r chi.Router) { + r.Post("/", insertSubcategoryHandler) + r.Delete("/{id:[0-9]+}", deleteSubcategoryHandler) + }) + + err := http.ListenAndServe(":8088", r) if err != nil { panic(err) @@ -41,52 +54,8 @@ func main() { } -func categories(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case "GET": - getCategoriesHandler(w, r) - default: - w.WriteHeader(http.StatusMethodNotAllowed) - w.Write([]byte("Method not allowed")) - } -} - -func category(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case "GET": - getCategoryHandler(w, r) - case "POST": - insertCategoryHandler(w, r) - case "DELETE": - deleteCategoryHandler(w, r) - default: - w.WriteHeader(http.StatusMethodNotAllowed) - w.Write([]byte("Method not allowed")) - } -} - -func subcategory(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case "POST": - insertSubcategoryHandler(w, r) - case "DELETE": - deleteSubcategoryHandler(w, r) - default: - w.WriteHeader(http.StatusMethodNotAllowed) - w.Write([]byte("Method not allowed")) - } -} - func deleteSubcategoryHandler(w http.ResponseWriter, r *http.Request) { - id := strings.TrimPrefix(r.URL.Path, "/subcategory/") - - value, err := strconv.ParseInt(id, 0, 64) - - if err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) - return - } + value, _ := strconv.ParseInt(chi.URLParam(r, "id"), 0, 64) if !deleteSubcategory(value) { w.WriteHeader(http.StatusBadRequest) @@ -98,15 +67,7 @@ func deleteSubcategoryHandler(w http.ResponseWriter, r *http.Request) { } func getCategoryHandler(w http.ResponseWriter, r *http.Request) { - id := strings.TrimPrefix(r.URL.Path, "/category/") - - value, err := strconv.ParseInt(id, 0, 64) - - if err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) - return - } + value, _ := strconv.ParseInt(chi.URLParam(r, "id"), 0, 64) category, ok := getCategoryByID(value)