Created
February 25, 2016 21:03
-
-
Save dr-kd/4d2d031faa089d24620f to your computer and use it in GitHub Desktop.
A plack development server for existing cgi scripts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File Edit Options Buffers Tools Perl Help | |
#!/usr/bin/env perl | |
use warnings; | |
use strict; | |
use Try::Tiny; | |
use lib 'lib'; | |
use Plack::Middleware::SSI; | |
use CGI::Emulate::PSGI; | |
use CGI::Compile; | |
use Plack::App::URLMap; | |
use Plack::App::Directory; | |
use File::Basename; | |
use Plack::Middleware::DirIndex; | |
use Plack::MIME; | |
my $base = dirname __FILE__; | |
my @apps = ( | |
{ | |
file => 'some/cgi/script.pl' | |
paths => [ qw( /myapp /myapp.cgi )], | |
}, | |
{ | |
file => 'some/cgi/script2.pl', | |
paths => [qw( /some_path )], | |
}, | |
); | |
my $main = Plack::App::URLMap->new; | |
foreach my $app (@apps) { | |
my $script = CGI::Compile->compile($base . '/' . $app->{file}); | |
my $handler = CGI::Emulate::PSGI->handler($script); | |
$main->map($_ => $handler) for @{$app->{paths}}; | |
} | |
# Static server for develepment environment | |
my $static = Plack::App::Directory->new({ root => '/path_to_static_resources', | |
content_type => sub { | |
my ($path) = @_; | |
my $ct; | |
if ($path =~ /\.shtml/) { | |
$ct = 'text/html'; | |
} | |
else { | |
$ct = Plack::MIME->mime_type($path); | |
} | |
return $ct; | |
} | |
})->to_app; | |
$static = Plack::Middleware::DirIndex->wrap($static, dir_index => 'index.shtml'); | |
# And for ancient crufty scripts with Server side includes :D | |
$static = Plack::Middleware::SSI->wrap($static); | |
$main->map('/' => $static); | |
$main->to_app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment