AnyEvent-HTTPD-Router

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

[![Build Status](https://travis-ci.org/ufobat/p5-AnyEvent-HTTPD-Router.svg?branch=master)](https://travis-ci.org/ufobat/p5-AnyEvent-HTTPD-Router)
# NAME

AnyEvent::HTTPD::Router - Adding Routes to AnyEvent::HTTPD

# DESCRIPTION

AnyEvent::HTTPD::Router is an extension to the [AnyEvent::HTTPD](https://metacpan.org/pod/AnyEvent::HTTPD) module, from
which it is inheriting. It adds the `reg_routes()` method to it.

This module aims to add as little as possible overhead to it while still being
flexible and extendable. It requires the same little dependencies that
[AnyEvent::HTTPD](https://metacpan.org/pod/AnyEvent::HTTPD) uses.

The dispatching for the routes happens first. If no route could be found, or you
do not stop further dispatching with `stop_request()` the registered callbacks
will be executed as well; as if you would use [AnyEvent::HTTPD](https://metacpan.org/pod/AnyEvent::HTTPD). In other
words, if you plan to use routes in your project you can use this module and
upgrade from callbacks to routes step by step.

Routes support http methods, but custom methods
[https://cloud.google.com/apis/design/custom\_methods](https://cloud.google.com/apis/design/custom_methods) can also be used. You
don't need to, of course ;-)

# SYNOPSIS

    use AnyEvent::HTTPD::Router;

    my $httpd       = AnyEvent::HTTPD::Router->new( port => 1337 );
    my $all_methods = [qw/GET DELETE HEAD POST PUT PATCH/];

    $httpd->reg_routes(
        GET => '/index.txt' => sub {
            my ( $httpd, $req ) = @_;
            $httpd->stop_request;
            $req->respond([
                200, 'ok', { 'Content-Type' => 'text/plain', }, "test!" ]);
        },
        $all_methods => '/my-method' => sub {
            my ( $httpd, $req ) = @_;
            $httpd->stop_request;
            $req->respond([
                200, 'ok', { 'X-Your-Method' => $req->method }, '' ]);
        },
        GET => '/calendar/:year/:month/:day' => sub {
            my ( $httpd, $req, $param ) = @_;
            my $calendar_entries = get_cal_entries(
                $param->{year}, $param->{month}, $param->{day}
            );

            $httpd->stop_request;
            $reg->respond([
                200, 'ok', { 'Content-Type' => 'application/json'},
                to_json($calendar_entries)
            ]);
        },
        GET => '/static-files/*' => sub {
            my ( $httpd, $req, $param ) = @_;
            my $requeted_file = $param->{'*'};
            my ($content, $content_type) = black_magic($requested_file);

            $httpd->stop_request;
            $req->respond([
                200, 'ok', { 'Content-Type' => $content_type }, $content ]);
        }
    );

    $httpd->run();

# METHODS

- `new()`

    Creates a new `AnyEvent::HTTPD::Router` server. The constructor handles the
    following parameters. All further parameters are passed to `AnyEvent::HTTPD`.

    - `dispatcher`

        You can pass your own implementation of your router dispatcher into this module.
        This expects the dispatcher to be an instance not a class name.

    - `dispatcher_class`

        You can pass your own implementation of your router dispatcher into this module.
        This expects the dispatcher to be a class name.

    - `routes`

        You can add the routes at the constructor. This is an ArrayRef.



( run in 0.819 second using v1.01-cache-2.11-cpan-e1769b4cff6 )