From 7190f017039774fc0b074420ad58c963ed4cb8f6 Mon Sep 17 00:00:00 2001 From: Luna Stadler Date: Mon, 1 Apr 2024 19:09:01 +0200 Subject: [PATCH] Replace gorilla/mux router with chi All Gorilla repos were archived in 2022 [1] and then seemingly revived in 2023 [2], but it seems they aren't that active and now I've switched already. [1]: https://github.com/gorilla/mux/issues/659#issuecomment-1344468929 [2]: https://gorilla.github.io/blog/2023-07-17-project-status-update/ --- go.mod | 2 +- go.sum | 4 ++-- numblr.go | 30 +++++++++++++++--------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index a3a42a7..683dadd 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/NYTimes/gziphandler v1.1.1 github.com/andybalholm/cascadia v1.3.2 - github.com/gorilla/mux v1.8.1 + github.com/go-chi/chi/v5 v5.0.12 github.com/hashicorp/golang-lru v1.0.2 github.com/mattn/go-sqlite3 v1.14.22 github.com/mmcdole/gofeed v1.2.1 diff --git a/go.sum b/go.sum index 7ef6330..71c4b59 100644 --- a/go.sum +++ b/go.sum @@ -13,11 +13,11 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= +github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= -github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c= github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= diff --git a/numblr.go b/numblr.go index cb5b353..ddef653 100644 --- a/numblr.go +++ b/numblr.go @@ -24,7 +24,7 @@ import ( "time" "github.com/NYTimes/gziphandler" - "github.com/gorilla/mux" + "github.com/go-chi/chi/v5" lru "github.com/hashicorp/golang-lru" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/yuin/goldmark" @@ -210,7 +210,7 @@ func main() { log.Fatal("setup avatar cache:", err) } - router := mux.NewRouter() + router := chi.NewRouter() router.Use(gziphandler.GzipHandler) router.Use(strictTransportSecurity) @@ -301,7 +301,7 @@ self.addEventListener('install', function(e) { // TODO: implement a follow button (first only for generic feed, either add to cookie or url, depending on context) - router.HandleFunc("/settings", func(w http.ResponseWriter, req *http.Request) { + router.Post("/settings", func(w http.ResponseWriter, req *http.Request) { list := req.FormValue("list") feeds := req.FormValue("feeds") @@ -339,9 +339,9 @@ self.addEventListener('install', function(e) { HttpOnly: true, }) http.Redirect(w, req, redirect, http.StatusSeeOther) - }).Methods("POST") + }) - router.HandleFunc("/settings/clear", func(w http.ResponseWriter, req *http.Request) { + router.Post("/settings/clear", func(w http.ResponseWriter, req *http.Request) { cookie, err := req.Cookie(CookieName) if err != nil { http.Redirect(w, req, "/", http.StatusTemporaryRedirect) @@ -352,7 +352,7 @@ self.addEventListener('install', function(e) { cookie.MaxAge = -1 http.SetCookie(w, cookie) http.Redirect(w, req, "/", http.StatusSeeOther) - }).Methods("POST") + }) router.HandleFunc("/proxy", func(w http.ResponseWriter, req *http.Request) { proxyURL := req.URL.Query().Get("url") @@ -456,7 +456,7 @@ func htmlPrelude(w http.ResponseWriter, req *http.Request, title, description, f } func HandleAvatar(w http.ResponseWriter, req *http.Request) { - tumblr := mux.Vars(req)["tumblr"] + tumblr := chi.URLParam(req, "tumblr") avatar, isCached := avatarCache.Get(tumblr) if isCached { @@ -541,7 +541,7 @@ func HandleTumblr(w http.ResponseWriter, req *http.Request) { return } - tag := mux.Vars(req)["tag"] + tag := chi.URLParam(req, "tag") if tag != "" { req.URL.Path = strings.Replace(req.URL.Path, "/tagged/"+tag, "", 1) } @@ -615,8 +615,8 @@ func HandleTumblr(w http.ResponseWriter, req *http.Request) { title := strings.Join(settings.SelectedFeeds, ",") if req.URL.Path == "" || req.URL.Path == "/" { title = "everything" - } else if mux.Vars(req)["list"] != "" { - title = mux.Vars(req)["list"] + } else if chi.URLParam(req, "list") != "" { + title = chi.URLParam(req, "list") } favicon := "/favicon.png" @@ -1022,7 +1022,7 @@ func HandleTumblr(w http.ResponseWriter, req *http.Request) {
-`, mux.Vars(req)["list"], len(settings.SelectedFeeds)+1, strings.Join(settings.SelectedFeeds, "\n")) +`, chi.URLParam(req, "list"), len(settings.SelectedFeeds)+1, strings.Join(settings.SelectedFeeds, "\n")) u := url.URL{ Path: strings.Join(settings.SelectedFeeds, ","), @@ -1294,7 +1294,7 @@ func getFeeds(req *http.Request) []string { cookieName := CookieName if isList { - cookieName = CookieName + "-list-" + mux.Vars(req)["list"] + cookieName = CookieName + "-list-" + chi.URLParam(req, "list") } cookie, err := req.Cookie(cookieName) @@ -1338,9 +1338,9 @@ func prettyDuration(dur time.Duration) string { } func HandlePost(w http.ResponseWriter, req *http.Request) { - tumblr := mux.Vars(req)["tumblr"] - postID := mux.Vars(req)["postId"] - slug := mux.Vars(req)["slug"] + tumblr := chi.URLParam(req, "tumblr") + postID := chi.URLParam(req, "postId") + slug := chi.URLParam(req, "slug") if slug != "" { slug = "/" + slug }