Write Apache modules quickly in Lua

I really like mod_lua [1], even in its alpha state. In less than five minutes I had a webpage that would display a different quote each time it was referenced. I was able to modify the Lua based qotd [2], changing:

QUOTESFILE = "/home/spc/quotes/quotes.txt"
quotes = {}
do
local eoln = "\r\n"
local f = io.open(QUOTESFILE,"r")
local s = ""
for line in f:lines() do
if line == "" then
-- each quote is separated by a blank link
if #s < 512 then
table.insert(quotes,s)
end
s = ""
else
s = s .. line .. eoln
end
end
f:close()
end
math.randomseek(os.time())
function main(socket)
socket:write(quotes[math.random(#quotes)])
end

to

QUOTESFILE = "/home/spc/quotes/quotes.txt"
quotes = {}
do
local eoln = "\r\n"
local f = io.open(QUOTESFILE,"r")
local s = ""
for line in f:lines() do
if line == "" then
-- each quote is separated by a blank link
if #s < 512 then
table.insert(quotes,s)
end
s = ""
else
s = s .. line .. eoln
end
end
f:close()
end
math.randomseek(os.time())
function handler(r)
r.content_type = "text/plain"
r:puts(quotes[math.random(#quotes)])
end

(you can see, it didn't take much), and adding

LuaMapHandler /quote.html /home/spc/web/lua/lib/quote.lua

to the site configuration (what you don't see is the only other line you need, LuaRoot), reload Apache [3] and I now have webpage backed by Lua [4].

And from there, it isn't much to add some HTML (HyperText Markup Language) to the output, but it should be clear that adding Apache modules in Lua isn't that hard.

What did take me by surprise is that there's no real way to do the heavy initialization just once. That bit of reading in the quotes file? It's actually done for every request—mod_lua just compiles the code and keeps the compiled version cached and for each request, runs the compiled code. It'd be nice if there was a way to do some persistent initialization once (a feature I use in the current mod_litbook [5]), but as written, mod_lua doesn't have support for that.

I also haven't see any action on my bug report [6]—not a good sign.

I'm wondering if I might not have to pick up [DELETED-the ball-DELETED] mod_lua and run with it …

=> [1] /boston/2010/04/03.1 | [2] /boston/2009/10/21.1 | [3] http://httpd.apache.org/ | [4] http://www.lua.org/ | [5] https://github.com/spc476/mod_litbook | [6] https://issues.apache.org/bugzilla/show_bug.cgi?id=49044

=> Gemini Mention this post | Contact the author

Proxy Information
Original URL
gemini://gemini.conman.org/boston/2010/04/06.3
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
556.848802 milliseconds
Gemini-to-HTML Time
2.88551 milliseconds

This content has been proxied by September (ba2dc).