Commit | Line | Data |
5c33dda5 |
1 | package Web::Simple; |
2 | |
3 | use strict; |
4 | use warnings FATAL => 'all'; |
5 | |
44db8e76 |
6 | sub setup_all_strictures { |
5c33dda5 |
7 | strict->import; |
8 | warnings->import(FATAL => 'all'); |
44db8e76 |
9 | } |
10 | |
11 | sub setup_dispatch_strictures { |
12 | setup_all_strictures(); |
5c33dda5 |
13 | warnings->unimport('syntax'); |
14 | warnings->import(FATAL => qw( |
15 | ambiguous bareword digit parenthesis precedence printf |
16 | prototype qw reserved semicolon |
17 | )); |
44db8e76 |
18 | } |
19 | |
20 | sub import { |
21 | setup_dispatch_strictures(); |
5c33dda5 |
22 | my ($class, $app_package) = @_; |
23 | $class->_export_into($app_package); |
24 | } |
25 | |
26 | sub _export_into { |
27 | my ($class, $app_package) = @_; |
28 | { |
29 | no strict 'refs'; |
92e23550 |
30 | *{"${app_package}::dispatch"} = sub (&) { |
31 | $app_package->_setup_dispatcher([ $_[0]->() ]); |
5c33dda5 |
32 | }; |
74afe4b7 |
33 | *{"${app_package}::response_filter"} = sub (&) { |
5c33dda5 |
34 | $app_package->_construct_response_filter($_[0]); |
35 | }; |
39119082 |
36 | *{"${app_package}::redispatch_to"} = sub { |
37 | $app_package->_construct_redispatch($_[0]); |
38 | }; |
795c4698 |
39 | *{"${app_package}::subdispatch"} = sub ($) { |
40 | $app_package->_construct_subdispatch($_[0]); |
41 | }; |
5c33dda5 |
42 | *{"${app_package}::default_config"} = sub { |
44db8e76 |
43 | $app_package->_setup_default_config(@_); |
5c33dda5 |
44 | }; |
45 | *{"${app_package}::self"} = \${"${app_package}::self"}; |
46 | require Web::Simple::Application; |
47 | unshift(@{"${app_package}::ISA"}, 'Web::Simple::Application'); |
48 | } |
b7063124 |
49 | (my $name = $app_package) =~ s/::/\//g; |
50 | $INC{"${name}.pm"} = 'Set by "use Web::Simple;" invocation'; |
5c33dda5 |
51 | } |
52 | |
7401408e |
53 | =head1 NAME |
54 | |
55 | Web::Simple - A quick and easy way to build simple web applications |
56 | |
57 | =head1 WARNING |
58 | |
59 | This is really quite new. If you're reading this from git, it means it's |
60 | really really new and we're still playing with things. If you're reading |
61 | this on CPAN, it means the stuff that's here we're probably happy with. But |
62 | only probably. So we may have to change stuff. |
63 | |
64 | If we do find we have to change stuff we'll add a section explaining how to |
65 | switch your code across to the new version, and we'll do our best to make it |
66 | as painless as possible because we've got Web::Simple applications too. But |
67 | we can't promise not to change things at all. Not yet. Sorry. |
68 | |
69 | =head1 SYNOPSIS |
70 | |
71 | #!/usr/bin/perl |
72 | |
73 | use Web::Simple 'HelloWorld'; |
74 | |
75 | { |
76 | package HelloWorld; |
77 | |
92e23550 |
78 | dispatch { |
7401408e |
79 | sub (GET) { |
80 | [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ] |
81 | }, |
82 | sub () { |
83 | [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] |
84 | } |
85 | ]; |
86 | } |
87 | |
88 | HelloWorld->run_if_script; |
89 | |
90 | If you save this file into your cgi-bin as hello-world.cgi and then visit |
91 | |
92 | http://my.server.name/cgi-bin/hello-world.cgi/ |
93 | |
94 | you'll get the "Hello world!" string output to your browser. For more complex |
95 | examples and non-CGI deployment, see below. |
96 | |
97 | =head1 WHY? |
98 | |
da8429c9 |
99 | Web::Simple was originally written to form part of my Antiquated Perl talk for |
100 | Italian Perl Workshop 2009, but in writing the bloggery example I realised |
101 | that having a bare minimum system for writing web applications that doesn't |
102 | drive me insane was rather nice and decided to spend my attempt at nanowrimo |
103 | for 2009 improving and documenting it to the point where others could use it. |
7401408e |
104 | |
105 | The philosophy of Web::Simple is to keep to an absolute bare minimum, for |
106 | everything. It is not designed to be used for large scale applications; |
107 | the L<Catalyst> web framework already works very nicely for that and is |
108 | a far more mature, well supported piece of software. |
109 | |
110 | However, if you have an application that only does a couple of things, and |
111 | want to not have to think about complexities of deployment, then Web::Simple |
112 | might be just the thing for you. |
113 | |
114 | The Antiquated Perl talk can be found at L<http://www.shadowcat.co.uk/archive/conference-video/>. |
115 | |
116 | =head1 DESCRIPTION |
117 | |
118 | The only public interface the Web::Simple module itself provides is an |
119 | import based one - |
120 | |
121 | use Web::Simple 'NameOfApplication'; |
122 | |
123 | This imports 'strict' and 'warnings FATAL => "all"' into your code as well, |
124 | so you can skip the usual |
125 | |
126 | use strict; |
127 | use warnings; |
128 | |
129 | provided you 'use Web::Simple' at the top of the file. Note that we turn |
130 | on *fatal* warnings so if you have any warnings at any point from the file |
131 | that you did 'use Web::Simple' in, then your application will die. This is, |
132 | so far, considered a feature. |
133 | |
134 | Calling the import also makes NameOfApplication isa Web::Simple::Application |
135 | - i.e. does the equivalent of |
136 | |
137 | { |
138 | package NameOfApplication; |
139 | use base qw(Web::Simple::Application); |
140 | } |
141 | |
142 | It also exports the following subroutines: |
143 | |
144 | default_config( |
145 | key => 'value', |
146 | ... |
147 | ); |
148 | |
92e23550 |
149 | dispatch { sub (...) { ... }, ... }; |
7401408e |
150 | |
74afe4b7 |
151 | response_filter { ... }; |
7401408e |
152 | |
153 | redispatch_to '/somewhere'; |
154 | |
795c4698 |
155 | subdispatch sub (...) { ... } |
156 | |
44db8e76 |
157 | and creates a $self global variable in your application package, so you can |
7401408e |
158 | use $self in dispatch subs without violating strict (Web::Simple::Application |
159 | arranges for dispatch subroutines to have the correct $self in scope when |
160 | this happens). |
161 | |
b7063124 |
162 | Finally, import sets |
163 | |
164 | $INC{"NameOfApplication.pm"} = 'Set by "use Web::Simple;" invocation'; |
165 | |
166 | so that perl will not attempt to load the application again even if |
167 | |
168 | require NameOfApplication; |
169 | |
170 | is encountered in other code. |
171 | |
7401408e |
172 | =head1 EXPORTED SUBROUTINES |
173 | |
174 | =head2 default_config |
175 | |
176 | default_config( |
177 | one_key => 'foo', |
178 | another_key => 'bar', |
179 | ); |
180 | |
181 | ... |
182 | |
183 | $self->config->{one_key} # 'foo' |
184 | |
185 | This creates the default configuration for the application, by creating a |
186 | |
187 | sub _default_config { |
188 | return (one_key => 'foo', another_key => 'bar'); |
189 | } |
190 | |
191 | in the application namespace when executed. Note that this means that |
44db8e76 |
192 | you should only run default_config once - calling it a second time will |
193 | cause an exception to be thrown. |
7401408e |
194 | |
195 | =head2 dispatch |
196 | |
92e23550 |
197 | dispatch { |
7401408e |
198 | sub (GET) { |
199 | [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ] |
200 | }, |
201 | sub () { |
202 | [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] |
203 | } |
92e23550 |
204 | }; |
7401408e |
205 | |
44db8e76 |
206 | The dispatch subroutine calls NameOfApplication->_setup_dispatcher with |
92e23550 |
207 | the return value of the block passed to it, which then creates your Web::Simple |
208 | application's dispatcher from these subs. The prototype of each subroutine |
7401408e |
209 | is expected to be a Web::Simple dispatch specification (see |
210 | L</DISPATCH SPECIFICATIONS> below for more details), and the body of the |
451853d5 |
211 | subroutine is the code to execute if the specification matches. |
212 | |
92e23550 |
213 | Each dispatcher is given the dispatcher constructed from the next subroutine |
214 | returned as its next dispatcher, except for the final subroutine, which |
451853d5 |
215 | is given the return value of NameOfApplication->_build_final_dispatcher |
216 | as its next dispatcher (by default this returns a 500 error response). |
217 | |
218 | See L</DISPATCH STRATEGY> below for details on how the Web::Simple dispatch |
7401408e |
219 | system uses the return values of these subroutines to determine how to |
220 | continue, alter or abort dispatch. |
221 | |
44db8e76 |
222 | Note that _setup_dispatcher creates a |
7401408e |
223 | |
44db8e76 |
224 | sub _dispatcher { |
225 | return <root dispatcher object here>; |
7401408e |
226 | } |
227 | |
228 | method in your class so as with default_config, calling dispatch a second time |
44db8e76 |
229 | will result in an exception. |
7401408e |
230 | |
231 | =head2 response_filter |
232 | |
233 | response_filter { |
234 | # Hide errors from the user because we hates them, preciousss |
235 | if (ref($_[1]) eq 'ARRAY' && $_[1]->[0] == 500) { |
236 | $_[1] = [ 200, @{$_[1]}[1..$#{$_[1]}] ]; |
237 | } |
238 | return $_[1]; |
239 | }; |
240 | |
241 | The response_filter subroutine is designed for use inside dispatch subroutines. |
242 | |
44db8e76 |
243 | It creates and returns a special dispatcher that always matches, and calls |
244 | the block passed to it as a filter on the result of running the rest of the |
245 | current dispatch chain. |
246 | |
247 | Thus the filter above runs further dispatch as normal, but if the result of |
248 | dispatch is a 500 (Internal Server Error) response, changes this to a 200 (OK) |
249 | response without altering the headers or body. |
250 | |
251 | =head2 redispatch_to |
252 | |
253 | redispatch_to '/other/url'; |
254 | |
255 | The redispatch_to subroutine is designed for use inside dispatch subroutines. |
256 | |
257 | It creates and returns a special dispatcher that always matches, and instead |
258 | of continuing dispatch re-delegates it to the start of the dispatch process, |
259 | but with the path of the request altered to the supplied URL. |
260 | |
261 | Thus if you receive a POST to '/some/url' and return a redipstch to |
262 | '/other/url', the dispatch behaviour will be exactly as if the same POST |
263 | request had been made to '/other/url' instead. |
7401408e |
264 | |
795c4698 |
265 | =head2 subdispatch |
266 | |
267 | subdispatch sub (/user/*/) { |
268 | my $u = $self->user($_[1]); |
269 | [ |
270 | sub (GET) { $u }, |
271 | sub (DELETE) { $u->delete }, |
272 | ] |
273 | } |
274 | |
275 | The subdispatch subroutine is designed for use in dispatcher construction. |
276 | |
277 | It creates a dispatcher which, if it matches, treats its return value not |
278 | as a final value but an arrayref of dispatch specifications such as could |
279 | be passed to the dispatch subroutine itself. These are turned into a dispatcher |
280 | which is then invoked. Any changes the match makes to the request are in |
281 | scope for this inner dispatcher only - so if the initial match is a |
282 | destructive one like .html the full path will be restored if the |
283 | subdispatch fails. |
284 | |
3583ca04 |
285 | =head1 DISPATCH STRATEGY |
286 | |
81a5b03e |
287 | =head2 Description of the dispatcher object |
288 | |
289 | Web::Simple::Dispatcher objects have three components: |
290 | |
291 | =over 4 |
292 | |
293 | =item * match - an optional test if this dispatcher matches the request |
294 | |
295 | =item * call - a routine to call if this dispatcher matches (or has no match) |
296 | |
297 | =item * next - the next dispatcher to call |
298 | |
299 | =back |
300 | |
301 | When a dispatcher is invoked, it checks its match routine against the |
302 | request environment. The match routine may provide alterations to the |
303 | request as a result of matching, and/or arguments for the call routine. |
304 | |
305 | If no match routine has been provided then Web::Simple treats this as |
306 | a success, and supplies the request environment to the call routine as |
307 | an argument. |
308 | |
309 | Given a successful match, the call routine is now invoked in list context |
310 | with any arguments given to the original dispatch, plus any arguments |
311 | provided by the match result. |
312 | |
313 | If this routine returns (), Web::Simple treats this identically to a failure |
314 | to match. |
315 | |
316 | If this routine returns a Web::Simple::Dispatcher, the environment changes |
317 | are merged into the environment and the new dispatcher's next pointer is |
318 | set to our next pointer. |
319 | |
320 | If this routine returns anything else, that is treated as the end of dispatch |
321 | and the value is returned. |
322 | |
323 | On a failed match, Web::Simple invokes the next dispatcher with the same |
324 | arguments and request environment passed to the current one. On a successful |
325 | match that returned a new dispatcher, Web::Simple invokes the new dispatcher |
326 | with the same arguments but the modified request environment. |
327 | |
328 | =head2 How Web::Simple builds dispatcher objects for you |
329 | |
330 | In the case of the Web::Simple L</dispatch> export the match is constructed |
331 | from the subroutine prototype - i.e. |
332 | |
333 | sub (<match specification>) { |
334 | <call code> |
335 | } |
336 | |
337 | and the 'next' pointer is populated with the next element of the array, |
338 | expect for the last element, which is given a next that will throw a 500 |
339 | error if none of your dispatchers match. If you want to provide something |
340 | else as a default, a routine with no match specification always matches, so - |
341 | |
342 | sub () { |
343 | [ 404, [ 'Content-type', 'text/plain' ], [ 'Error: Not Found' ] ] |
344 | } |
345 | |
346 | will produce a 404 result instead of a 500 by default. You can also override |
347 | the L<Web::Simple::Application/_build_final_dispatcher> method in your app. |
348 | |
349 | Note that the code in the subroutine is executed as a -method- on your |
350 | application object, so if your match specification provides arguments you |
351 | should unpack them like so: |
352 | |
353 | sub (<match specification>) { |
354 | my ($self, @args) = @_; |
355 | ... |
356 | } |
357 | |
358 | =head2 Web::Simple match specifications |
359 | |
360 | =head3 Method matches |
361 | |
15dfe701 |
362 | sub (GET ...) { |
363 | |
364 | A match specification beginning with a capital letter matches HTTP requests |
365 | with that request method. |
366 | |
81a5b03e |
367 | =head3 Path matches |
368 | |
15dfe701 |
369 | sub (/login) { |
370 | |
371 | A match specification beginning with a / is a path match. In the simplest |
372 | case it matches a specific path. To match a path with a wildcard part, you |
373 | can do: |
374 | |
375 | sub (/user/*) { |
376 | $self->handle_user($_[1]) |
377 | |
378 | This will match /user/<anything> where <anything> does not include a literal |
379 | / character. The matched part becomes part of the match arguments. You can |
380 | also match more than one part: |
381 | |
382 | sub (/user/*/*) { |
383 | my ($self, $user_1, $user_2) = @_; |
384 | |
385 | sub (/domain/*/user/*) { |
386 | my ($self, $domain, $user) = @_; |
387 | |
388 | and so on. To match an arbitrary number of parts, use - |
389 | |
390 | sub (/page/**) { |
391 | |
392 | This will result in an element per /-separated part so matched. Note that |
393 | you can do |
394 | |
395 | sub (/page/**/edit) { |
396 | |
397 | to match an arbitrary number of parts up to but not including some final |
398 | part. |
399 | |
da8429c9 |
400 | Finally, |
401 | |
402 | sub (/foo/...) { |
403 | |
404 | will match /foo/ on the beginning of the path -and- strip it, much like |
405 | .html strips the extension. This is designed to be used to construct |
406 | nested dispatch structures, but can also prove useful for having e.g. an |
407 | optional language specification at the start of a path. |
408 | |
409 | Note that the '...' is a "maybe something here, maybe not" so the above |
410 | specification will match like this: |
411 | |
412 | /foo # no match |
413 | /foo/ # match and strip path to '/' |
414 | /foo/bar/baz # match and strip path to '/bar/baz' |
415 | |
81a5b03e |
416 | =head3 Extension matches |
417 | |
15dfe701 |
418 | sub (.html) { |
419 | |
420 | will match and strip .html from the path (assuming the subroutine itself |
421 | returns something, of course). This is normally used for rendering - e.g. |
422 | |
423 | sub (.html) { |
74afe4b7 |
424 | response_filter { $self->render_html($_[1]) } |
15dfe701 |
425 | } |
426 | |
b8bd7bd1 |
427 | Additionally, |
428 | |
429 | sub (.*) { |
430 | |
431 | will match any extension and supplies the stripped extension as a match |
432 | argument. |
433 | |
9b9866ae |
434 | =head3 Query and body parameter matches |
435 | |
436 | Query and body parameters can be match via |
437 | |
438 | sub (?<param spec>) { # match URI query |
439 | sub (%<param spec>) { # match body params |
440 | |
441 | The body is only matched if the content type is |
442 | application/x-www-form-urlencoded (note this means that Web::Simple does |
443 | not yet handle uploads; this will be addressed in a later release). |
444 | |
445 | The param spec is elements of one of the following forms - |
446 | |
447 | param~ # optional parameter |
448 | param= # required parameter |
449 | @param~ # optional multiple parameter |
450 | @param= # required multiple parameter |
eb9e0e25 |
451 | :param~ # optional parameter in hashref |
452 | :param= # required parameter in hashref |
453 | :@param~ # optional multiple in hashref |
454 | :@param= # required multiple in hashref |
455 | * # include all other parameters in hashref |
456 | @* # include all other parameters as multiple in hashref |
9b9866ae |
457 | |
eb9e0e25 |
458 | separated by the & character. The arguments added to the request are |
459 | one per non-:/* parameter (scalar for normal, arrayref for multiple), |
460 | plus if any :/* specs exist a hashref containing those values. |
9b9866ae |
461 | |
462 | So, to match a page parameter with an optional order_by parameter one |
463 | would write: |
464 | |
465 | sub (?page=&order_by~) { |
eb9e0e25 |
466 | my ($self, $page, $order_by) = @_; |
467 | return unless $page =~ /^\d+$/; |
468 | $page ||= 'id'; |
9b9866ae |
469 | response_filter { |
470 | $_[1]->search_rs({}, $p); |
471 | } |
472 | } |
473 | |
474 | to implement paging and ordering against a L<DBIx::Class::ResultSet> object. |
475 | |
eb9e0e25 |
476 | To get all parameters as a hashref of arrayrefs, write: |
477 | |
478 | sub(?@*) { |
479 | my ($self, $params) = @_; |
480 | ... |
481 | |
81a5b03e |
482 | =head3 Combining matches |
483 | |
15dfe701 |
484 | Matches may be combined with the + character - e.g. |
485 | |
b8bd7bd1 |
486 | sub (GET + /user/*) { |
487 | |
488 | to create an AND match. They may also be combined withe the | character - e.g. |
489 | |
490 | sub (GET|POST) { |
491 | |
492 | to create an OR match. Matches can be nested with () - e.g. |
493 | |
494 | sub ((GET|POST) + /user/*) { |
495 | |
496 | and negated with ! - e.g. |
497 | |
498 | sub (!/user/foo + /user/*) { |
499 | |
500 | ! binds to the immediate rightmost match specification, so if you want |
501 | to negate a combination you will need to use |
502 | |
503 | sub ( !(POST|PUT|DELETE) ) { |
504 | |
505 | and | binds tighter than +, so |
506 | |
507 | sub ((GET|POST) + /user/*) { |
508 | |
509 | and |
510 | |
511 | sub (GET|POST + /user/*) { |
512 | |
513 | are equivalent, but |
514 | |
515 | sub ((GET + .html) | (POST + .html)) { |
516 | |
517 | and |
518 | |
519 | sub (GET + .html | POST + .html) { |
520 | |
521 | are not - the latter is equivalent to |
522 | |
523 | sub (GET + (.html|POST) + .html) { |
524 | |
525 | which will never match. |
526 | |
527 | =head3 Whitespace |
15dfe701 |
528 | |
529 | Note that for legibility you are permitted to use whitespace - |
530 | |
44db8e76 |
531 | sub (GET + /user/*) { |
15dfe701 |
532 | |
b8bd7bd1 |
533 | but it will be ignored. This is because the perl parser strips whitespace |
534 | from subroutine prototypes, so this is equivalent to |
535 | |
536 | sub (GET+/user/*) { |
15dfe701 |
537 | |
3583ca04 |
538 | =cut |
7401408e |
539 | |
5c33dda5 |
540 | 1; |