From fb45f1f5002ffdb40150333c5a48458b801f3022 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Thu, 30 May 2024 18:04:49 +0200 Subject: ref: Move to gorilla mux Go's default mux is very limited while comes to overlapping routes. For example "/{name}/about" conflicts with "/static/" and it throws a panic. In the case I would like static to have precedence over everything else. --- go.mod | 1 + go.sum | 2 ++ pkg/handler/router.go | 12 +++++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5bd4d76..eabf863 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-git/go-git/v5 v5.12.0 github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 github.com/google/go-cmp v0.6.0 + github.com/gorilla/mux v1.8.1 github.com/valyala/quicktemplate v1.7.0 golang.org/x/sync v0.7.0 ) diff --git a/go.sum b/go.sum index 69c34b7..bc82b64 100644 --- a/go.sum +++ b/go.sum @@ -51,6 +51,8 @@ github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 h1:yEt5djSYb4i github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= diff --git a/pkg/handler/router.go b/pkg/handler/router.go index 1150f2f..f73e9fb 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -9,6 +9,7 @@ import ( "git.gabrielgio.me/cerrado/pkg/handler/git" "git.gabrielgio.me/cerrado/pkg/handler/static" "git.gabrielgio.me/cerrado/pkg/service" + "github.com/gorilla/mux" ) // Mount handler gets the requires service and repository to build the handlers @@ -29,11 +30,16 @@ func MountHandler( return nil, err } - mux := http.NewServeMux() - mux.Handle("/static/", staticHandler) + mux := mux.NewRouter() + + mux.PathPrefix("/static").Handler(staticHandler) + mux.HandleFunc("/{name}/about", gitHandler.About) + mux.HandleFunc("/{name}/summary", gitHandler.Summary) + mux.HandleFunc("/{name}/refs", gitHandler.Refs) + mux.HandleFunc("/{name}/tree", gitHandler.Tree) + mux.HandleFunc("/{name}/log", gitHandler.Log) mux.HandleFunc("/config", configHander) mux.HandleFunc("/about", aboutHandler.About) - mux.HandleFunc("/{name}", gitHandler.Item) mux.HandleFunc("/", gitHandler.List) return mux, nil } -- cgit v1.2.3