WebDyne
view release on metacpan or search on metacpan
bin/webdyne.psgi view on Meta::CPAN
# the same terms as the Perl 5 programming language system itself.
#
# Full license text is available at:
#
# <http://dev.perl.org/licenses/>
#
package WebDyne::Request::PSGI::Run;
# Compiler Pragma
#
use strict qw(vars);
use vars qw($VERSION);
use warnings;
no warnings qw(uninitialized);
# External Modules
#
use HTTP::Status qw(:constants is_success is_error);
use IO::String;
use Data::Dumper;
use Cwd qw(cwd);
# WebDyne Modules
#
use WebDyne;
use WebDyne::Constant;
use WebDyne::Util;
use WebDyne::Request::PSGI;
use WebDyne::Request::PSGI::Constant;
# Version information
#
$VERSION='2.075';
# API file name cache
#
our (%API_fn);
# Test file to use if no DOCUMENT_ROOT found
#
(my $test_dn=$INC{'WebDyne.pm'})=~s/\.pm$//;
my $test_fn=File::Spec->catfile($test_dn, 'time.psp');
# Set DOCUMENT_DEFAULT
#
$DOCUMENT_DEFAULT=$ENV{'DOCUMENT_DEFAULT'} || $DOCUMENT_DEFAULT;
# All done. Start endless loop if called from command line or return
# handler code ref.
#
if (!caller || exists $ENV{PAR_TEMP}) {
# Running from command line without being stared by plackup or starman
#
require Plack::Runner;
my $plack_or=Plack::Runner->new();
# User specified --test on command line ? Note and consume before
# passing to parse_options ?
#
my $test_fg;
if ($test_fg=grep {/^--test$/} @ARGV) {
@ARGV=grep {!/^--test$/} @ARGV;
}
# Used to do --static as option, now default, change to negate option, i.e. always
# serve static files for convenience when started from the command line, same with noindex
#
my $nostatic_fg;
if ($nostatic_fg=grep {/^--nostatic$/} @ARGV) {
@ARGV=grep {!/^--nostatic/} @ARGV;
}
my $noindex_fg;
if ($noindex_fg=grep {/^--noindex$/} @ARGV) {
@ARGV=grep {!/^--noindex/} @ARGV;
}
# Mac conflicts with Plack default port of 5000 - choose 5001
#
if ($^O eq 'darwin') {
$plack_or->parse_options('--port', '5001', @ARGV);
}
else {
$plack_or->parse_options(@ARGV);
}
# Finalise DOCUMENT_ROOT. First try and get as last command line option or env or variable but --test
# flag wins over everything else
#
$DOCUMENT_ROOT=shift(@{$plack_or->{'argv'}}) ||
$ENV{'DOCUMENT_ROOT'} || $DOCUMENT_ROOT;
#if ($test_fg || !$DOCUMENT_ROOT) {
# $DOCUMENT_ROOT=$test_fn;
#}
if ($test_fg) {
$DOCUMENT_ROOT=$test_fn;
}
elsif(! $DOCUMENT_ROOT) {
$DOCUMENT_ROOT=cwd();
}
$DOCUMENT_ROOT=&normalize_dn($DOCUMENT_ROOT);
# Indexing ? Do by default unless file specified as DOCUMENT_ROOT or --noindex spec'd etc.
#
unless (-f $DOCUMENT_ROOT || -f File::Spec->catfile($DOCUMENT_ROOT, $DOCUMENT_DEFAULT) || $noindex_fg) {
# Final check. Only do if directory
#
bin/webdyne.psgi view on Meta::CPAN
}
else {
# Not running from comamnd line. Get DOCUMENT_ROOT from environment or
# vars file
#
$DOCUMENT_ROOT=$ENV{'DOCUMENT_ROOT'}
|| $DOCUMENT_ROOT || $test_fn;
$DOCUMENT_ROOT=&normalize_dn($DOCUMENT_ROOT);
# Read in local webdyne.conf.pl
#
&local_constant_load($DOCUMENT_ROOT);
}
# Return handler code ref
#
&handler_build($WEBDYNE_PSGI_STATIC ? &handler_static(\&handler) : \&handler);
#==================================================================================================
sub normalize_dn {
# Normal dir, normally document_root
#
my $rel_dn=shift();
my $abs_dn=File::Spec->rel2abs($rel_dn);
$abs_dn =~ s{/$}{} unless $abs_dn eq '/';
return $abs_dn;
}
sub local_constant_load {
# Read in local webdyne.conf.pl
#
my $root_dn=shift();
# If root_dn is a file get dir name
if (-f $root_dn) {
$root_dn=(File::Spec->splitpath($root_dn))[1];
}
WebDyne::Constant->import(File::Spec->catfile($root_dn, sprintf('.%s', $WEBDYNE_CONF_FN)));
}
# Build a Plack::Build ref if there is middleware requested
#
sub handler_static {
# Used when starting webdyne.psgi from command line without plackup or via starman - presumably for dev
# purposes so include the Plack static middleware to allow serving non-psp files such as css
#
my ($handler_cr, @param)=@_;
if (my $qr=$WEBDYNE_PSGI_MIDDLEWARE_STATIC) {
my $root_dn;
if (-f $DOCUMENT_ROOT) {
# DOCUMENT_ROOT is actually a file. Get the directory name
#
require File::Basename;
$root_dn=&File::Basename::dirname($DOCUMENT_ROOT)
}
else {
# DOCUMENT_ROOT is a dirname, keep but check
$root_dn=$DOCUMENT_ROOT;
(-d $root_dn) || return err("$root_dn is not a directory, aborting");
}
require Plack::Middleware::Static;
$handler_cr=Plack::Middleware::Static->wrap($handler_cr, path=>$qr, root=>$root_dn );
}
return $handler_cr;
}
sub handler_build {
# Check for any additional Plack middleware handlers requested in config and wrap them if needed
#
my ($handler_cr, @param)=@_;
if (my $middleware_ar=$WEBDYNE_PSGI_MIDDLEWARE) {
# Yes, middleware requested
#
foreach my $middleware_hr (@{$middleware_ar}) {
while (my ($middleware, $opt_hr)=each %{$middleware_hr}) {
if ($middleware !~ /^Plack::Middleware/) {
$middleware = "Plack::Middleware::${middleware}";
}
(my $middleware_pm = $middleware) =~ s{::}{/}g;
$middleware_pm.='.pm';
eval { require $middleware_pm } ||
return err("error loading Plack middleware: $middleware ($middleware_pm), $@");
if (ref($opt_hr) eq 'CODE') {
# If opt_hr is code ref means we want DOCUMENT_ROOT built in
$opt_hr=$opt_hr->($DOCUMENT_ROOT);
}
$handler_cr=$middleware->wrap($handler_cr, %{$opt_hr});
}
}
}
#if ($WEBDYNE_PSGI_ENV_KEEP || $WEBDYNE_PSGI_ENV_SET) {
# require Plack::Middleware::ForceEnv;
# $handler_cr=Plack::Middleware::ForceEnv->wrap($handler_cr,
# %{$WEBDYNE_PSGI_ENV_SET},
# map {$_=>$ENV{$_}} @{$WEBDYNE_PSGI_ENV_KEEP}
# )
#}
return $handler_cr;
}
( run in 1.206 second using v1.01-cache-2.11-cpan-e93a5daba3e )