summaryrefslogtreecommitdiffstats
path: root/bin/preview
blob: aebeaa379baa7abb47cd52a8638bc9fe35dad830 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env perl
use strict;
use warnings;

use Plack::App::Directory;
use Plack::Builder;
use Plack::Request;
use Plack::Response;
use Plack::Runner;

my $site_root = 'site';
my $dir_app = Plack::App::Directory->new(root => $site_root)->to_app;
my $app = sub {
    my ($env) = @_;
    my $req = Plack::Request->new($env);
    if (-d ("$site_root" . $req->path)) {
        my $resp = Plack::Response->new(302);
        my $path = $req->path;
        $path .= '/' unless $path =~ m:/$:;
        $resp->location("${path}index.html");
        return $resp->finalize;
    }
    else {
        return $dir_app->($env);
    }
};

my $runner = Plack::Runner->new;
$runner->parse_options(@ARGV);
$runner->run($app);