What should I do to run a Plack application on a server?

E. Choroba

Prague.pm

GoodData

PerlMonks, CPAN, StackOverflow

choroba@matfyz.cz

Motivation

Motivation

 

Rent a virtual server

Operating system

What was available to me:

Fruit market

Installation of Perl & modules

Registering a domain

In most cases, already included in the service.

Masked man with a gun

From the moment my domain was registered, the server began to be bombarded by various requests (HTTP, SSH).

Fortify the server

Fortify the server (2)

Fortify the server (3)

The application

I’ll use the application I run on my server as an example. It lists my Perl talks with slides (and can show the graph we saw in the previous slide).

.
├── cpanfile
├── lib
│   ├── BanGraph.pm
│   ├── MyTemplate.pm
│   ├── NoTrailingSlash.pm
│   └── Talks.pm
├── app.pl
└── talks.in

The application (2)

The application (3)

#! /usr/bin/perl
use warnings;
use strict;

use Plack::Builder;
use FindBin;

use lib './lib';
use Talks;
use BanGraph;
use NoTrailingSlash;
use MyTemplate;

my $root = $FindBin::Bin;

my $app = sub {
    my $env = shift;
    my $status = 200;
    my $headers = ['Content-type' => 'text/html'];

    my $body = [ MyTemplate::page(title => 'E. Choroba',
                                  h1    => 'My Talks',
                                  li    => [Talks::talks($root)]) ];
    return [ $status, $headers, $body ]
};

builder {
    my $dirs_re = Talks::dirs_re($root);

    enable sub { NoTrailingSlash::add_index(shift, $dirs_re) };

    enable 'Static',
        path => sub { s{^/($dirs_re)/$}{$1/index.html} },
        root => $root;

    enable 'Static',
        path => qr{^/$dirs_re/.},
        root => $root;

    mount '/ban.png' => sub { BanGraph::png($root) };

    mount '/' => $app;
};

The application (4)

No Trailing Slash

Unfortunately, this doesn’t work:

enable 'Static',
    path => sub { s{^/($dirs_re)/$}{$1/index.html} },
    root => $root;

The application (4)

No Trailing Slash

Unfortunately, this doesn’t work:

enable 'Static',               # ↓ #
    path => sub { s{^/($dirs_re)/?$}{$1/index.html} },
    root => $root;

It breaks relative links.

The application (5)

No Trailing Slash

package NoTrailingSlash;
use Plack::Request;

sub add_index {
    my ($app, $dirs_re) = @_;
    sub {
        my ($env) = @_;

        my $req = 'Plack::Request'->new($env);
        my $path = $req->path;
        return [ 301, [ Location => "$1/index.html" ], [] ]
            if $path =~ m{^/($dirs_re)$};

        my $res = $app->($env);
        $res
    }
}

__PACKAGE__

Enabling HTTPS

Running under HTTPS

starman --port 5000             \
        --access-log access.log \
        --ssl-key domain.key    \
        --ssl-cert domain.crt   \
        --enable-ssl            \
        app.pl &> https.log

Wrap up

To run an application on a virtual server, you need

Thank you

https://e-choroba.eu/21-plack/

Thank you

https://e-choroba.eu/21-plack