Bailador For Beginners

 

Lance Wicks

lancew@cpan.org

Welcome

Hi !

This is a workshop for beginners, no advanced content here.

 

We will go from zero to full site up, running and ready for users.

 

 

Overview:

  • Development Environment
  • Getting started
  • Static Content
  • Initial Routes
  • User Registration
  • Login
  • Sessions
  • Databases
  • Routes

Git Repo

All steps including a virtual box appliance with all pre-requisites:

 

3 x USB Sticks are available

Windows

You might want to try installing Perl6 with chocolatey


choco install rakudostar

zef install Bailador

Linux

apt get rakudo

 

packer -S rakudo

 

From your distro

Install Modules

  • zef install Bailador

  • zef install DBIish
  • zef install Crypt::Bcrypt

Hello world

Lets get something working!

hello_london.pl6

use v6;
use Bailador;

get '/' => sub {
    "hello london"
}

baile();

c63e1081b71706244fe8a594d060402f22ae49d6

hello_london.pl6

$ bailador watch hello_london.pl6

 

or

 

$ perl6 hello_london.pl6

 

LPW2017 App

$ bailador new lpw2017

 

08153d854eedbe8f881077fbad05070e739ea153

Directories

/bin/app.pl6

/views

/t

Template Variables

<%= $foo %>

 

LPW2017 App

Lets make it look a bit prettier and serve some static content:

 

/static

/static/favicon.ico

/static/css

/static/js

 

   
static-dir / (.+) / => 'static/';

 

 

 

03b74e0101f74986e7d3961929b0f6a3fe4c7993

LPW2017 App

Start using a layout

 

/layout

/layout/main.tt

 

Simpler template

81ea6f5976fb7b3618736c485609c99230540a09

LPW2017 App

Create a login route

 

+post '/login' => sub {
+    my %params = request.params;
+    if (
+        %params<email> eq 'lancew@cpan.org'
+        && %params<password> eq 'london'
+    ) {
+        return 'Logged in';
+    } else {
+        return 'not logged in';
+    }
+};

 

46f960bd6ed4a0cdda2d165281c361445ba460b2

LPW2017 App

Sessions

 

kludge to show login or logout depending on session.

 

Perl6 in templates:

 

+% use Bailador;
+% my $session = session;
+        % if ( $session<user_email>:exists ) {

 

dbf1a07368b5bb06b1e6f3a5093fb33026978c34

LPW2017 App

/register route


+    my $dbh = DBIish.connect("SQLite", :database<db/lpw2017.db>);
+
+    my $sth = $dbh.prepare(q:to/STATEMENT/);
+        INSERT INTO users
+            (username,password_hash)
+            VALUES (?,?)
+        STATEMENT
+
+    my $hash = bcrypt-hash(%params<password>);
+    $sth.execute(%params<email>,$hash);

 

401359f9c7da2ac5232834a8ebbdd41a11651fa4

LPW2017 App

Database a bcrypt based login

 

+           if ( bcrypt-match(%params<password>, $hash) ) {
+               $password_is_correct = True;
+           }

 

 

dfedd783db1e8ca4d347cf384a86ba1c9adc2f3b

LPW2017::Bailador

By Lance Wicks

LPW2017::Bailador

Slides for my 2017 workshop at the London Perl Workshop "Bailador for Beginners".

  • 1,803