Setting up Lua for Web Development on OS X
Lua is a programming language whose potential as a web development platform has been largely overlooked. Languages such as Python, Ruby, and PHP have a long history of use for web development, but Lua has remained largely stuck in its niche as a games development language. This is unfortunate, because I believe Lua offers some interesting possibilities that make it a good candidate for web applications:
Small footprint. Lua's interpreter weighs in about 150k, far smaller than any other web development language. This makes it an excellent candidate for running sites on older hardware, or small VPS systems.
Simplicity. Lua is a tiny, minimalistic language, and like PHP is easy for programmers and non-programmers alike to learn. Unlike PHP, however, it is a very elegant with carefully planned syntax, and consistently named functions and name spaces.
Performance. In almost all benchmarks, Lua is significantly faster than Ruby, PHP or Python. While this doesn't necessarily translate to better web application performance, there's significant potential to create very, very fast web applications in Lua.
Of course, the state of libraries for web development with Lua doesn't hold a candle to any of the other major web languages; if you're a Ruby or Python developer taking on a project working with Lua, you will likely feel as if you're stepping back into the dark ages. If you are looking for the Lua equivalent of Rails, you will most certainly be disappointed. However, if you're looking for the equivalent of Sinatra or Rack, you may be pleasantly surprised.
So assuming by now that you've either given up on Lua for web development, or want to know how to get it installed, let's see how to set it up.
Macports
Installing Lua from Macports is simple:
sudo port -v install lua
This will give you a basic installation of Lua 5.1.4. You'll notice that the download and compile time is almost nothing; welcome to the tiny, fast world of Lua.
Luarocks
Luarocks is the most popular package manager for Lua libraries. Version 2.0 was recently released, but has not yet shown up in Macports, so we'll have to download and install it manually.
Download the latest Luarocks (currently 2.0.1).
tar xzf luarocks-2.0.1.tar.gz
cd luarocks-2.0.1
./configure --prefix=/opt/local
make
make install
Note that we're installing Luarocks into /opt/local, where Lua is also
installed by default by Macports. This is because the current Luarocks can
sometimes have a hard time compiling binary rock files if it is installed in a
location different from Lua itself. In general it's not a good idea to manually
install things into the same tree as Macports, but in this case the benefits (it
will work) outweigh the drawbacks (it sometimes won't).
Editor Support
Vim has excellent support for Lua, and supports syntax-highlighting out of the box.
Here are a few plugins I use:
If you use Vim, feel free to check out my vim config files to see how I'm using it with Lua.
There's also a Textmate bundle for Lua, which you can install like this:
mkdir -p ~/Library/Application Support/TextMate/Bundles
cd ~/Library/Application Support/TextMate/Bundles
git clone git://github.com/textmate/lua.tmbundle.git
You'll also have to configure the TM_LUA environment variable in Textmate
under Preferences > Advanced, and set it to /opt/local/bin/lua.
Orbit
Orbit is a tiny web MVC framework for creating web applications in Lua. Installing via Luarocks is simple:
luarocks install orbit
Using Orbit, you can create web applications in a single Lua file, or spread out
across multiple files. To see how it works, take a look in the sample files that
Luarocks will install in /opt/local/lib/luarocks/rocks/orbit/2.1.0-1/samples.
The songs sample shows much of Orbit's functionality in one small file; the
example below sets up a one-page application at that shows a list (truncated) of
songs from the Beatles' Sgt. Pepper. It sets up a dispatcher to respond with the
render_index method at the root URL, and uses Cosmo and Orbit's built-in HTML
template engine to render the list of songs in HTML.
local orbit = require "orbit"
local cosmo = require "cosmo"
local songs = orbit.new()
function songs.index(web)
local songlist = {
"Sgt. Pepper's Lonely Hearts Club Band",
"With a Little Help from My Friends",
"Lucy in the Sky with Diamonds"
}
return songs.layout(songs.render_index({ songs = songlist }))
end
songs:dispatch_get(songs.index, "/")
function songs.layout(inner_html)
return html{
head{ title "Song List" },
body{ inner_html }
}
end
orbit.htmlify(songs, "layout")
songs.render_index = cosmo.compile[[
<h1>Songs</h1>
<table>
$songs[=[<tr><td>$it</td></tr>]=]
</table>
]]
return songs.run
Orbit also comes with a basic ORM, allowing you to create database-driven MVC apps in Lua.
You can deploy Lua applications as a CGI using almost any web server; because of Lua's tiny size, this incurs nowhere near the performance penalty of running CGI scripts written in Perl or other larger languages. You can also deploy Lua apps using FastCGI on Apache, Lighttpd and other web servers.
Misc
If I've piqued your interest, I recommend the following links to learn more about web development with Lua:
- Fabio Mascarenhas's Blog - Fabio is the author or Orbit and several other web development libraries for Lua.
- Sputnik - A CMS/Wiki engine, there's also a good tutorial here.
- Mercury - A tiny web framework similar to Sinatra.
- LuaHaml - An implementation of the Haml markup language for Lua.
- WSAPI - A low-level web server library used by most Lua web frameworks.
Published