Getting Started with Raku

Now that my little textboard project is in a decent state I've begun thinking about what I wanted to try next. I knew it'll be some sort of webbed site and there'd be a database but other than that it was up in the air. Mojo.js appealed to me for obvious reasons but honestly I found the Node/JavaScript conventions and ecosystem pretty intimidating. I've never been any good at JS and the whole front-end thing is an enigma to me, as is probably obvious if you've seen my prior art.

Thankfully I discovered Humming-Bird which I like because it reminds me of Mojo or Sinatra. It's also a great excuse to use Raku, a language I also find intimidating but at least feels more 'fun' and less 'enterprise' than JS or even Perl/Ruby. If you already have a nice little CPAN/Perl set-up then getting Raku is easy enough:

cpanm App::Rakubrew && rakubrew download

If not then Debian ships it:

apt install rakudo

Finally we can use zef to install Humming-Bird:

zef install Humming-Bird

Humming-Bird is quite minimal like Sinatra is so I want to install something for templates. Template::Mustache was recommended to me after I had trouble getting others to work:

zef install Template::Mustache

To get started let's import Humming-Bird::Core and our template module; I'm going to keep my template in a directory called templates:

#!/usr/bin/env raku

use v6.d;
use Humming-Bird::Core;
use Template::Mustache;

my $template = Template::Mustache.new: :from<./templates>;

I'll go ahead and throw in a basic template named index.mustache:

<!DOCTYPE html>
<html>
<head>
  <title>{{title}}</title>
</head>
<body>
<h1>{{title}}</h1>
<p>We will we will... RAKU!!</p>
</body>
</html>

To render this template we only need to add the following to our script, I've named mine basic-site.raku if you're playing along at home:

get '/', -> $request, $response {
    my Str %stash = title => 'Hello, web!';

    $response.html($template.render: 'index', %stash);
};

listen 3000;

That is literally all you need to take flight:

$ curl -i http://localhost:3000
HTTP/1.1 200 OK
Content-Length: 144
Date: Fri, 10 Nov 2023 02:29:44 +0000
X-Server: Humming-Bird (Raku)
content-type: text/html; charset=utf8

<!DOCTYPE html>
<html>
<head>
  <title>Hello, web!</title>
</head>
<body>
<h1>Hello, web!</h1>
<p>We will we will... RAKU!!</p>
</body>
</html>

This is pretty decent as-is but if there's anything I create more than silly websites, it's compiler errors and warnings. I'd like them to be more verbose so I can also add this:

# Add the 'middleware' and 'advice' parts
use Humming-Bird::Middleware;
use Humming-Bird::Advice;

# All you need to use 'em out-of-the-box
middleware &middleware-logger;
advice     &advice-logger;

Our entire script now looks like this:

#!/usr/bin/env raku

use v6.d;
use Humming-Bird::Core;
use Humming-Bird::Middleware;
use Humming-Bird::Advice;
use Template::Mustache;

middleware &middleware-logger;
advice     &advice-logger;

my $template = Template::Mustache.new: :from<./templates>;

get '/', -> $request, $response {
    my Str %stash = title => 'Hello, web!';

    $response.html($template.render: 'index', %stash);
};

listen 3000;

You can take a better look at it in my git repo along with my other Raku experiments so far. I'm glad Humming-Bird arrived on the scene as it's left me with no more excuses to not at least try Raku and so far I'm really diggin it. I've found the best sources of Raku halp to be Reddit and their Discord (which is bridged to IRC as well); you can get your hyperlinks on their website.

#webDev #noScript #RakuLang