justinas / alice

Painless middleware chaining for Go

Home Page:https://godoc.org/github.com/justinas/alice

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conditional middlewares

2hamed opened this issue · comments

I though it might be a good idea to make alice be able to apply middlewares based on conditions which the Request must satisfy.
For example some url paths need user authentication and some don't. It's a waste to apply the auth middleware to those as well.

Any idea on this?

Hi @2hamed,

Try something like this:

r := mux.NewRouter()
r.Handle("/about", alice.New(middleware1, middleware2).Then(aboutPage))
r.Handle("/contact", alice.New(middleware3, middleware4).Then(contactPage))

Gorilla mux can also match on headers, query values, etc., so this approach is pretty flexible.

Awesome, thanks.