Skip to content

Commit

Permalink
Replace gorilla/mux router with chi
Browse files Browse the repository at this point in the history
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]: gorilla/mux#659 (comment)
[2]: https://gorilla.github.io/blog/2023-07-17-project-status-update/
  • Loading branch information
heyLu committed Apr 1, 2024
1 parent 1371db2 commit 7190f01
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
30 changes: 15 additions & 15 deletions numblr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func HandleTumblr(w http.ResponseWriter, req *http.Request) {
<form method="POST" action="/settings/clear">
<input type="submit" value="Clear" title="FIXME: clear currently broken :/" disabled />
</form>
`, 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, ","),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7190f01

Please sign in to comment.