Day 13 – Web server from scratch with Cro and Debian

I talked to Santa Claus and he said he does not know how to install Cro on Debian, so I said to myself: I’m going to help him out.

If you have some experience with web servers like Apache, and you have heard about the powerful concurrent/reactive aspects of Perl6, surely you are interested to know about Cro Services. The scope of this post are people with basic experience in Debian and basic experience or none in Perl6… like Santa.

Cro is a Perl6 module that gives everything you need to build reactive and concurrent services easily, for example: a web server.

In this post we will see how to install Cro from scratch in Debian, one of the most popular Linux distributions. Then, I will explain the demo example of Cro.

We will use Debian 9, 64-bit (Stretch) and we will start once it is installed.

Installing Rakudo Perl6 compiler

Rakudo is the current Perl6 compiler where the Cro module runs. The regular way to install Rakudo is Rakudo Star, but I prefer the fast way: rakudo-pkg… how? just downloading and installing the corresponding Debian package from this repo. In our case, is the .deb file from Debian 9, 64-bit.

Using the root user in Debian we can create a packages folder for Rakudo in the root home, enter it, download the current Rakudo package for Debian 9, 64-bit and install it. In my case:

mkdir ~/rakudo-packages && cd $_
wget https://github.com/nxadm/rakudo-pkg/releases/download/v2018.10-02/rakudo-pkg-Debian9_2018.10-02_amd64.deb
dpkg -i rakudo-pkg-Debian9_2018.10-02_amd64.deb

Rakudo run-time compiler and related tools are now installed in /opt/rakudo-pkg folder. Let’s make it available for all users inserting the line below in /etc/profile file, just before export PATH line:

 PATH=$PATH:/opt/rakudo-pkg/bin

Finally run:

 source /etc/profile

to reload the Debian profile for all users.

Type perl6 -v:

perl6 -v
This is Rakudo version 2018.10 built on MoarVM version 2018.10
implementing Perl 6.c.

Welcome to Rakudo Perl6!

Installing Cro Services

Cro is a Perl6 module and Zef is the current Perl6 modules manager that is already installed. Let’s install Cro!

First we will install some Cro package dependencies, like git to connect and download files from Cro related repositories, libssl-dev to provide support of secure sockets layer and build-essential to build some dependencies used by Cro during its installation:

apt-get install git libssl-dev build-essential

If you are under a firewall that only allow web traffic (ports 80 and 443), it will block the port used by git protocol and the Cro installation will fail. To avoid this, type:

git config --global url."https://".insteadOf git://

to tell git use https instead git protocol to connect with git remote repos.

Now we are ready to install Cro with Zef (and all of its dependencies):

zef install cro

If the installation fails during test phase, try with:

zef install --/test cro

If Cro installation completes correctly, Cro is ready.

Cro in action

Let’s continue with the demo script. Create a workspace folder, enter it and paste the code below into a new file named server.p6:

use Cro::HTTP::Router;
use Cro::HTTP::Server;

my $application = route {
  get -> 'greet', $name {
  content 'text/plain', "Hello, $name!";
  }
}

my Cro::Service $hello = Cro::HTTP::Server.new(:host<0.0.0.0>, :port<10000>, :$application);

$hello.start;

react whenever signal(SIGINT) { $hello.stop; exit; }

This script has 4 parts:

use makes available the Router and Server classes to use below.

$application is the object that contains the routing logic of our web application, receiving and distributing the data from the client to our server. In this case get -> ‘greet’, $name maps the incoming URL /greet/Ramiro from a client web browser pushing Ramiro in the object $name. Then the code into curly braces { } returns the response HTTP header content ‘text/plain’ and HTTP body Hello, Ramiro! to the client web browser. In a serious application, in this part there would be a call to the application itself and it would wait for the answer to responding like the example.

$hello is a service object that makes the incoming data to be delivered to our $application through a new listening socket. It have 3 parameters, :host<0.0.0.0> is the localhost from the server starts, :port<10000> is the port used to listen the incoming data and $application is our web application. The line below $hello.start begins the listening.

react whenever… waits for CTRL-C to stop the web server.

Now is the moment to run the web server from the shell:

perl6 server.p6

Now you need to know the current IP address from the server host, try with ip addr. In my case is 192.168.1.48.

Then, from other host in the same network, open a web browser and type (in my case):

http://192.168.1.48:10000/greet/Ramiro

the answer should be Hello, Ramiro!

Summary

Starting from a fresh installation of Debian we have seen how to install Cro and run the demo script. Now you are ready to continue with the Cro Documentation and discover the most promising concurrent/async services platform that Perl6 can offer.

I hope that after reading this post and having a look at the Cro documentation, Santa Claus can migrate his website to Cro Services.

One thought on “Day 13 – Web server from scratch with Cro and Debian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.