Grammar-based dispatching – a step further

You may remember the idea of dispatching HTTP requests with Perl 6 grammars. Turned out to be funny and interesting, but wait, what’s the big deal? What’s good about a webapp that runs in the CLI, and dispatches only the strings given in the source code? Yeah, it’s funny, but is it practical? Not quite. So here we go with something practical this time.

Someone asked me about a HTTP requests parser. Looks like there alredy was one, built-in in HTTP::Server::Simple by mberends++. After adapting it to Rakudo’s needs (specs change) and to my needs (REQUEST_URI wasn’t working in it), and commiting some hacks of mine, i ended up with a PSGI server suitable for serving webapps. Webapps like our last week’s dispatcher. So here we go:

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;
}

The code is pretty straightforward (methinks), so let’s just skip to the interesting part. Which is: it actually works. You can write a working PSGI app now, dispatching requests with a Perl 6 grammars. Cute, eh?

About these ads


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.