The voice of BeeBole

Video Tutorial To Start Developing Web Applications on Erlang

October 20, 2008 by Hughes (Erlang)

Please find hereunder a video tutorial highlighting the key points of developing a JSON based web application with Erlang and Mochiweb.

Note that, for best performances (HD Definition), you have to directly go to the Vimeo website where the video is stored and watch it in full screen mode.

You may want to download the video. In that case, you will have to follow the instructions at the bottom of the video page on the Vimeo website.

Click on the image below to go the the video page.

Go to the video page

The purpose of all this, aside from arousing your curiosity, is to provide you with a getting started pack and allow you to go a step further in the Erlang web application sphere.

I invite you to download the example application (as illustrated in the video stickyNotes.zip) in order for you to understand all the pieces of the puzzle and to venture behind the scene.

Just keep in mind that stickyNotes is a one shot application that I’ve developed from scratch with the unique aim of illustrating the video example.

Having said that, any comment or suggestion is always welcome, especially about ’struct.erl’, a module that I’ve created when I was learning the language in order to play with the tree structure resulting from the mochijson2 decode function.

We are using it in our mainstream development and I’m still wondering if I might have missed something important with regard to the Erlang distro or Mochiweb itself.

In order to install this stuff on your system and assuming that Erlang is properly setup on your box, just follow these steps:

1) Download stickyNotes and extract the archive in your home directory:

stickyNotes.zip

2) cd to stickyNotes dir and compile the code :

make clean && make

3) Start the application and initialize the db from the Erlang console (only once)

./start-dev.sh
stickydb:reset().

4) Browse the following address 127.0.0.1:8000

Super easy isn’t ?

Enjoy

Web Application on Erlang: Configure Nginx with Mochiweb

September 30, 2008 by Hughes (Erlang)

My previous post, the first of our series dedicated to the Erlang world, was definitively getting too long, so here is the second chapter of this marvelous article.

I’ll assume that you have successfully installed Erlang and Mochiweb and deployed your first small tiny app.

When it comes to deliver static content at lightning speed and do reverse proxy job, there is one name : Nginx, the Igor Sysoev high-performance HTTP server.

It plays well with ssl and in the last dev version you can even find an interesting module for uploading files, nothing less … we will probably tell you more about that in the series.

Nginx is really turning itself into a real handy swiss knife.

Step 0 - SSL packages check

So let’s start by ensuring that the ssl packages are already installed on your box :

foo@bar:~$ sudo apt-get install openssl libssl-dev

Step 1 - Install Nginx

Now, we are ready to download, compile and install Nginx.

Here, we want to install the last stable version (0.6.32).

foo@bar:~$ sudo apt-get build-dep nginx
foo@bar:~$ wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
foo@bar:~$ tar -zxvf nginx-0.6.32.tar.gz
foo@bar:~$ cd nginx-0.6.32
foo@bar:~/nginx-0.6.32$ ./configure –with-http_ssl_module
foo@bar:~/nginx-0.6.32$ make && sudo make install

Done.

Step 2 - Nginx configuration

We do have now to tell Nginx what to proxy, in our case http://my_server_name:8000 (in the previous post, remember?)

You can find the Nginx configuration file, nginx.conf, under /usr/local/nginx/conf/.

Here is a working example one :

Discover the code in Friendpaste

user www-data;

worker_processes  4;
...

http {
    include       mime.types;
    ...

    upstream mochiweb {
        server my_server_name:8000;
    }

    server {
        listen       80;
        server_name  my_server_name;

        ...

        location / {
            root   path_to_myapp/myapp/priv/www;
            index  index.html index.htm;
            ...

            if (!-f $request_filename) {
                proxy_pass http://mochiweb;
                break;
            }

        }

    }

}

Step 3 - Start, Stop Nginx

Finally, we are going to tell Ubuntu how to start and stop the server.

Just create a new file named ‘nginx’ in /etc/init.d and paste the content of this link:

Discover the code in Friendpaste

foo@bar:~/nginx-0.6.32$ cd /etc/init.d
foo@bar:~/etc/init.d$ sudo vi nginx

Above, for those of you who are not in love with vi, you can use nano instead: sudo nano nginx

foo@bar:~/etc/init.d$ sudo chmod +x nginx
foo@bar:~/etc/init.d$ sudo update-rc.d nginx defaults
foo@bar:~/etc/init.d$ sudo ./nginx start

Cool, you have now a working reverse proxy.

foo@bar:~/etc/init.d$ cd ~/myapp
foo@bar:~/myapp$ ./start-dev.sh

Browse this link http://my_server_name

Peace!

As a reminder, the next points we will discuss are:

  • The basic configuration of Postfix (mail)
  • The use of Imagemagick to create dynamically a captcha for your application
  • The configuration of Bind9 in order to play with the url CNAME

So, stay tuned

How To Quickly Set Up Ubuntu 8.04 loaded with Erlang, Mochiweb and Nginx

September 25, 2008 by Hughes (Erlang)

Let’s say you want to give a try to Erlang (Discover our post about Why Erlang?) for your next web development project and you want to be up and running as quickly as possible… you just landed smoothly in the right place.

This post is the starting point of a series of posts in which I’m going to provide you with all the commands you’ll need to set up an Ubuntu 8.04 server loaded with Erlang, Mochiweb proxied by Nginx.

