Created
November 27, 2010 18:55
-
-
Save tadzik/718160 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use HTTP::Server::Simple::PSGI; | |
grammar Mysite { | |
token TOP { <main> | <about> | <contact> | <api> | <e404> } | |
token main { ^ '/' $ } | |
token about { ^ '/about' $ } | |
token contact { ^ '/about/contact' $ } | |
token api { ^ '/api/' $<type>=[ \S ]* $ } | |
token e404 { } | |
} | |
class Mysite::Actions { | |
method TOP($/) { make $/.values[0].ast } | |
method main($/) { make "Hello from the main page!" } | |
method about($/) { make "About me" } | |
method contact($/) { make "Reach me at 555 01 23" } | |
method api($/) { | |
given $<type>.Str { | |
when 'json' { make '{"foo":"bar"}' } | |
when 'xml' { make '<foo>bar</foo>' } | |
default { make 'Wrong serialization method passed' } | |
} | |
} | |
method e404($/) { make "404: Not found" } | |
} | |
my $app = sub ($env) { | |
my $res = Mysite.parse( | |
$env<REQUEST_URI>, | |
:actions(Mysite::Actions.new) | |
).ast; | |
return ['200', [ 'Content-Type' => 'text/plain' ], $res]; | |
} | |
given HTTP::Server::Simple::PSGI.new { | |
.host = 'localhost'; | |
.app($app); | |
.run; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment