Amon2-Lite

 view release on metacpan or  search on metacpan

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

package Amon2::Lite;
use strict;
use warnings;
use 5.008008;
our $VERSION = '0.13';

use parent qw/Amon2 Amon2::Web/;
use Router::Simple 0.04;
use Text::Xslate;
use Text::Xslate::Bridge::TT2Like;
use File::Spec;
use File::Basename qw(dirname);
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 });
                    return $app_file_2->call($env);
                } else {
                    return $orig_app->($env);
                }
            };
        }
        if (my @middlewares = @{"${caller}::_MIDDLEWARES"}) {
            for my $middleware (@middlewares) {
                my ($klass, $args) = @$middleware;
                $klass = Plack::Util::load_class($klass, 'Plack::Middleware');
                $app = $klass->wrap($app, %$args);
            }
        }
        unless ($opts{no_x_content_type_options}) {
            $class->add_trigger(AFTER_DISPATCH => sub {
                my ($c, $res) = @_;
                $res->header( 'X-Content-Type-Options' => 'nosniff' );
            });
        }
        unless ($opts{no_x_frame_options}) {
            $class->add_trigger(AFTER_DISPATCH => sub {
                my ($c, $res) = @_;
                $res->header( 'X-Frame-Options' => 'DENY' );
            });
        }
        return $app;
    };

    *{"${base_class}::enable_middleware"} = sub {
        my ($class, $klass, %args) = @_;
        push @{"${caller}::_MIDDLEWARES"}, [$klass, \%args];
    };
    *{"${base_class}::enable_session"} = sub {
        my ($class, %args) = @_;
        $args{state} ||= do {
            require Plack::Session::State::Cookie;
            Plack::Session::State::Cookie->new(httponly => 1); # for security
        };
        require Plack::Middleware::Session;
        $class->enable_middleware('Plack::Middleware::Session', %args);
        $class->add_trigger(AFTER_DISPATCH => sub {
            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(
                $pattern,
                {code => $code},
            );
        }
    };

    *{"$caller\::get"} = sub {
        $router->connect($_[0], {code => $_[1], method => ['GET', 'HEAD']}, {method => 'GET'});
    };

    *{"$caller\::post"} = sub {
        $router->connect($_[0], {code => $_[1], method => ['POST']}, {method => ['POST']});
    };

    *{"${base_class}\::dispatch"} = sub {
        my ($c) = @_;
        if (my $p = $router->match($c->request->env)) {
            return $p->{code}->( $c, $p );
        } else {
            if ($router->method_not_allowed) {
                my $content = '405 Method Not Allowed';
                return $c->create_response(
                    405,
                    [
                        'Content-Type'   => 'text/plain; charset=utf-8',
                        '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',
            'module'   => [ 'Text::Xslate::Bridge::TT2Like' ],
            'path'     => [ $vpath, $tmpl_dir ],
            'function' => {
                c        => sub { Amon2->context() },
                uri_with => sub { Amon2->context()->req->uri_with(@_) },
                uri_for  => sub { Amon2->context()->uri_for(@_) },
            },
        );
        my $merge = sub {
            my ($stuff) = @_;
            for (qw(module path)) {
                if ($stuff->{$_}) {
                    unshift @{$params{$_}}, @{delete $stuff->{$_}};
                }
            }
            for (qw(function)) {
                if ($stuff->{$_}) {
                    $params{$_} = +{ %{$params{$_}}, %{delete $stuff->{$_}} };
                }
            }
            while (my ($k, $v) = each %$stuff) {
                $params{$k} = $v;
            }
        };
        if (my $config = $caller->config->{'Text::Xslate'}) {
            $merge->($config);
        }
        if (%options) {
            $merge->(\%options);
        }
        my $xslate = Text::Xslate->new(%params);
        no warnings 'redefine';
        *{"${caller}::create_view"} = sub { $xslate };
        $xslate;
    };

    if (-d File::Spec->catdir($caller->base_dir, 'config')) {
        *{"${base_class}::load_config"} = sub { Amon2::Config::Simple->load(shift) };
    } else {
        *{"${base_class}::load_config"} = sub { +{ } };
    }
}


1;
__END__

=for stopwords TinyURL

=encoding utf8



( run in 0.871 second using v1.01-cache-2.11-cpan-63c85eba8c4 )