Difference between revisions of "User:Exor674/RoutingExamplePages"
From Dreamwidth Notes
(Created page with '== Not using a template -- Data Page == <source lang="perl"> package DW::Controller::MOO; use strict; use warnings; use DW::Routing; use DW::Request; use JSON; DW::Routing->reg…') |
|||
Line 32: | Line 32: | ||
$format->[1]->( $r, $response ); | $format->[1]->( $r, $response ); | ||
+ | |||
+ | return $r->OK; | ||
+ | } | ||
+ | |||
+ | 1; | ||
+ | </source> | ||
+ | |||
+ | == Not using a template -- Data Page with caching == | ||
+ | <source lang="perl"> | ||
+ | package DW::Controller::SlowMOO; | ||
+ | use strict; | ||
+ | use warnings; | ||
+ | use DW::Routing; | ||
+ | use DW::Request; | ||
+ | use DW::FragmentCache; | ||
+ | use JSON; | ||
+ | |||
+ | DW::Routing->register_string( "/slow_moo", \&handler ); | ||
+ | |||
+ | sub handler { | ||
+ | my $r = DW::Request->get; | ||
+ | |||
+ | $get_response = sub { | ||
+ | sleep 5; | ||
+ | return { | ||
+ | time => time, | ||
+ | cute => ["kittens", "puppies"], | ||
+ | string => "Kittens and puppies are cute", | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | my $formats = { | ||
+ | 'json' => [ "application/json", sub { | ||
+ | $_[0]->print( DW::FragmentCache->get( "slowmoo:json", { | ||
+ | render => sub { return objToJson( $get_response->( $_[0]) ); } | ||
+ | }, {} )); | ||
+ | } ], | ||
+ | 'text' => [ "text/plain", sub { | ||
+ | $_[0]->print( DW::FragmentCache->get( "slowmoo:json", { | ||
+ | render => sub { return $get_response->( $_[0] )->{string}; } | ||
+ | }, {} )); | ||
+ | } ], | ||
+ | }; | ||
+ | |||
+ | my $format = $formats->{ $_[0]->format || 'json' }; | ||
+ | |||
+ | return $r->NOT_FOUND if ( ! $format ); | ||
+ | |||
+ | $r->content_type( $format->[0] ) if $format->[0]; | ||
+ | |||
+ | $format->[1]->( $r ); | ||
return $r->OK; | return $r->OK; |
Revision as of 02:55, 5 November 2009
Not using a template -- Data Page
package DW::Controller::MOO; use strict; use warnings; use DW::Routing; use DW::Request; use JSON; DW::Routing->register_string( "/moo", \&handler ); sub handler { my $r = DW::Request->get; $response = { time => time, cute => ["kittens", "puppies"], string => "Kittens and puppies are cute", }; my $formats = { 'json' => [ "application/json", sub { $_[0]->print(objToJson( $_[1] )); } ], 'text' => [ "text/plain" sub { $_[0]->print( $_[1]->string )) } ], }; my $format = $formats->{ $_[0]->format || 'json' }; return $r->NOT_FOUND if ( ! $format ); $r->content_type( $format->[0] ) if $format->[0]; $format->[1]->( $r, $response ); return $r->OK; } 1;
Not using a template -- Data Page with caching
package DW::Controller::SlowMOO; use strict; use warnings; use DW::Routing; use DW::Request; use DW::FragmentCache; use JSON; DW::Routing->register_string( "/slow_moo", \&handler ); sub handler { my $r = DW::Request->get; $get_response = sub { sleep 5; return { time => time, cute => ["kittens", "puppies"], string => "Kittens and puppies are cute", }; }; my $formats = { 'json' => [ "application/json", sub { $_[0]->print( DW::FragmentCache->get( "slowmoo:json", { render => sub { return objToJson( $get_response->( $_[0]) ); } }, {} )); } ], 'text' => [ "text/plain", sub { $_[0]->print( DW::FragmentCache->get( "slowmoo:json", { render => sub { return $get_response->( $_[0] )->{string}; } }, {} )); } ], }; my $format = $formats->{ $_[0]->format || 'json' }; return $r->NOT_FOUND if ( ! $format ); $r->content_type( $format->[0] ) if $format->[0]; $format->[1]->( $r ); return $r->OK; } 1;