Archive for the 'Lua' Category

Learning About the Awesome Window Manager (WM)

I’m enjoying the learning process involved with setting up Awesome. Its working for me, though I did encounter quite a few gotchas:

Here’s what I setup:

.xinitrc / .Xsession

nm-applet &
exec awesome

Then I copied the default rc.lua:

cp /etc/xdg/awesome/rc.lua ~/.config/awesome/rc.lua

I messed around with rc.lua for awhile, but just couldn’t figure out anything useful. Eventually I’ll figure it out, but first I have to figure out the tags.



I’m glad the config file is written in Lua, as I’ve been trying to learn more of it lately.

Lua, XML, and SQL

Though a little hard to find, there are some nice xml projects in lua. Here are two that caught my attention:

http://etree.luaforge.net/ - includes the ability to output a lua data structure as XML. Nice, but uses Expat. Expat is fine, but most of the bindings I use these days connect with libxml2.

http://asbradbury.org/projects/lua-xmlreader/ - Like the name suggests, implements the xmlreader functionality of libxml2. Its a great way of dealing with XML, but I’m not sure if it includes an output function. The author has also written lua cdb bindings - that is awesome.

While not a lua library or project, here’s a post about lua tables and xml which includes an XSLT stylesheet which can convert XML into a lua data structure, very interesting!

http://www.latenightpc.com/blog/archives/2006/02/06/converting-simple-xml-to-lua-tables-with-xslt

There are also some terrific SQL libraries, the one I’m trying out is even a database abstraction layer - LuaSQL. With the help of their examples, this actually works:

require "luasql.sqlite3"
 
env = assert (luasql.sqlite3())
con = assert (env:connect("luasql-test"))
 
res = con:execute"DROP TABLE metadata"
res = assert (con:execute[[
  CREATE TABLE metadata(
    id  int(11),
    key varchar(50),
    value varchar(50)
  )
]])
 
list = {
  { id=1, key="month", value="May", },
}
for i, p in pairs (list) do
  res = assert (con:execute(string.format([[
    INSERT INTO metadata
    VALUES ('%s', '%s', '%s')]], p.id, p.key, p.value)
  ))
end
cur = assert (con:execute"SELECT key,value from metadata")
row = cur:fetch ({}, "a")
while row do
  print(string.format("Key: %s, Value: %s", row.key, row.value))
  row = cur:fetch (row, "a")
end
 
cur:close()
con:close()
env:close()

Why LUA Rocks? I think because its fast and uses memory efficiently!




This is very interesting…

http://www.timestretch.com/FractalBenchmark.html

I don’t want to start a flame war here. The benchmark is just one facet of the programming languages listed, but its interesting to look at and simple enough easily digest. For a more comprehensive analysis of programming languages, see the programming language benchmark shootout game.

In the timestretch benchmarks, lua comes in remarkably competitive with some heavy duty languages. Considering how free, lightweight, and simple lua is, I’m very impressed.

Its a great thing that mod_wombat is coming along, I’m very interested in making use of that when its ready. I should also note that LUA is also used with the new mysql-proxy server that’s in the works.

LUA might also be useful for routing tables, additional types of proxies, or rule set managers for in general. Think about more sophisticated firewalls, spam filtering / email organization, and load balancing. The rules for this stuff is usually written in an incredibly simple syntax, like key value pairs, or on the flip side, sometimes overly complicated regular expressions, and can be compiled into native code using C. That works very well for most cases, but if a little more “logical flexibility”, LUA might just be the answer.

Trying out mod_wombat, an in-development LUA Module for Apache2

I’m finally trying to setup mod_wombat:

sudo apt-get install apache2-threaded-dev liblua5.1-0-dev libapreq2-dev
cd /usr/src/
svn co http://svn.apache.org/repos/asf/httpd/mod_wombat/trunk/ ./mod_wombat
cd ./mod_wombat
./buildconf
./configure --with-mpm=worker --enable-so --with-apxs=/usr/bin/apxs2 --with-apreq2=/usr/

Some configure warnings, but then make gives an error:

/usr/share/apr-1.0/build/libtool --silent --mode=compile --tag=disable-static i486-linux-gnu-gcc -prefer-pic -DLINUX=2 -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -D_REENTRANT -I/usr/include/apr-1.0 -I/usr/include/openssl -I/usr/include/postgresql -I/usr/include/xmltok -pthread     -I/usr/include/apache2  -I/usr/include/apr-1.0   -I/usr/include/apr-1.0 -I/usr/include/postgresql -Wall -Werror -I/usr//include/apreq2 -I/usr/include/lua5.1  -c -o config.lo config.c && touch config.slo
config.c:197:2: error: no newline at end of file
apxs:Error: Command failed with rc=65536
.
make: *** [mod_wombat.la] Error 1

I added “;\n” to the end, ( a real newline ), and it compiled. With a sudo make install, seems to be OK. I just tried out the Hello World example, and it worked!

Hello Lua World!

I couldn’t get the script to execute in a non-public directory, but I didn’t go through the configuration directives with a fine toothed comb. This is a great little tool, it bridges the gap between mod_rewrite / varnish and mod_perl / aolserver. :-)

NOTE: I’ve got to remember that Lua is also used with mysql_proxy.

If this is something that you are interested in, you might want to check out the work Maxime Petazonni is doing for the Google Summer of Code.

Other external links:
http://lua-users.org/wiki/LuaXml
http://asbradbury.org/projects/lua-xmlreader/