lua-resty-rack

59 views
Skip to first unread message

James Hurst

unread,
May 31, 2012, 12:47:40 PM5/31/12
to open...@googlegroups.com
Hi all,

I've been working on some small applications using ngx_lua, and couldn't quite decide on a convention for loading / configuring modules. It occurred to me today that perhaps some kind of "Rack" like system - a simple predictable interface for creating middleware - would be useful?

So I created a module which looks a bit like Connect (http://www.senchalabs.org/connect/) for Node.js. The code is here as a "resty" module:


The idea is that you create a module by implementing a function called "call" which can accept parameters as a table, and in turn returns the function to be run when this middleware has its turn. Such as:

module("mymodule", package.seeall)

function call(options)
  return function(req, res, next)
    ... do things
    next() -- I'm done modifying the req/res, try the next middleware.

    -- or

    return ngx.HTTP_OK, headers, "Hello World" -- I'm taking over this request..
  end
end

You either return status, header, body, or just call next() if your job is more like a filter.


To use the middleware, you'd do something like:

server {
    location /test {
        content_by_lua '
            local rack = require "resty.rack"

            rack.use(rack.middleware.method_override)
            ... other middleware.. 
            rack.run()
        ';
    }
}


I just wrote this today, as an experiment.. any thoughts / ideas / alternative approaches would be appreciated :)

Cheers,


--
James Hurst
+44 (0) 7979 817 702 | @pintsized

River Rat Records
Politi Arts, 10 Manor Road, London, N16 5SA
www.riverratrecords.com | @riverratrecords

agentzh

unread,
Jun 1, 2012, 12:28:27 AM6/1/12
to open...@googlegroups.com
Hello!

On Fri, Jun 1, 2012 at 12:47 AM, James Hurst <ja...@riverratrecords.com> wrote:
> I've been working on some small applications using ngx_lua, and couldn't
> quite decide on a convention for loading / configuring modules. It occurred
> to me today that perhaps some kind of "Rack" like system - a simple
> predictable interface for creating middleware - would be useful?
>

Your example looks interesting :) Thanks for sharing!

Regards,
-agentzh

James Hurst

unread,
Jun 8, 2012, 10:45:58 AM6/8/12
to open...@googlegroups.com
Hi again,

I've worked on this a little more, and written some documentation. Hopefully some people will find it useful.. I'll try to write some better bundled utility middleware soon:


Also, I've documented my HTTP caching middleware called "ledge", which is built using "lua-resty-rack":


The plan for this is much broader; serving stale requests, collapse forwarding, ESI, stats gathering, and I'm working on some "auto priming" tools for it to. For now, it simply does caching.

Any feedback welcome!

Cheers,

James.



agentzh

unread,
Jun 8, 2012, 9:29:58 PM6/8/12
to open...@googlegroups.com
Hello!

On Fri, Jun 8, 2012 at 10:45 PM, James Hurst <ja...@riverratrecords.com> wrote:
> Also, I've documented my HTTP caching middleware called "ledge", which is
> built using "lua-resty-rack":
>
>  https://github.com/pintsized/ledge
>
> The plan for this is much broader; serving stale requests, collapse
> forwarding, ESI, stats gathering, and I'm working on some "auto priming"
> tools for it to. For now, it simply does caching.
>

Awesome! I'm so glad to see that things are getting more and more
interesting here. It kinda reminds me of the ngx_srcache module:

https://github.com/agentzh/srcache-nginx-module

but it can surely do much more ;)

Thanks!
-agentzh
Reply all
Reply to author
Forward
0 new messages