Amon2-Lite

 view release on metacpan or  search on metacpan

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

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
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

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        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

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
                    '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.235 second using v1.01-cache-2.11-cpan-0f795438458 )