Amon2-Lite
view release on metacpan or search on metacpan
lib/Amon2/Lite.pm view on Meta::CPAN
*{"${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;
( run in 1.347 second using v1.01-cache-2.11-cpan-98e64b0badf )