In the same series, I’ll also cover:

  • The basic configuration of Postfix (mail)
  • The use of Imagemagick to create dynamically a captcha for your application
  • The configuration of Bind9 in order to play with the url CNAME

The goal here is not to set up an hardened production server with all the optimizations and security niceties in head (I definitively want to avoid a ‘Why not Gentoo, or Slackware, or Debian, or FreeBSD, …?’, I love them all).

No, it’s all about being able to quickly set up a test server and/or your dev machine in order to dive directly in the development typhoon. And, I must say, Ubuntu(*) is a king at this task.

(*) from a dev point of view, having your dev machine installed with the same OS that your test server is running is definitively a big plus.

You’ve just rent a cheap box from your web host provider, done a fresh Ubuntu 8.04 server installation and you are currently ssh logged in as root … yes, yes, you have done all those things. (In the case you are setting up your desktop machine you can go directly to point 3)

So let’s start!

Step 0 - Upgrade

We ensure with this step that our package sources and our installation is up to date.

root@bar:~$ apt-get update
root@bar:~$ apt-get clean
root@bar:~$ apt-get upgrade

Step 1 - Denyhosts

We install Denyhosts, a Python script used to prevent brute force hacking of our SSH server.

Also, we set RESET_ON_SUCCESS = yes in order to avoid being blocked because we reached the max attempt value.

root@bar:~$ apt-get install denyhosts
root@bar:~$ vi /etc/denyhosts.conf

/RESET_ON_SUCCESS [enter]
i

RESET_ON_SUCCESS = yes

[esc]
:wq

(Read the rest of the article…)

Web Application on Erlang: So Far, So Good, …

September 2, 2008 by Hughes (Erlang)

As a quick recap, I’m building a lightweight server side custom framework which focuses mainly on handling JSON service requests.  This, combined with Mnesia, is our MC stack (Model - Controller).  The client browser will be fully in charge of the ‘V’iew layer of the app.

(See our post Why Erlang ? for more information).

A stable version of the core has been finished a week ago.  In brief, a request hits the server, Nginx proxies it to Mochiweb which starts a new process. 

At this point the framework converts the posted data (JSON) to a native Erlang structure. We check if the request contains one or more services and, based on their name (=:= module), we spawn processes accordingly.

As you may have noticed, in order to save some http requests and to use the full power of our server, the client can ask for an array of services.

i.e. : Let’s imagine you are building a screen which is divided into multiple small independent functional parts : your preferences, your status, your personal info, whatever …, you have the choice to wrap all those service calls into one JSON and the framework will happily process those in parallel … fine isn’t?

At this stage of development, and provided that the DB is not fully populated, if I make a comparison with other developments I’ve done using other languages/architectures, my global feeling is : SPEED.

Whenever I find myself in the situation of using a ‘map’ function and depending on the computation weight on each element, I’m parallelizing the operation (pmap).

Coupled with PURE for the rendering at the client side, we aim to end up with a highly responsive application.

The job now is to continue on that track and make the whole thing even more flexible.

One of my concern when choosing Erlang was the lack of libraries compared to other languages such as Ruby. However, I found relatively easy to develop, from scratch, a mail module responsible to dynamically build messages to be send to one or more addresses (via Postfix) and a ‘captcha’ system.

Soon, I will say a little bit more about those two points and will also describe how to quickly set up an Ubuntu server running Erlang and friends.

Stay tuned

Why Erlang ?

July 24, 2008 by Hughes (Erlang)

Why we chose Erlang over Python, Ruby, PHP family for our back-end.

The short answer :

Because it’s a completely different approach from the OO programming languages (even from the pure functional ones like Lisp or Haskell), it’s concurrent, stateless, has been battle tested in real large-scale industrial products, has an active web app centric community and finally, because it fits perfectly with our view of a light back-end delivery service engine.

Quick links featured in this post :

  • Mochiweb, the web application server we chose
  • Yaws, a web server written in Erlang

When we were looking for our main back-end language back in April, Google had just launched a micro-bomb named GAE, the Google App Engine.  We know all about Google’s killer web apps,  services and brains reservoir, and so it was difficult not to have a closer look at the potential of their brand new SDK.  In brief, GAE is the Google answer to the Amazon S3 in the cloud computing arena. It simply enables you to benefit the same scalable systems than those that power Google applications.

We decided that the server part of our web app should remain light, flexible and be able to deliver chunks of JSON as fast as possible.  The client (browser) would be in charge, among other things, of processing the data and building the final XHTML.

At that time, I was comfortable with Ruby and was following the progresses of the Merb framework project.  But after all, why not change and learn something new ? I love trying out new things, so why not Python, keeping in mind that we could benefit from GAE in the future?  Don’t misunderstand me, I’m still following the development of Merb with a lot of interest :).

So, I started to look at WebPy, Pylons and was getting excited to enter this new community. Coming from Ruby, learning Python, and making a small working prototype using WebPy (soon to be transferred to the GAE SDK) was quite easy.  The goal isn’t to enter in a deep comparison of those two languages, I’m not mastering Python enough anyway. It wasn’t to start a flame war either,  it should have been just the opposite (Ruby2Python or even Python2PHP, …). ALL languages have their pros and cons and those might even change depending on the context.
(Read the rest of the article…)

© 2008 BeeBole | powered by WordPress with Barecity