Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Middlewares in mux Router #463

Open
niondir opened this issue Jul 28, 2023 · 5 comments
Open

Support for Middlewares in mux Router #463

niondir opened this issue Jul 28, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@niondir
Copy link
Collaborator

niondir commented Jul 28, 2023

Is there any plan to support Middlewares in mux.Router?

I imagine some API like in chi.Router (see: https://github.com/go-chi/chi/blob/master/mux.go)
func (mx *Mux) Use(middlewares ...func(http.Handler) http.Handler)
func (mx *Mux) With(middlewares ...func(http.Handler) http.Handler) Router
And optionally:
func (mx *Mux) Group(fn func(r Router)) Router

Code seems straight forward and easy to implement.

Currently I use some custom code, but it's not nice to use:

type CoapMiddleWareChain struct {
	HandlerFunctions []func(h mux.HandlerFunc) mux.HandlerFunc
}

func (m *CoapMiddleWareChain) with(next func(h mux.HandlerFunc) mux.HandlerFunc) *CoapMiddleWareChain {
	m.HandlerFunctions = append(m.HandlerFunctions, next)
	return m
}

func (m *CoapMiddleWareChain) wrap(handler mux.HandlerFunc) mux.HandlerFunc {
	if len(m.HandlerFunctions) == 0 {
		return handler
	} else {
		for i := len(m.HandlerFunctions) - 1; i >= 0; i-- {
			handler = m.HandlerFunctions[i](handler)
		}
	}
	return handler
}
@jkralik
Copy link
Member

jkralik commented Aug 1, 2023

@niondir The middleware's are already supported:

func (r *Router) Use(mwf ...MiddlewareFunc) {

eg example:

r.Use(loggingMiddleware)

Do you need something more ?

@niondir
Copy link
Collaborator Author

niondir commented Aug 2, 2023

I need to check that later. Maybe I was missing it in earlier Versions and just missed when it was added.

Actually I very like the inline syntax of chi. But that's very opinionated, I will see how this works out.

@niondir
Copy link
Collaborator Author

niondir commented Aug 11, 2023

Just missing:
func (mx *Mux) With(middlewares ...func(http.Handler) http.Handler) Router

Adding middleware inline is a quiet nice API. Better than wrapping functions in functions.

@jkralik
Copy link
Member

jkralik commented Aug 21, 2023

Ok. So, pls, could you contribute the function? Thx

@jkralik jkralik added the enhancement New feature or request label Aug 29, 2023
@niondir
Copy link
Collaborator Author

niondir commented Sep 1, 2023

Need to find some time but I will if nobody else does before me :)

@jkralik jkralik assigned Lukaasko and unassigned jkralik and Lukaasko Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants