Introduction

We are going to use OpenResty with Lapis and thus create some MoonScript magic.

First, let's take a look at these components:

OpenResty

OpenResty (aka. ngx_openresty) is a software bundle on enhanced distribution of Nginx. It uses standard Nginx core, so you can connect all 3rd-party Nginx modules, as well as most of their external dependencies. OpenResty turns nginx into high performance web server where web developers can use Lua to script various nginx C and Lua modules. It also provides extremely high-performance speed for web applications – it can handle 1k/rps on $5 droplet and more than 10k/rps on more powerful server configurations. OpenResty allows to run your server-side web app completely in the Nginx server, leveraging Nginx event model to do non-blocking I/O not only with the HTTP clients, but also with remote backends like MySQL, PostgreSQL, Memcached, and Redis.

Moonscript

Moonscript is a dynamic scripting language that compiles into Lua. It works like Coffiescript for JavaScript. Moonscript based code is very simple and gets a bit of “syntactic sugar”.

It extends the basic Lua functionality and adds table comprehensions, implicit return on functions, classes, inheritance, scope management statements import & export, and convenient object creation statement with.

class Thing
  name: "unknown"

class Person extends Thing
  say_name: => print "Hello, I am", @name

with Person!
  .name = "MoonScript"
  \say_name!

You can use it for yours regular Lua projects - because it compiles right into Lua code, it is fully compatible with all the known Lua libraries.

Lapis

Finally, Lapis is a MoonScript based framework (it can also be used with Lua) and, as we already know, also compiles into Lua, so we can use it on OpenResty.

It's rather simple; that's how this application looks like:

lapis = require "lapis"

class extends lapis.Application
  -- Define a basic url-pattern that matches /
  "/": =>
    profile_url = @url_for "profile", name: “test-user”
    @html ->
      h2 "Welcome!"
      text "Go to my "
      a href: profile_url, "profile"

  -- Define a named route pattern with a variable called name
  [profile: "/:name"]: =>
    @html ->
      div class: "profile", ->
        text "Welcome to the profile of ", @params.name

Getting Prepared

First, you need to have sudo privileges on your virtual private server and installed Lua and Postgresql server.

If you run Debian or any Debian based OS, we would advise to install the following packages:

apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make

For Fedora and RadHat users:

yum install readline-devel pcre-devel openssl-devel

For Mac OS X you'll need to install any package management tool, for instance Homebrew:

brew install pcre

Step One: installing OpenResty

Simply go to http://openresty.org and download the latest version of the application.

Open terminal and type in the following:

wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz

Then, unpack it and configure for compilation:

tar xzvf ngx_openresty-1.5.8.1.tar.gz
cd ngx_openresty-1.5.8.1/
./configure --with-luajit
      --with-http_postgres_module

That is an important step since, if you get any troubles or missing packages, you'll discover it right now. In that case, check the 'Getting Prepared' step again or just leave a comment below and we'll try to fix it together.

In case everything is well:

make
make install

Wait until compilation completes. Congrats, now, OpenResty is installed!

In the next article we will install Lua package manager luarocks and finally install Moonscript with Lapis.