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::Server::CGI;
49 Plack::Server::CGI->run($self->to_psgi_app);
54 require Plack::Server::FCGI;
55 Plack::Server::FCGI->run($self->to_psgi_app);
59 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
60 $self->_dispatcher->to_app;
65 if ($ENV{PHP_FCGI_CHILDREN} || $ENV{FCGI_ROLE} || $ENV{FCGI_SOCKET_PATH}) {
66 return $self->_run_fcgi;
67 } elsif ($ENV{GATEWAY_INTERFACE}) {
68 return $self->_run_cgi;
70 unless (@ARGV && $ARGV[0] =~ m{^/}) {
71 return $self->_run_cli(@ARGV);
74 my $path = shift @ARGV;
76 require HTTP::Request::Common;
78 local *GET = \&HTTP::Request::Common::GET;
80 my $request = GET($path);
82 Plack::Test::test_psgi($self->to_psgi_app, sub { $response = shift->($request) });
83 print $response->as_string;
88 die $self->_cli_usage;
92 "To run this script in CGI test mode, pass a URL path beginning with /:\n".
102 Web::Simple::Application - A base class for your Web-Simple application
106 This is a base class for your L<Web::Simple> application. You probably don't
107 need to construct this class yourself, since L<Web::Simple> does the 'heavy
108 lifting' for you in that regards.
112 This class exposes the following public methods.
114 =head2 default_config
116 Merges with the C<config> initializer to provide configuration information for
117 your application. For example:
122 posts_dir => $FindBin::Bin.'/posts',
126 Now, the C<config> attribute of C<$self> will be set to a HashRef
127 containing keys 'title' and 'posts_dir'.
129 The keys from default_config are merged into any config supplied, so
130 if you construct your application like:
132 MyWebSimpleApp::Web->new(
133 config => { title => 'Spoon', environment => 'dev' }
136 then C<config> will contain:
140 posts_dir => '/path/to/myapp/posts',
146 The run_if_script method is designed to be used at the end of the script
147 or .pm file where your application class is defined - for example:
149 ## my_web_simple_app.pl
151 use Web::Simple 'HelloWorld';
156 sub dispatch_request {
158 [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ]
161 [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
166 HelloWorld->run_if_script;
168 This returns a true value, so your file is now valid as a module - so
170 require 'my_web_simple_app.pl';
172 my $hw = HelloWorld->new;
174 will work fine (and you can rename it to lib/HelloWorld.pm later to make it
175 a real use-able module).
177 However, it detects if it's being run as a script (via testing $0) and if
178 so attempts to do the right thing.
180 If run under a CGI environment, your application will execute as a CGI.
182 If run under a FastCGI environment, your application will execute as a
183 FastCGI process (this works both for dynamic shared-hosting-style FastCGI
184 and for apache FastCgiServer style setups).
186 If run from the commandline with a URL path, it runs a GET request against
189 $ perl -Ilib examples/hello-world/hello-world.cgi /
191 Content-Type: text/plain
195 Additionally, you can treat the file as though it were a standard PSGI
196 application file (*.psgi). For example you can start up up with C<plackup>
198 plackup my_web_simple_app.pl
202 starman my_web_simple_app.pl
206 This method is called by L</run_if_script> to create the L<PSGI> app coderef
207 for use via L<Plack> and L<plackup>. If you want to globally add middleware,
208 you can override this method:
210 use Web::Simple 'HelloWorld';
217 around 'to_psgi_app', sub {
218 my ($orig, $self) = (shift, shift);
219 my $app = $self->$orig(@_);
221 enable ...; ## whatever middleware you want
227 This method can also be used to mount a Web::Simple application within
228 a separate C<*.psgi> file -
236 mount '/' => WSApp->to_psgi_app;
237 mount '/another' => AnotherWSApp->to_psgi_app;
240 This method can be called as a class method, in which case it implicitly
241 calls ->new, or as an object method ... in which case it doesn't.
245 Used for running your application under stand-alone CGI and FCGI modes. Also
248 my $app = MyWebSimpleApp::Web->new;
249 my $c = HTTP::Request::AsCGI->new(@args)->setup;
254 Matt S. Trout <mst@shadowcat.co.uk>
258 None required yet. Maybe this module is perfect (hahahahaha ...).
262 Copyright (c) 2010 the Web::Simple L</AUTHOR> and L</CONTRIBUTORS>
267 This library is free software and may be distributed under the same terms