HTTP::Router - RESTfulなDispatcher

HTTP::Routerのファーストユーザー兼開発者という事でikasam_aにDispatchされたので、早速HTTP::Routerの紹介を。
http://search.cpan.org/~masaki/HTTP-Router-0.01/

HTTP::Routerは一言で説明すると、RESTfulなDispatcherです。RailsやMerbのRoutesと同じでRestfulなDispatch tableが簡単に作れます。Catalyst::Controller::Resourcesを使っていた人は気にいるんじゃないかなと。(実際にDispatchをするわけではないので、RoutingのTableを作るものといったほうがいいかもしれませんが)

具体的には以下のようなroutingの定義をしておくと、

HTTP::Router->define(
    sub {
        $_->match('/')->to( { controller => 'Root', action => 'index' } );
        $_->resources('Book');
    }   
);

以下のようなDispatch Tableを作る事ができます。
以下は、angelosのroutingテーブルのデバッグプラグインを使って表示したものになります。

Dispatch Table:
.----------------------------------------------------+------------+------------+------------.
| path                                               | method     | controller | action     |
+----------------------------------------------------+------------+------------+------------+
| /                                                  |            | Root       | index      |
| /book.{format}                                     | POST       | Books      | create     |
| /book                                              | POST       | Books      | create     |
| /book.{format}                                     | GET        | Books      | index      |
| /book                                              | GET        | Books      | index      |
| /book/new.{format}                                 | GET        | Books      | post       |
| /book/new                                          | GET        | Books      | post       |
| /book/{book_id}.{format}                           | GET        | Books      | show       |
| /book/{book_id}                                    | GET        | Books      | show       |
| /book/{book_id}/edit.{format}                      | GET        | Books      | edit       |
| /book/{book_id}/edit                               | GET        | Books      | edit       |
| /book/{book_id}.{format}                           | DELETE     | Books      | destroy    |
| /book/{book_id}                                    | DELETE     | Books      | destroy    |
| /book/{book_id}.{format}                           | PUT        | Books      | update     |
| /book/{book_id}                                    | PUT        | Books      | update     |
'----------------------------------------------------+------------+------------+------------'

ちなみにネストすることもできます。

HTTP::Router->define(
    sub {
        $_->resources('Users', sub {
                $_->resources('Articles');
            }   
        );  
    }   
);
Dispatch Table:
.----------------------------------------------------+------------+------------+------------.
| path                                               | method     | controller | action     |
+----------------------------------------------------+------------+------------+------------+
| /users.{format}                                    | POST       | Users      | create     |
| /users                                             | POST       | Users      | create     |
| /users.{format}                                    | GET        | Users      | index      |
| /users                                             | GET        | Users      | index      |
| /users/new.{format}                                | GET        | Users      | post       |
| /users/new                                         | GET        | Users      | post       |
| /users/{user_id}.{format}                          | GET        | Users      | show       |
| /users/{user_id}                                   | GET        | Users      | show       |
| /users/{user_id}/edit.{format}                     | GET        | Users      | edit       |
| /users/{user_id}/edit                              | GET        | Users      | edit       |
| /users/{user_id}.{format}                          | DELETE     | Users      | destroy    |
| /users/{user_id}                                   | DELETE     | Users      | destroy    |
| /users/{user_id}.{format}                          | PUT        | Users      | update     |
| /users/{user_id}                                   | PUT        | Users      | update     |
| /users/{user_id}/articles.{format}                 | POST       | Articles   | create     |
| /users/{user_id}/articles                          | POST       | Articles   | create     |
| /users/{user_id}/articles.{format}                 | GET        | Articles   | index      |
| /users/{user_id}/articles                          | GET        | Articles   | index      |
| /users/{user_id}/articles/new.{format}             | GET        | Articles   | post       |
| /users/{user_id}/articles/new                      | GET        | Articles   | post       |
| /users/{user_id}/articles/{article_id}.{format}    | GET        | Articles   | show       |
| /users/{user_id}/articles/{article_id}             | GET        | Articles   | show       |
| /users/{user_id}/articles/{article_id}/edit.{form- | GET        | Articles   | edit       |
| at}                                                |            |            |            |
| /users/{user_id}/articles/{article_id}/edit        | GET        | Articles   | edit       |
| /users/{user_id}/articles/{article_id}.{format}    | DELETE     | Articles   | destroy    |
| /users/{user_id}/articles/{article_id}             | DELETE     | Articles   | destroy    |
| /users/{user_id}/articles/{article_id}.{format}    | PUT        | Articles   | update     |
| /users/{user_id}/articles/{article_id}             | PUT        | Articles   | update     |
'----------------------------------------------------+------------+------------+------------'

HTTP::RouterのWAFへの組み込み方

angelosではHTTP::Routerを使っているので、組み込み方を参考にしたい方はどうぞ。

作るのは、routing定義を読み込んでDispatch tableを作成するBuilderと、そのDispatch Tableを使ったDispatcherです。
コードは以下の部分になります。

わずかこれだけの記述でRESTfulなDispatcherが作れるHTTP::Routerはとてもいいですね。ikasam_a++