APItools / router.lua

A barebones router for Lua. It matches urls and executes lua functions.
MIT License
195 stars 47 forks source link

Add support for Wildcard Routes #9

Closed NZKoz closed 8 years ago

NZKoz commented 9 years ago

Enables you to match an entire subtree to a single function rather than requiring multiple route definitions. We have a number of services which 'own' everything in a sub tree an have quite a substantial list of routes internally. By using a wildcard route we can just blindly send everything off to the relevant service, rather than having to have each sub-app's routes defined in both lua and the underlying app.

r:match('GET', '/api/news_feed/*everything', function(params)
  -- proxy everything under /api/news_feed to the news_feed service
  ngx.exec("@news_feed")
end)
mikz commented 9 years ago

@NZKoz this looks great! We definitely want this functionality. Let me check it it would make sense to introduce another keyword to mount whole subtree over using star. Star could have different meanings like just current routes without slash or something like that. Please push me if I don't respond in few days.

NZKoz commented 9 years ago

I took the syntax from the rails router and emulated the behaviour we actually need is to route entire subtrees over. However we could find ourselves needing:

/api/1.0/news_feed/spam/enable => main monolith app
/api/1.0/news_feed/*whatever => news feed service

But for now I'd survive with this as is. We'll likely roll this branch into production over the next week or two and see how it goes

mikz commented 9 years ago

Rails has also mount which mounts another app to a path. How that would work? Unfortunately we store every method in own table, so there is no easy way of saying all methods going to this path are using this. When using the wildcard you would have to copy the route for every method and to me it seems as duplication that should not be there. You dont have this problem?

NZKoz commented 9 years ago

We do have that problem, but it's pretty minor. Most of our stuff falls through to a big monolith and it's just the high-load read endpoints (GET) that get routed in lua.

The only way to emulate rails' syntax would be to emulate our implementation, where you store routes as a linear list that's evaluated one at a time (modulo some smarts to skip ahead when there are known prefix-misses). That'd make some of our cases easier for sure, but was a bigger change than I was prepared to hack up given the relatively minor cost of the dups to us.

mikz commented 9 years ago

@NZKoz makes sense. Let me add a test case to support /path/* and I'll merge it soon.

skwerlman commented 8 years ago

any news on this?

mikz commented 8 years ago

I'm not working on this or have time to do so. But the change looks good, so mergin!