1 package Web::Simple::Application;
9 +{ $self->default_config }
12 my ($self, $value) = @_;
13 my %default = $self->default_config;
14 my @not = grep !exists $value->{$_}, keys %default;
15 @{$value}{@not} = @default{@not};
19 sub default_config { () }
21 has '_dispatcher' => (is => 'lazy');
23 sub _build__dispatcher {
25 require Web::Dispatch;
26 require Web::Simple::DispatchNode;
27 my $final = $self->_build_final_dispatcher;
29 app => sub { $self->dispatch_request(@_), $final },
30 node_class => 'Web::Simple::DispatchNode',
31 node_args => { app_object => $self }
35 sub _build_final_dispatcher {
36 [ 404, [ 'Content-type', 'text/plain' ], [ 'Not found' ] ]
40 # ->to_psgi_app is true for require() but also works for plackup
41 return $_[0]->to_psgi_app if caller(1);
42 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
48 require Plack::Handler::CGI;
49 Plack::Handler::CGI->new->run($self->to_psgi_app);
54 require Plack::Handler::FCGI;
55 Plack::Handler::FCGI->new->run($self->to_psgi_app);
59 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
60 $self->_dispatcher->to_app;
66 $ENV{PHP_FCGI_CHILDREN} || $ENV{FCGI_ROLE} || $ENV{FCGI_SOCKET_PATH}
67 || -S STDIN # STDIN is a socket, almost certainly FastCGI
69 return $self->_run_fcgi;
70 } elsif ($ENV{GATEWAY_INTERFACE}) {
71 return $self->_run_cgi;
73 unless (@ARGV && $ARGV[0] =~ m{^[A-Z/]}) {
74 return $self->_run_cli(@ARGV);
79 unshift(@args, 'GET') if $args[0] =~ m{^/};
81 $self->_run_cli_test_request(@args);
84 sub _test_request_spec_to_http_request {
85 my ($self, $method, $path, @rest) = @_;
87 # if it's a reference, assume a request object
88 return $method if ref($method);
90 my $request = HTTP::Request->new($method => $path);
94 while (my ($header, $value) = splice(@rest, 0, 2)) {
95 unless ($header =~ s/:$//) {
96 push @params, $header, $value;
98 $request->headers->push_header($header, $value);
101 if (($method eq 'POST' or $method eq 'PUT') and @rest) {
104 my $url = URI->new('http:');
105 $url->query_form(@rest);
108 $request->header('Content-Type' => 'application/x-www-form-urlencoded');
109 $request->header('Content-Length' => length($content));
110 $request->content($content);
116 sub run_test_request {
117 my ($self, @req) = @_;
119 require HTTP::Request;
122 my $request = $self->_test_request_spec_to_http_request(@req);
124 Plack::Test::test_psgi(
125 $self->to_psgi_app, sub { shift->($request) }
129 sub _run_cli_test_request {
130 my ($self, @req) = @_;
131 my $response = $self->run_test_request(@req);
133 binmode(STDOUT); binmode(STDERR); # for win32
135 print STDERR $response->status_line."\n";
136 print STDERR $response->headers_as_string("\n")."\n";
137 my $content = $response->content;
138 $content .= "\n" if length($content) and $content !~ /\n\z/;
139 print STDOUT $content if $content;
144 die $self->_cli_usage;
148 "To run this script in CGI test mode, pass a URL path beginning with /:\n".
158 Web::Simple::Application - A base class for your Web-Simple application
162 This is a base class for your L<Web::Simple> application. You probably don't
163 need to construct this class yourself, since L<Web::Simple> does the 'heavy
164 lifting' for you in that regards.
168 This class exposes the following public methods.
170 =head2 default_config
172 Merges with the C<config> initializer to provide configuration information for
173 your application. For example:
178 posts_dir => $FindBin::Bin.'/posts',
182 Now, the C<config> attribute of C<$self> will be set to a HashRef
183 containing keys 'title' and 'posts_dir'.
185 The keys from default_config are merged into any config supplied, so
186 if you construct your application like:
188 MyWebSimpleApp::Web->new(
189 config => { title => 'Spoon', environment => 'dev' }
192 then C<config> will contain:
196 posts_dir => '/path/to/myapp/posts',
202 The run_if_script method is designed to be used at the end of the script
203 or .pm file where your application class is defined - for example:
205 ## my_web_simple_app.pl
207 use Web::Simple 'HelloWorld';
212 sub dispatch_request {
214 [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ]
217 [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
222 HelloWorld->run_if_script;
224 This returns a true value, so your file is now valid as a module - so
226 require 'my_web_simple_app.pl';
228 my $hw = HelloWorld->new;
230 will work fine (and you can rename it to lib/HelloWorld.pm later to make it
231 a real use-able module).
233 However, it detects if it's being run as a script (via testing $0) and if
234 so attempts to do the right thing.
236 If run under a CGI environment, your application will execute as a CGI.
238 If run under a FastCGI environment, your application will execute as a
239 FastCGI process (this works both for dynamic shared-hosting-style FastCGI
240 and for apache FastCgiServer style setups).
242 If run from the commandline with a URL path, it runs a GET request against
245 $ perl -Ilib examples/hello-world/hello-world.cgi /
247 Content-Type: text/plain
251 Additionally, you can treat the file as though it were a standard PSGI
252 application file (*.psgi). For example you can start up up with C<plackup>
254 plackup my_web_simple_app.pl
258 starman my_web_simple_app.pl
262 This method is called by L</run_if_script> to create the L<PSGI> app coderef
263 for use via L<Plack> and L<plackup>. If you want to globally add middleware,
264 you can override this method:
266 use Web::Simple 'HelloWorld';
273 around 'to_psgi_app', sub {
274 my ($orig, $self) = (shift, shift);
275 my $app = $self->$orig(@_);
277 enable ...; ## whatever middleware you want
283 This method can also be used to mount a Web::Simple application within
284 a separate C<*.psgi> file -
292 mount '/' => WSApp->to_psgi_app;
293 mount '/another' => AnotherWSApp->to_psgi_app;
296 This method can be called as a class method, in which case it implicitly
297 calls ->new, or as an object method ... in which case it doesn't.
301 Used for running your application under stand-alone CGI and FCGI modes.
303 I should document this more extensively but run_if_script will call it when
304 you need it, so don't worry about it too much.
306 =head2 run_test_request
308 my $res = $app->run_test_request(GET => '/');
310 my $res = $app->run_test_request(POST => '/' => %form);
312 my $res = $app->run_test_request($http_request);
314 Accepts either an L<HTTP::Request> object or ($method, $path) and runs that
315 request against the application, returning an L<HTTP::Response> object.
317 If the HTTP method is POST or PUT, then a series of pairs can be passed after
318 this to create a form style message body. If you need to test an upload, then
319 create an L<HTTP::Request> object by hand or use the C<POST> subroutine
320 provided by L<HTTP::Request::Common>.
324 See L<Web::Simple> for authors.
326 =head1 COPYRIGHT AND LICENSE
328 See L<Web::Simple> for the copyright and license.