Skip to content

Commit

Permalink
replacing (obsolete) gorilla/mux with go-chi/chi
Browse files Browse the repository at this point in the history
  • Loading branch information
ninlil committed Mar 27, 2023
1 parent 85e2ef1 commit 7ea0761
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module github.com/ninlil/butler

go 1.19
go 1.20

require (
github.com/gorilla/mux v1.8.0
github.com/go-chi/chi v1.5.4
github.com/justinas/alice v1.2.0
github.com/rs/xid v1.3.0
github.com/rs/zerolog v1.27.0
github.com/rs/xid v1.4.0
github.com/rs/zerolog v1.29.0
)

require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
golang.org/x/sys v0.6.0 // indirect
)
24 changes: 14 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs=
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15 changes: 7 additions & 8 deletions router/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"time"

"github.com/gorilla/mux"
"github.com/ninlil/butler/bufferedresponse"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
Expand Down Expand Up @@ -55,7 +54,6 @@ func accessLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w2, _ := bufferedresponse.Get(w)
log := hlog.FromRequest(r)
route := mux.CurrentRoute(r)

start := time.Now()
next.ServeHTTP(w, r)
Expand All @@ -77,12 +75,13 @@ func accessLogger(next http.Handler) http.Handler {
e.Int("status", w2.Status())
e.Int("size", w2.Size())

if route != nil {
e.Str("route", route.GetName())
// if path, err := route.GetPathTemplate(); err == nil {
// e.Str("path", path)
// }
}
// 'chi' doesn't have a way to get the route name
// if route != nil {
// e.Str("route", route.GetName())
// // if path, err := route.GetPathTemplate(); err == nil {
// // e.Str("path", path)
// // }
// }

e.Msgf("%s %s", r.Method, r.URL.Path)
})
Expand Down
8 changes: 6 additions & 2 deletions router/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"reflect"
"regexp"

"github.com/gorilla/mux"
"github.com/go-chi/chi"
"github.com/ninlil/butler/log"
)

Expand Down Expand Up @@ -67,7 +67,11 @@ func (param *paramData) getValue(f reflect.Value, tags *tagInfo, r *http.Request
switch tags.From {
case fromPath:
if param.vars == nil {
param.vars = mux.Vars(r)
params := chi.RouteContext(r.Context()).URLParams
param.vars = make(map[string]string, len(params.Keys))
for i, key := range params.Keys {
param.vars[key] = params.Values[i]
}
}
value, found = param.vars[tags.Name]

Expand Down
22 changes: 11 additions & 11 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sync"
"time"

"github.com/gorilla/mux"
"github.com/go-chi/chi"
"github.com/justinas/alice"
"github.com/ninlil/butler/log"
"github.com/ninlil/butler/runtime"
Expand Down Expand Up @@ -113,7 +113,7 @@ type Router struct {
skip204 bool

// runtime
router *mux.Router
router *chi.Mux
routes []*Route
server *http.Server
mutex sync.Mutex
Expand Down Expand Up @@ -169,7 +169,7 @@ func New(routes []Route, opts ...Option) (*Router, error) {
router.name = "default"
}

router.router = mux.NewRouter().StrictSlash(router.strictSlash)
router.router = chi.NewRouter()

for i := range routes {
route := routes[i]
Expand Down Expand Up @@ -201,22 +201,22 @@ func (r *Router) Serve() error {
var haveReady bool
var haveHealty bool

//r.router.Use(wrapWriterMW, log.NewHandler(), IDHandler(), accessLogger)

for _, route := range r.routes {
// log.Trace().Msgf("adding %s %s", route.Method, route.Path)
h := r.router.NewRoute().Name(route.Name)
// h := r.router.NewRoute().Name(route.Name)
var method = "GET"

if route.Method != All {
h = h.Methods(route.Method)
method = route.Method
}

if route.Path == "/" && r.prefix != "" {
route.Path = r.prefix
} else {
route.Path = r.prefix + route.Path
}
if route.Path != "" {
h = h.Path(route.Path)
}
// log.Trace().Msgf("%s -> %s", route.Name, route.Path)

switch route.Path {
Expand All @@ -236,16 +236,16 @@ func (r *Router) Serve() error {
// chain = chain.Append(hlog.RefererHandler("referer"))
// chain = chain.Append(hlog.RequestIDHandler("req_id", "Request-Id"))

h.Handler(chain.Append(r.panicHandler).ThenFunc(route.wrapHandler()))
r.router.Method(method, route.Path, chain.Append(r.panicHandler).ThenFunc(route.wrapHandler()))
}

if !haveHealty && r.healthPath != "" {
// log.Trace().Msg("adding /healtyz")
r.router.NewRoute().Name("healthyz").Methods("GET").Path(r.healthPath).HandlerFunc(healthyProbe)
r.router.Get(r.healthPath, healthyProbe)
}
if !haveReady && r.readyPath != "" {
// log.Trace().Msg("adding /readyz")
r.router.NewRoute().Name("readyz").Methods("GET").Path(r.readyPath).HandlerFunc(readyProbe)
r.router.Get(r.readyPath, readyProbe)
}

if err := running.addRouter(r); err != nil {
Expand Down

0 comments on commit 7ea0761

Please sign in to comment.