summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2013-08-20 18:41:01 +0000
committerAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2013-08-20 19:01:10 +0000
commit6033b687f47f33a75c45db3b55ef4731e5b348e3 (patch)
tree9babd507602df34b3bf10c4cae717a840f0f0917
parentbd20dd645eefc8581f76288d2843f531852c53be (diff)
downloadlem-6033b687f47f33a75c45db3b55ef4731e5b348e3.tar.gz
lem-6033b687f47f33a75c45db3b55ef4731e5b348e3.tar.xz
lem-6033b687f47f33a75c45db3b55ef4731e5b348e3.zip
hathaway: support multiple instances
Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
-rw-r--r--lem/hathaway.lua116
1 files changed, 71 insertions, 45 deletions
diff --git a/lem/hathaway.lua b/lem/hathaway.lua
index 1fb14cd..04a2b58 100644
--- a/lem/hathaway.lua
+++ b/lem/hathaway.lua
@@ -26,11 +26,11 @@ local M = {}
function M.debug() end
-local lookup = {}
-M.lookup = lookup
+local ins = {}
+ins.__index = ins
-function M.GET(path, handler)
- local entry = lookup[path]
+function ins:GET(path, handler)
+ local entry = self.lookup[path]
if entry then
entry['HEAD'] = handler
entry['GET'] = handler
@@ -39,34 +39,34 @@ function M.GET(path, handler)
['HEAD'] = handler,
['GET'] = handler,
}
- lookup[path] = entry
+ self.lookup[path] = entry
end
end
do
local function static_setter(method)
- return function(path, handler)
- local entry = lookup[path]
+ return function(self, path, handler)
+ local entry = self.lookup[path]
if entry then
entry[method] = handler
else
- lookup[path] = { [method] = handler }
+ self.lookup[path] = { [method] = handler }
end
end
end
- M.POST = static_setter('POST')
- M.PUT = static_setter('PUT')
- M.DELETE = static_setter('DELETE')
- M.OPTIONS = static_setter('OPTIONS')
+ ins.POST = static_setter('POST')
+ ins.PUT = static_setter('PUT')
+ ins.DELETE = static_setter('DELETE')
+ ins.OPTIONS = static_setter('OPTIONS')
end
-function M.GETM(pattern, handler)
+function ins:GETM(pattern, handler)
local i = 1
while true do
- local entry = lookup[i]
+ local entry = self.lookup[i]
if entry == nil then
- lookup[i] = { pattern,
+ self.lookup[i] = { pattern,
['GET'] = handler,
['HEAD'] = handler
}
@@ -83,12 +83,12 @@ end
do
local function match_setter(method)
- return function(pattern, handler)
+ return function(self, pattern, handler)
local i = 1
while true do
- local entry = lookup[i]
+ local entry = self.lookup[i]
if entry == nil then
- lookup[i] = { pattern, [method] = handler }
+ self.lookup[i] = { pattern, [method] = handler }
break
end
if entry[1] == pattern then
@@ -100,10 +100,10 @@ do
end
end
- M.POSTM = match_setter('POST')
- M.PUTM = match_setter('PUT')
- M.DELETEM = match_setter('DELETE')
- M.OPTIONSM = match_setter('OPTIONS')
+ ins.POSTM = match_setter('POST')
+ ins.PUTM = match_setter('PUT')
+ ins.DELETEM = match_setter('DELETE')
+ ins.OPTIONSM = match_setter('OPTIONS')
end
local function check_match(entry, req, res, ok, ...)
@@ -117,10 +117,10 @@ local function check_match(entry, req, res, ok, ...)
return true
end
-local function handler(req, res)
+local function handler(self, req, res)
local method, path = req.method, req.path
- M.debug('info', format("%s %s HTTP/%s", method, req.uri, req.version))
- local entry = lookup[path]
+ self.debug('info', format("%s %s HTTP/%s", method, req.uri, req.version))
+ local entry = self.lookup[path]
if entry then
local handler = entry[method]
if handler then
@@ -132,7 +132,7 @@ local function handler(req, res)
local i = 0
repeat
i = i + 1
- local entry = lookup[i]
+ local entry = self.lookup[i]
if not entry then
httpresp.not_found(req, res)
break
@@ -141,42 +141,68 @@ local function handler(req, res)
end
end
-function M.Hathaway(host, port)
+function ins:handler(req, res)
+ if req ~= nil then
+ return handler(self, req, res)
+ else
+ return function(req, res)
+ handler(self, req, res)
+ end
+ end
+end
+
+function ins:run(host, port)
local server, err
if port then
- server, err = httpserv.new(host, port, handler)
+ server, err = httpserv.new(host, port, self:handler())
else
- server, err = httpserv.new(host, handler)
+ server, err = httpserv.new(host, self:handler())
end
- if not server then M.debug('new', err) return nil, err end
+ if not server then self.debug('new', err) return nil, err end
- M.server = server
- server.debug = M.debug
+ self.server = server
+ server.debug = self.debug
local ok, err = server:run()
if not ok and err ~= 'interrupted' then
- M.debug('run', err)
+ self.debug('run', err)
return nil, err
end
return true
end
-function M.import(env)
+function ins:import(env)
if not env then
env = _G
end
- env.GET = M.GET
- env.POST = M.POST
- env.PUT = M.PUT
- env.DELETE = M.DELETE
- env.OPTIONS = M.OPTIONS
- env.GETM = M.GETM
- env.POSTM = M.POSTM
- env.PUTM = M.PUTM
- env.DELETEM = M.DELETEM
- env.OPTIONSM = M.OPTIONSM
- env.Hathaway = M.Hathaway
+ env.GET = function(...) self:GET(...) end
+ env.POST = function(...) self:POST(...) end
+ env.PUT = function(...) self:PUT(...) end
+ env.DELETE = function(...) self:DELETE(...) end
+ env.OPTIONS = function(...) self:OPTIONS(...) end
+ env.GETM = function(...) self:GETM(...) end
+ env.POSTM = function(...) self:POSTM(...) end
+ env.PUTM = function(...) self:PUTM(...) end
+ env.DELETEM = function(...) self:DELETEM(...) end
+ env.OPTIONSM = function(...) self:OPTIONSM(...) end
+ env.Hathaway = function(...) self:run(...) end
+end
+
+function ins.new()
+ return setmetatable({
+ lookup = {},
+ debug = M.debug
+ }, ins)
+end
+
+M.new = ins.new
+
+local main_instance = ins.new()
+main_instance:import(M)
+function M.import(...)
+ main_instance:import(...)
+ main_instance.debug = M.debug
end
return M