elixir-lang/plug — pull request #139 by whatyouhide (2014-12-06 19:14:11+00:00)
Title: Add a Plug.Router.scope macro

This is a throwaway PR :smile:

I love the scoping functionality provided by the Phoenix framework (and by Rails and by Sinatra + gems) and would love to be able to use it with Plug's compiled routes. Don't get me wrong, I've got nothing against Phoenix, but I like using barebones Plug when I can just for speed and simplicity. I find it's somehow like the Rails vs. Sinatra use case.

Just for the sake of playing with Elixir (which is fun!), I jotted down an implementation of a `scope` macro (`Plug.Router.scope`) which turned out to work in most cases. I implemented the macro so that it delegates as much as possible to the already existing macros (`match`, `forward` and so on): it just "injects" the prefix path and forwards any additional option (like `:host`) to the original macro.

This way, scoped routes are just compiled and pattern matched like all other routes.

The implementation is far from perfect and I already spotted a couple of bugs (e.g. it doesn't play well with `match _ do...`), but I'm opening the PR anyway mostly to see if people like the idea and so that I have a place where I can push commits regarding this topic.

So, let me know :smile_cat: