Changed the HTTP router to chi.
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user