improve Application.pm docs
[catagits/Web-Simple.git] / lib / Web / Simple / Application.pm
CommitLineData
5c33dda5 1package Web::Simple::Application;
2
8bd060f4 3use Moo;
5c33dda5 4
876e62e1 5has 'config' => (
6 is => 'ro',
7 default => sub {
8 my ($self) = @_;
9 +{ $self->default_config }
10 },
11 trigger => sub {
12 my ($self, $value) = @_;
13 my %default = $self->default_config;
14 my @not = grep !exists $value->{$_}, keys %default;
15 @{$value}{@not} = @default{@not};
16 }
17);
5c33dda5 18
445b3ea0 19sub default_config { () }
39119082 20
445b3ea0 21has '_dispatcher' => (is => 'lazy');
5c33dda5 22
445b3ea0 23sub _build__dispatcher {
24 my $self = shift;
25 require Web::Dispatch;
26 require Web::Simple::DispatchNode;
27 my $final = $self->_build_final_dispatcher;
28 Web::Dispatch->new(
29 app => sub { $self->dispatch_request(@_), $final },
30 node_class => 'Web::Simple::DispatchNode',
31 node_args => { app_object => $self }
32 );
5c33dda5 33}
34
3583ca04 35sub _build_final_dispatcher {
4ed4fb42 36 [ 404, [ 'Content-type', 'text/plain' ], [ 'Not found' ] ]
5c33dda5 37}
38
5c33dda5 39sub run_if_script {
b9e047ef 40 # ->to_psgi_app is true for require() but also works for plackup
445b3ea0 41 return $_[0]->to_psgi_app if caller(1);
e27ab5c5 42 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
5c33dda5 43 $self->run(@_);
44}
45
913a9cf9 46sub _run_cgi {
5c33dda5 47 my $self = shift;
e27ab5c5 48 require Plack::Server::CGI;
b9e047ef 49 Plack::Server::CGI->run($self->to_psgi_app);
d3a61609 50}
51
e27ab5c5 52sub _run_fcgi {
53 my $self = shift;
54 require Plack::Server::FCGI;
b9e047ef 55 Plack::Server::FCGI->run($self->to_psgi_app);
e27ab5c5 56}
57
445b3ea0 58sub to_psgi_app {
bc57805c 59 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
445b3ea0 60 $self->_dispatcher->to_app;
5c33dda5 61}
62
913a9cf9 63sub run {
64 my $self = shift;
e27ab5c5 65 if ($ENV{PHP_FCGI_CHILDREN} || $ENV{FCGI_ROLE} || $ENV{FCGI_SOCKET_PATH}) {
66 return $self->_run_fcgi;
67 } elsif ($ENV{GATEWAY_INTERFACE}) {
2514ad17 68 return $self->_run_cgi;
913a9cf9 69 }
d104fb1d 70 unless (@ARGV && $ARGV[0] =~ m{^/}) {
db2899c3 71 return $self->_run_cli(@ARGV);
d104fb1d 72 }
73
74 my $path = shift @ARGV;
913a9cf9 75
913a9cf9 76 require HTTP::Request::Common;
e27ab5c5 77 require Plack::Test;
913a9cf9 78 local *GET = \&HTTP::Request::Common::GET;
79
80 my $request = GET($path);
e27ab5c5 81 my $response;
b9e047ef 82 Plack::Test::test_psgi($self->to_psgi_app, sub { $response = shift->($request) });
e27ab5c5 83 print $response->as_string;
913a9cf9 84}
85
d104fb1d 86sub _run_cli {
87 my $self = shift;
88 die $self->_cli_usage;
89}
90
91sub _cli_usage {
92 "To run this script in CGI test mode, pass a URL path beginning with /:\n".
93 "\n".
94 " $0 /some/path\n".
95 " $0 /\n"
96}
97
5c33dda5 981;
32d29dcd 99
100=head1 NAME
101
6a4808bf 102Web::Simple::Application - A base class for your Web-Simple application
32d29dcd 103
104=head1 DESCRIPTION
105
106This is a base class for your L<Web::Simple> application. You probably don't
107need to construct this class yourself, since L<Web::Simple> does the 'heavy
108lifting' for you in that regards.
109
110=head1 METHODS
111
6a4808bf 112This class exposes the following public methods.
32d29dcd 113
114=head2 default_config
115
6a4808bf 116Merges with the C<config> initializer to provide configuration information for
117your application. For example:
32d29dcd 118
119 sub default_config {
120 (
121 title => 'Bloggery',
122 posts_dir => $FindBin::Bin.'/posts',
123 );
124 }
125
6a4808bf 126Now, the C<config> attribute of C<$self> will be set to a HashRef
32d29dcd 127containing keys 'title' and 'posts_dir'.
128
12b3e9a3 129The keys from default_config are merged into any config supplied, so
130if you construct your application like:
6a4808bf 131
12b3e9a3 132 MyWebSimpleApp::Web->new(
133 config => { title => 'Spoon', environment => 'dev' }
134 )
6a4808bf 135
12b3e9a3 136then C<config> will contain:
6a4808bf 137
12b3e9a3 138 {
139 title => 'Spoon',
140 posts_dir => '/path/to/myapp/posts',
141 environment => 'dev'
142 }
32d29dcd 143
12b3e9a3 144=head2 run_if_script
32d29dcd 145
12b3e9a3 146The run_if_script method is designed to be used at the end of the script
147or .pm file where your application class is defined - for example:
32d29dcd 148
149 ## my_web_simple_app.pl
150 #!/usr/bin/env perl
151 use Web::Simple 'HelloWorld';
152
153 {
154 package HelloWorld;
155
156 sub dispatch_request {
157 sub (GET) {
158 [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ]
159 },
160 sub () {
161 [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
162 }
163 }
164 }
165
166 HelloWorld->run_if_script;
167
12b3e9a3 168This returns a true value, so your file is now valid as a module - so
6a4808bf 169
12b3e9a3 170 require 'my_web_simple_app.pl';
6a4808bf 171
12b3e9a3 172 my $hw = HelloWorld->new;
6a4808bf 173
12b3e9a3 174will work fine (and you can rename it to lib/HelloWorld.pm later to make it
175a real use-able module).
6a4808bf 176
12b3e9a3 177However, it detects if it's being run as a script (via testing $0) and if
178so attempts to do the right thing.
32d29dcd 179
12b3e9a3 180If run under a CGI environment, your application will execute as a CGI.
32d29dcd 181
12b3e9a3 182If run under a FastCGI environment, your application will execute as a
183FastCGI process (this works both for dynamic shared-hosting-style FastCGI
184and for apache FastCgiServer style setups).
32d29dcd 185
12b3e9a3 186If run from the commandline with a URL path, it runs a GET request against
187that path -
32d29dcd 188
12b3e9a3 189 $ perl -Ilib examples/hello-world/hello-world.cgi /
190 200 OK
191 Content-Type: text/plain
192
193 Hello world!
32d29dcd 194
12b3e9a3 195Additionally, you can treat the file as though it were a standard PSGI
196application file (*.psgi). For example you can start up up with C<plackup>
197
198 plackup my_web_simple_app.pl
32d29dcd 199
12b3e9a3 200or C<starman>
32d29dcd 201
12b3e9a3 202 starman my_web_simple_app.pl
203
204=head2 to_psgi_app
205
206This method is called by L</run_if_script> to create the L<PSGI> app coderef
207for use via L<Plack> and L<plackup>. If you want to globally add middleware,
208you can override this method:
6a4808bf 209
210 use Web::Simple 'HelloWorld';
211 use Plack::Builder;
212
213 {
214 package HelloWorld;
215
216
217 around 'to_psgi_app', sub {
218 my ($orig, $self) = (shift, shift);
219 my $app = $self->$orig(@_);
220 builder {
221 enable ...; ## whatever middleware you want
222 $app;
223 };
224 };
225 }
226
12b3e9a3 227This method can also be used to mount a Web::Simple application within
228a separate C<*.psgi> file -
229
230 use strictures 1;
231 use Plack::Builder;
232 use WSApp;
233 use AnotherWSApp;
234
235 builder {
236 mount '/' => WSApp->to_psgi_app;
237 mount '/another' => AnotherWSApp->to_psgi_app;
238 };
239
240This method can be called as a class method, in which case it implicitly
241calls ->new, or as an object method ... in which case it doesn't.
32d29dcd 242
243=head2 run
244
245Used for running your application under stand-alone CGI and FCGI modes. Also
246useful for testing:
247
248 my $app = MyWebSimpleApp::Web->new;
249 my $c = HTTP::Request::AsCGI->new(@args)->setup;
250 $app->run;
251
252=head1 AUTHOR
253
254Matt S. Trout <mst@shadowcat.co.uk>
255
256=head1 CONTRIBUTORS
257
258None required yet. Maybe this module is perfect (hahahahaha ...).
259
260=head1 COPYRIGHT
261
6a4808bf 262Copyright (c) 2010 the Web::Simple L</AUTHOR> and L</CONTRIBUTORS>
32d29dcd 263as listed above.
264
265=head1 LICENSE
266
267This library is free software and may be distributed under the same terms
268as perl itself.
269
270=cut