Intro to Mojolicious

Starting from scratch: Mojolicious::Lite

Learning the basics

Routes

# The main landing page; index.html
get '/' => sub {
    my ($c)     = @_;
    my $count   = format_number time; # Grab epoch and add commas
    my $fortune = `/usr/games/fortune` || `fortune` || "huh??\n";

    $c->stash(
        count   => $count,
        fortune => $fortune
        );

    $c->render();
} => 'index';

Minimal routes

get '/about';

Placeholders

get '/:route';

Restrictive placeholder

# Default route
get '/:route' => [route => [qw{die me news portal}]] => sub {
    my ($c) = @_;

    $c->render(template => $c->stash('route'));
};

Sessions

my ($c)         = @_;
my $sessionLife = 604800;

if ($c->cookie('banner') eq 'seen') {
    # Set session for a week
    $c->session(expiration => $sessionLife, banner => 'seen');
    # Kill plain-text cookie
    $c->cookie(banner => 'seen', {expires => 1});
}

# Pass in the expiration for plain-text cookie
$c->stash(sessionLife => $sessionLife);

Plugins

plugin 'Config';

# CGI scripts
plugin CGI => ['/cgi-bin/guest.cgi'  => './cgi-bin/guest_mm.cgi'];
plugin CGI => ['/cgi-bin/whoami.cgi' => './cgi-bin/whoami.cgi'  ];

Config plugin

Under

# Handle the session
under sub {
    my ($c)         = @_;
    my $sessionLife = 604800;

    if ($c->cookie('banner') eq 'seen') {
        # Set session for a week
        $c->session(expiration => $sessionLife, banner => 'seen');
        # Kill plain-text cookie
        $c->cookie(banner => 'seen', {expires => 1});
    }

    # Pass in the expiration for plain-text cookie
    $c->stash(sessionLife => $sessionLife);

    1;
};

Another good use for under

# /foo
under '/foo';

# /foo/bar
get '/bar' => {text => 'foo bar'};

# /foo/baz
get '/baz' => {text => 'foo baz'};

# / (reset)
under '/' => {msg => 'whatever'};

# /bar
get '/bar' => {inline => '<%= $msg %> works'};

Templates

% title 'Swagg::Net ULA Tool';
% layout 'swagg-iframe';
<% if ($ula6) { %>
<p>Your IPv6 ULA prefix is <%= $ula6 %>.</p>
<% } else { %>
<form action="/ula6" method="get">
  MAC address:<br>
  <input type="text" name="macaddr"><br>
  <br>
  <input type="submit" value="Submit">
</form>
<% } %>

Layouts

<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title><%= title %></title>
  <link rel="stylesheet" type="text/css" href="/css/swagg.css">
</head>
<body>
<%= content %>
</body>
</html>

Layout + sessions = my banner

<% unless (session('banner') eq 'seen') { %>
<!-- Soundtrack -->
<!-- <embed> doesn't work anymore SAD
<embed src="/misc/Smashmouth_-_All_Star.mid" width="100%" height="60">-->
<audio id="soundtrack" src="/Music/Smashmouth-All-Star.mp3" preload="auto">
</audio>
<!-- "GDPR" banner -->
<div id="gdpr">
  <b>Notice:</b> This site uses MIDI technology instead of tracking cookies.
  <img alt="a compact disc playing music" src="/Pictures/Music_CD.gif"
       style="vertical-align: bottom"><br>
  <br>
  <button class="win95button" onclick="playIt();"><u>O</u>K</button>
  <button class="win95button" onclick="closeIt();"><u>C</u>ancel</button>
</div>
<script>
  function closeIt() {
      document.getElementById("gdpr").style.display = "none";
      document.cookie = "banner=seen; <%= $sessionLife %>;";
  }

  function playIt() {
      document.getElementById("soundtrack").play();
      closeIt();
  }
</script>
<% } %>

TODO

Goodbye

# Delete whole session by setting an expiration date in the past
$c->session(expires => 1);