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 If you construct your application like:
131 MyWebSimpleApp::Web->new(config=>{environment=>'dev'})
133 then C<config> will have a C<environment> key with a value of 'dev'.
137 In the case where you wish to run your L<Web::Simple> based application as a
138 stand alone CGI application, you can simple do:
140 ## my_web_simple_app.pl
141 use MyWebSimpleApp::Web;
142 MyWebSimpleApp::Web->run_if_script.
144 Or (even more simply) just inline the entire application:
146 ## my_web_simple_app.pl
148 use Web::Simple 'HelloWorld';
153 sub dispatch_request {
155 [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ]
158 [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
163 HelloWorld->run_if_script;
165 Additionally, you can treat the above script as though it were a standard PSGI
166 application file (*.psgi). For example you can start up up with C<plackup>
168 plackup my_web_simple_app.pl
170 Which means you can write a L<Web::Simple> application as a plain old CGI
171 application and seemlessly migrate to a L<Plack> based solution when you are
174 Lastly, L</run_if_script> will automatically detect and support a Fast CGI
179 Given a L<Web::Simple> application root namespace, return it in a form suitable
180 to run in inside a L<Plack> container, or in L<Plack::Builder> or in a C<*.psgi>
186 use MyWebSimpleApp::Web;
193 ## return application
194 MyWebSimpleApp::Web->to_psgi_app;
197 This could be run via C<plackup>, etc. Please note the L<Plack::Builder> DSL
198 is optional, if you are enabling L<Plack::Middleware> internally in your
199 L<Web::Simple> application; your app.psgi could be as simple as:
201 use MyWebSimpleApp::Web;
202 MyWebSimpleApp::Web->to_psgi_app;
204 This means if you want to provide a 'default' set of middleware, one option is
205 to modify this method:
207 use Web::Simple 'HelloWorld';
214 around 'to_psgi_app', sub {
215 my ($orig, $self) = (shift, shift);
216 my $app = $self->$orig(@_);
218 enable ...; ## whatever middleware you want
224 As always, mix and match the pieces you actually need and remember the
225 L<Web::Simple> philosophy of trying to keep it as minimal and simple as possible.
229 Used for running your application under stand-alone CGI and FCGI modes. Also
232 my $app = MyWebSimpleApp::Web->new;
233 my $c = HTTP::Request::AsCGI->new(@args)->setup;
238 Matt S. Trout <mst@shadowcat.co.uk>
242 None required yet. Maybe this module is perfect (hahahahaha ...).
246 Copyright (c) 2010 the Web::Simple L</AUTHOR> and L</CONTRIBUTORS>
251 This library is free software and may be distributed under the same terms