| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Path::Tiny qw( path ); |
| 6 |
use Getopt::Long qw( GetOptions ); |
| 7 |
use URI; |
| 8 |
use URI::Escape qw( uri_unescape ); |
| 9 |
use JSON::PP qw( encode_json decode_json ); |
| 10 |
use HTTP::Server::PSGI; |
| 11 |
use Plack::Builder; |
| 12 |
use Plack::App::Directory; |
| 13 |
|
| 14 |
|
| 15 |
my $daemon = 0; |
| 16 |
my $kill = 0; |
| 17 |
my $host = 'localhost'; |
| 18 |
|
| 19 |
GetOptions( |
| 20 |
"d" => \$daemon, |
| 21 |
"k" => \$kill, |
| 22 |
"host=s" => \$host, |
| 23 |
); |
| 24 |
|
| 25 |
my $bindir = path(__FILE__)->parent->absolute; |
| 26 |
my $distdir = $bindir->parent->parent; |
| 27 |
|
| 28 |
my $config_file = $bindir->child('httpd.json'); |
| 29 |
|
| 30 |
if(-r $config_file) |
| 31 |
{ |
| 32 |
my $config = decode_json($config_file->slurp); |
| 33 |
my $pid = $config->{pid}; |
| 34 |
if(defined $pid) |
| 35 |
{ |
| 36 |
kill 'KILL', $pid; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
exit if $kill; |
| 41 |
|
| 42 |
if($daemon) |
| 43 |
{ |
| 44 |
require Proc::Daemon; |
| 45 |
my $daemon = Proc::Daemon->new( |
| 46 |
child_STDOUT => $bindir->child('httpd.log')->stringify, |
| 47 |
child_STDERR => $bindir->child('httpd.log')->stringify, |
| 48 |
); |
| 49 |
$daemon->Init; |
| 50 |
} |
| 51 |
|
| 52 |
my $url = URI->new('http://localhost/corpus/dist/'); |
| 53 |
$url->host($host); |
| 54 |
$url->port(do { |
| 55 |
require IO::Socket::INET; |
| 56 |
IO::Socket::INET->new(Listen => 5, LocalAddr => "127.0.0.1")->sockport; |
| 57 |
}); |
| 58 |
|
| 59 |
my %config = ( |
| 60 |
root => $distdir->child('corpus/dist')->stringify, |
| 61 |
pid => $$, |
| 62 |
url => $url->as_string, |
| 63 |
); |
| 64 |
$config_file->spew(encode_json(\%config)); |
| 65 |
|
| 66 |
my $app = builder { |
| 67 |
mount '/corpus/dist/test1/' => sub { |
| 68 |
my $env = shift; |
| 69 |
my %headers; |
| 70 |
foreach my $key (keys %$env) |
| 71 |
{ |
| 72 |
next unless $key =~ /^HTTP_(.*)$/; |
| 73 |
my $name = join '-', map { ucfirst $_ }map { lc $_ } split /_/, $1; |
| 74 |
$headers{$name} = $env->{$key}; |
| 75 |
} |
| 76 |
my $uri = URI->new($env->{'psgi.url_scheme'} . '://' . $env->{SERVER_NAME} . uri_unescape($env->{REQUEST_URI}), $env->{'psgi.url_scheme'}); |
| 77 |
$uri->port($env->{SERVER_PORT}); |
| 78 |
|
| 79 |
my %query; |
| 80 |
my @query = $uri->query_form; |
| 81 |
while(@query) |
| 82 |
{ |
| 83 |
my $key = shift @query; |
| 84 |
my $value = shift @query; |
| 85 |
push @{ $query{$key} }, $value; |
| 86 |
} |
| 87 |
|
| 88 |
return [ |
| 89 |
'200', |
| 90 |
[ 'Content-Type' => 'text/plain; charset=UTF-8' ], |
| 91 |
[ encode_json( { headers => \%headers, url => { scheme => $uri->scheme, host => $uri->host, port => $uri->port, path => $uri->path, query => \%query } } ) ], |
| 92 |
]; |
| 93 |
}; |
| 94 |
mount '/corpus/dist/about.json' => sub { |
| 95 |
my $env = shift; |
| 96 |
return [ |
| 97 |
'200', |
| 98 |
[ 'Content-Type' => 'application/json' ], |
| 99 |
[ encode_json( { ident => 'AB Test HTTPd' } ) ], |
| 100 |
]; |
| 101 |
}; |
| 102 |
mount '/' => Plack::App::Directory->new({ root => $distdir->stringify })->to_app; |
| 103 |
}; |
| 104 |
|
| 105 |
my $server = HTTP::Server::PSGI->new( |
| 106 |
host => $url->host, |
| 107 |
port => $url->port, |
| 108 |
); |
| 109 |
|
| 110 |
$server->run($app); |