From 3025aab4452bf413be612451cb973bde929a0847 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Tue, 29 Mar 2022 19:41:12 +0300 Subject: [PATCH] Categories: Make /category/ and /category the same --- web/categories.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/web/categories.go b/web/categories.go index 70b1e5b..6e30b40 100644 --- a/web/categories.go +++ b/web/categories.go @@ -7,7 +7,9 @@ import ( "github.com/bouncepaw/mycorrhiza/views" "github.com/gorilla/mux" "io" + "log" "net/http" + "strings" ) func initCategories(r *mux.Router) { @@ -18,14 +20,18 @@ func initCategories(r *mux.Router) { } func handlerListCategory(w http.ResponseWriter, rq *http.Request) { + log.Println("Viewing list of categories") views.CategoryList(views.MetaFrom(w, rq)) } func handlerCategory(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - var ( - catName = util.HyphaNameFromRq(rq, "category") - ) + catName := util.CanonicalName(strings.TrimPrefix(strings.TrimPrefix(rq.URL.Path, "/category"), "/")) + if catName == "" { + handlerListCategory(w, rq) + return + } + log.Println("Viewing category", catName) views.CategoryPage(views.MetaFrom(w, rq), catName) }