Lapis: Lua for the Web

published 2013-09-01 [ home ]

I use different programming languages for different tasks, but the one I prefer is Lua. I have always wanted to use it for the Web, and in a way I already do: this blog is a static website generated by a custom Lua program. I have also written several services that can speak HTTP+JSON in Lua. For larger, HTML-based Web applications however, I have never found the framework I wanted. I have tried several of them, but kept coming back to more dependable platforms.

Last March, I gave Lapis a try. It is a relatively new framework written by Leaf Corcoran, the author of MoonScript, a programming language that compiles to Lua (like CoffeeScript for JavaScript). Lapis is powered by OpenResty, an incredibly fast web application server that run inside the nginx Web server and is already used by large websites like Taobao and CloudFlare.

Lapis was built with MoonScript in mind, so I had to hack around it to make it work with plain Lua. It worked but was too verbose, so I eventually gave up on that and on Lapis altogether. But that was Lapis version 0.0.1! Leaf continues to improve it and recently released version 0.0.4. Last Friday evening, on a train to Bordeaux, I decided to give it another try.

Lapis now natively supports Lua and has improved in various aspects. I have found it comfortable to write the views and the configuration file with the MoonScript DSLs provided by the framework. For logic (models and controllers) I used Lua directly since I prefer its syntax to MoonScript for regular code.

I have published a small skeleton application which demonstrates this dual languages style to GitHub. I chose to use Redis for the datastore because I know it well and had it running on my laptop, so I did not use the database integration layer of MoonScript which is designed primarily for PostgreSQL (but it looks nice as well). This application is a kind of Hello World but it demonstrates most features of the framework, including sub-applications, widgets, layouts, exception-handling and input validation.

If you want an example of a larger Lapis codebase with a different style, MoonRocks is written in Lapis and its code is on GitHub.

So, what did I think of it? Well, finally I could see myself write a serious Web application in Lua! I will still choose Python and Flask in a professional setting because it is is a more stable, more feature-complete stack, and because I would not want to ask a whole team to learn both Lua and MoonScript to work on the project. But if I make a Web application as a personal side project, I will certainly try to use Lapis for that.