1 package Web::Simple::Application;
5 has 'config' => (is => 'ro', trigger => sub {
6 my ($self, $value) = @_;
7 my %default = $self->_default_config;
8 my @not = grep !exists $value->{$_}, keys %default;
9 @{$value}{@not} = @default{@not};
12 sub default_config { () }
14 has '_dispatcher' => (is => 'lazy');
16 sub _build__dispatcher {
18 require Web::Dispatch;
19 require Web::Simple::DispatchNode;
20 my $final = $self->_build_final_dispatcher;
22 app => sub { $self->dispatch_request(@_), $final },
23 node_class => 'Web::Simple::DispatchNode',
24 node_args => { app_object => $self }
28 sub _build_final_dispatcher {
29 [ 404, [ 'Content-type', 'text/plain' ], [ 'Not found' ] ]
33 # ->as_psgi_app is true for require() but also works for plackup
34 return $_[0]->to_psgi_app if caller(1);
35 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
41 require Plack::Server::CGI;
42 Plack::Server::CGI->run($self->as_psgi_app);
47 require Plack::Server::FCGI;
48 Plack::Server::FCGI->run($self->as_psgi_app);
52 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
53 $self->_dispatcher->to_app;
58 if ($ENV{PHP_FCGI_CHILDREN} || $ENV{FCGI_ROLE} || $ENV{FCGI_SOCKET_PATH}) {
59 return $self->_run_fcgi;
60 } elsif ($ENV{GATEWAY_INTERFACE}) {
61 return $self->_run_cgi;
63 unless (@ARGV && $ARGV[0] =~ m{^/}) {
64 return $self->_run_cli(@ARGV);
67 my $path = shift @ARGV;
69 require HTTP::Request::Common;
71 local *GET = \&HTTP::Request::Common::GET;
73 my $request = GET($path);
75 Plack::Test::test_psgi($self->as_psgi_app, sub { $response = shift->($request) });
76 print $response->as_string;
81 die $self->_cli_usage;
85 "To run this script in CGI test mode, pass a URL path beginning with /:\n".