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?
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