Amon2-Lite

 view release on metacpan or  search on metacpan

lib/Amon2/Lite.pm  view on Meta::CPAN

use Data::Section::Simple ();
use Amon2::Config::Simple;

my $COUNTER;

sub import {
    my $class = shift;
    no strict 'refs';

    my $router = Router::Simple->new();
    my $caller = caller(0);

    my $base_class = 'Amon2::Lite::_child_' . $COUNTER++;
    {
        no warnings;
        unshift @{"$base_class\::ISA"}, qw/Amon2 Amon2::Web/;
        unshift @{"$caller\::ISA"}, $base_class;
    }

    *{"$caller\::to_app"} = sub {
        my ($class, %opts) = @_;

        my $app = $class->Amon2::Web::to_app();
        if (delete $opts{handle_static}) {
            my $vpath = Data::Section::Simple->new($caller)->get_data_section();
            require Plack::App::File;
            my $orig_app = $app;
            my $app_file_1;
            my $app_file_2;
            my $root1 = File::Spec->catdir( dirname((caller(0))[1]), 'static' );
            my $root2 = File::Spec->catdir( dirname((caller(0))[1]) );
            $app = sub {
                my $env = shift;
                if ((my $content = $vpath->{$env->{PATH_INFO}}) && $env->{PATH_INFO} =~ m{^/}) {
                    my $ct = Plack::MIME->mime_type($env->{PATH_INFO});
                    return [200, ['Content-Type' => $ct, 'Content-Length' => length($content)], [$content]];
                } elsif ($env->{PATH_INFO} =~ qr{^(?:/robots\.txt|/favicon\.ico)$}) {
                    $app_file_1 ||= Plack::App::File->new({ root => $root1 });
                    return $app_file_1->call($env);
                } elsif ($env->{PATH_INFO} =~ m{^/static/}) {
                    $app_file_2 ||= Plack::App::File->new({ root => $root2 });

lib/Amon2/Lite.pm  view on Meta::CPAN

            my ($c, $res) = @_;
            $res->header('Cache-Control' => 'private');
        });
    };

    *{"$caller\::router"} = sub { $router };

    # any [qw/get post delete/] => '/bye' => sub { ... };
    # any '/bye' => sub { ... };
    *{"$caller\::any"} = sub ($$;$) {
        my $pkg = caller(0);
        if (@_==3) {
            my ($methods, $pattern, $code) = @_;
            $router->connect(
                $pattern,
                {code => $code, method => [ map { uc $_ } @$methods ]},
                {method => [map { uc $_ } @$methods]},
            );
        } else {
            my ($pattern, $code) = @_;
            $router->connect(

lib/Amon2/Lite.pm  view on Meta::CPAN

                        'Content-Length' => length($content),
                    ],
                    [$content]
                );
            } else {
                return $c->res_404();
            }
        }
    };

    my $tmpl_dir = File::Spec->catdir(dirname((caller(0))[1]), 'tmpl');
    *{"${base_class}::create_view"} = sub {
        $base_class->template_options();
    };
    *{"${base_class}::template_options"} = sub {
        my ($class, %options) = @_;

        # using lazy loading to read __DATA__ section.
        my $vpath = Data::Section::Simple->new($caller)->get_data_section();
        my %params = (
            'syntax'   => 'TTerse',



( run in 0.531 second using v1.01-cache-2.11-cpan-26ccb49234f )