rs / xmux

xmux is a httprouter fork on top of xhandler (net/context aware)
Other
98 stars 11 forks source link

Wrapping group with middleware #5

Closed Taik closed 8 years ago

Taik commented 8 years ago

Hi,

Firstly, thanks for the awesome package!

I'm currently evaluating xhandler/xmux, and I can't seem to find a way to wrap a Group instance with middleware.

My use case would be something like:

m := xmux.New()
g := m.Group("/admin")
g.GET("/users", AuthMiddleware(adminUserHandler))
g.GET("/items", AuthMiddleware(adminItemHandler))

Is there a way for me to remove the redundant AuthMiddleware on each handler and associate it with g so it is evaluated for every route on g?

Thanks!

rs commented 8 years ago

No there is not. The group feature is a syntax sugar. Behind the scene, all routes are added to m with "/admin" prefixed. The pros is that groups have no performance penalties, the cons is that you can't apply a middleware to the group.