Moved compilation of page templates to lovebird.init()

All the page templates are now compiled into functions when lovebird is
inited. This avoids having to interpret a page template each time it is
requested.
This commit is contained in:
rxi 2014-04-13 16:25:58 +01:00
parent 188d052d34
commit 62db558f4c

View File

@ -379,9 +379,11 @@ end
function lovebird.init() function lovebird.init()
-- Init server
lovebird.server = assert(socket.bind(lovebird.host, lovebird.port)) lovebird.server = assert(socket.bind(lovebird.host, lovebird.port))
lovebird.addr, lovebird.port = lovebird.server:getsockname() lovebird.addr, lovebird.port = lovebird.server:getsockname()
lovebird.server:settimeout(0) lovebird.server:settimeout(0)
-- Wrap print
if lovebird.wrapprint then if lovebird.wrapprint then
local oldprint = print local oldprint = print
print = function(...) print = function(...)
@ -389,6 +391,10 @@ function lovebird.init()
lovebird.print(...) lovebird.print(...)
end end
end end
-- Compile page templates
for k, page in pairs(lovebird.pages) do
lovebird.pages[k] = lovebird.template(page, "lovebird, req")
end
lovebird.inited = true lovebird.inited = true
end end
@ -498,8 +504,7 @@ function lovebird.onrequest(req, client)
local str local str
xpcall(function() xpcall(function()
str = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" .. str = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" ..
lovebird.template(lovebird.pages[page], lovebird.pages[page](lovebird, req)
"lovebird, req")(lovebird, req)
end, lovebird.onerror) end, lovebird.onerror)
return str return str
end end