From 88442edee5ea6ff8587927dc5e515c347d6e8488 Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Fri, 2 Jun 2023 11:55:06 -0700 Subject: [PATCH] chore(deps): remove gorilla mux in favor of using go-chi (#2232) * chore(deps): remove gorilla mux in favor of using go-chi * fixes broken redirect and tests * chore: clean up * reverts changes --- assets/components.d.ts | 1 - go.mod | 5 +++-- go.sum | 8 ++----- web/auth.go | 6 ++--- web/routes.go | 50 +++++++++++++++++++++++++----------------- web/routes_test.go | 5 ++--- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/assets/components.d.ts b/assets/components.d.ts index b4742ecc..fa49a5c5 100644 --- a/assets/components.d.ts +++ b/assets/components.d.ts @@ -36,7 +36,6 @@ declare module '@vue/runtime-core' { LogStd: typeof import('./components/LogViewer/LogStd.vue')['default'] LogViewer: typeof import('./components/LogViewer/LogViewer.vue')['default'] LogViewerWithSource: typeof import('./components/LogViewer/LogViewerWithSource.vue')['default'] - 'Mdi:check': typeof import('~icons/mdi/check')['default'] 'Mdi:dotsVertical': typeof import('~icons/mdi/dots-vertical')['default'] 'Mdi:lightChevronDoubleDown': typeof import('~icons/mdi-light/chevron-double-down')['default'] 'Mdi:lightChevronLeft': typeof import('~icons/mdi-light/chevron-left')['default'] diff --git a/go.mod b/go.mod index 28742080..293724af 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/dustin/go-humanize v1.0.1 github.com/gogo/protobuf v1.3.2 // indirect - github.com/gorilla/mux v1.8.0 github.com/gorilla/sessions v1.2.1 github.com/magiconair/properties v1.8.7 github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect @@ -27,6 +26,8 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) +require github.com/go-chi/chi/v5 v5.0.8 + require ( github.com/alexflint/go-scalar v1.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect @@ -42,4 +43,4 @@ require ( gotest.tools/v3 v3.0.3 // indirect ) -go 1.19 +go 1.20 diff --git a/go.sum b/go.sum index c7b780c9..b782da03 100644 --- a/go.sum +++ b/go.sum @@ -63,8 +63,6 @@ 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/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8= github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v24.0.1+incompatible h1:NxN81beIxDlUaVt46iUQrYHD9/W3u9EGl52r86O/IGw= -github.com/docker/docker v24.0.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker v24.0.2+incompatible h1:eATx+oLz9WdNVkQrr0qjQ8HvRJ4bOOxfzEo8R+dA3cg= github.com/docker/docker v24.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= @@ -79,6 +77,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0= +github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -140,8 +140,6 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= -github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI= @@ -196,8 +194,6 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= -github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/web/auth.go b/web/auth.go index 013621c2..f3db5735 100644 --- a/web/auth.go +++ b/web/auth.go @@ -27,17 +27,17 @@ func initializeAuth(h *handler) { } } -func authorizationRequired(f http.HandlerFunc) http.Handler { +func authorizationRequired(next http.Handler) http.Handler { if secured { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if isAuthorized(r) { - f(w, r) + next.ServeHTTP(w, r) } else { http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } }) } else { - return f + return next } } diff --git a/web/routes.go b/web/routes.go index 50e5172c..315f62e2 100644 --- a/web/routes.go +++ b/web/routes.go @@ -13,8 +13,9 @@ import ( "github.com/amir20/dozzle/analytics" "github.com/amir20/dozzle/docker" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" - "github.com/gorilla/mux" log "github.com/sirupsen/logrus" ) @@ -47,31 +48,40 @@ func CreateServer(clients map[string]docker.Client, content fs.FS, config Config var fileServer http.Handler -func createRouter(h *handler) *mux.Router { +func createRouter(h *handler) *chi.Mux { initializeAuth(h) base := h.config.Base - r := mux.NewRouter() - r.Use(cspHeaders) - if base != "/" { - r.HandleFunc(base, func(w http.ResponseWriter, req *http.Request) { - http.Redirect(w, req, base+"/", http.StatusMovedPermanently) - }) + r := chi.NewRouter() + + if log.IsLevelEnabled(log.DebugLevel) { + r.Use(middleware.Logger) } - s := r.PathPrefix(base).Subrouter() - s.Handle("/api/logs/stream", authorizationRequired(h.streamLogs)) - s.Handle("/api/logs/download", authorizationRequired(h.downloadLogs)) - s.Handle("/api/logs", authorizationRequired(h.fetchLogsBetweenDates)) - s.Handle("/api/events/stream", authorizationRequired(h.streamEvents)) - s.HandleFunc("/api/validateCredentials", h.validateCredentials) - s.Handle("/logout", authorizationRequired(h.clearSession)) - s.Handle("/version", authorizationRequired(h.version)) - s.HandleFunc("/healthcheck", h.healthcheck) + r.Use(cspHeaders) + + r.Route(base, func(r chi.Router) { + r.Group(func(r chi.Router) { + r.Use(authorizationRequired) + r.Get("/api/logs/stream", h.streamLogs) + r.Get("/api/events/stream", h.streamEvents) + r.Get("/api/logs/download", h.downloadLogs) + r.Get("/api/logs", h.fetchLogsBetweenDates) + r.Get("/logout", h.clearSession) + r.Get("/version", h.version) + }) + + r.Post("/api/validateCredentials", h.validateCredentials) + r.Get("/healthcheck", h.healthcheck) + defaultHandler := http.StripPrefix(strings.Replace(base+"/", "//", "/", 1), http.HandlerFunc(h.index)) + r.NotFound(func(w http.ResponseWriter, req *http.Request) { + defaultHandler.ServeHTTP(w, req) + }) + }) if base != "/" { - s.PathPrefix("/").Handler(http.StripPrefix(base+"/", http.HandlerFunc(h.index))) - } else { - s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(h.index))) + r.Get(base, func(w http.ResponseWriter, req *http.Request) { + http.Redirect(w, req, base+"/", http.StatusMovedPermanently) + }) } fileServer = http.FileServer(http.FS(h.content)) diff --git a/web/routes_test.go b/web/routes_test.go index 7f1bbdbd..e4cd4310 100644 --- a/web/routes_test.go +++ b/web/routes_test.go @@ -7,9 +7,8 @@ import ( "io" "io/fs" - "github.com/gorilla/mux" - "github.com/amir20/dozzle/docker" + "github.com/go-chi/chi/v5" "github.com/stretchr/testify/mock" @@ -59,7 +58,7 @@ func (m *MockedClient) ContainerLogsBetweenDates(ctx context.Context, id string, return args.Get(0).(io.ReadCloser), args.Error(1) } -func createHandler(client docker.Client, content fs.FS, config Config) *mux.Router { +func createHandler(client docker.Client, content fs.FS, config Config) *chi.Mux { if client == nil { client = new(MockedClient) client.(*MockedClient).On("ListContainers").Return([]docker.Container{}, nil)