Apache-Hendrix
view release on metacpan or search on metacpan
lib/Apache/Hendrix.pm view on Meta::CPAN
@params{ keys %+ } = values %+;
}
my $result = $route->get_method->( \%params, $r, $route );
if ( ref $result ) {
return make_json( $r, $result );
}
return $result;
}
} ## end ROUTE: for my $route ( @{$handlers...})
# No route found, we 404.
return Apache2::Const::NOT_FOUND;
} ## end sub handler
sub make_json {
my ( $r, $object ) = @_;
my $result = __PACKAGE__->json->encode($object);
$r->content_type('application/json');
$r->set_content_length( length($result) );
print $result;
return Apache2::Const::OK;
}
## Route Handling
sub route {
my ( $types, @route ) = @_;
my $handlers = __PACKAGE__->handlers->{ $ENV{CONTEXT_PREFIX} };
for my $route_type ( @{$types} ) {
push @{ $handlers->{$route_type} },
Apache::Hendrix::Route->new( {
path => $route[ 0 ],
method => $route[ 1 ],
base => __PACKAGE__->class_base,
} );
}
__PACKAGE__->handlers->{ $ENV{CONTEXT_PREFIX} } = $handlers;
return;
}
sub get { ## no critic (RequireArgUnpacking)
return route( [ 'GET' ], @_ );
}
sub head { ## no critic (RequireArgUnpacking)
return route( [ 'HEAD' ], @_ );
}
sub post { ## no critic (RequireArgUnpacking)
return route( [ 'POST' ], @_ );
}
sub put { ## no critic (RequireArgUnpacking)
return route( [ 'PUT' ], @_ );
}
sub any {
my (@route) = @_;
# If we haven't specified which type, it's truly "any" route.
# Which for us is currently GET, POST, and HEAD.
if ( scalar(@route) == 2 ) {
unshift @route, [ 'GET', 'POST', 'HEAD', 'PUT' ];
}
return route(@route);
}
## Template Handling
sub template_config {
__PACKAGE__->my_template_config->{ $ENV{CONTEXT_PREFIX} } //= {}; # $template_config_default->();
return __PACKAGE__->my_template_config->{ $ENV{CONTEXT_PREFIX} };
}
sub template {
my ( $template, $vars ) = @_;
__PACKAGE__->my_template->{ $ENV{CONTEXT_PREFIX} }
||= Template->new( __PACKAGE__->my_template_config->{ $ENV{CONTEXT_PREFIX} } );
# Copy site variables to template
if ( __PACKAGE__->template_variable->{ $ENV{CONTEXT_PREFIX} } ) {
while ( my ( $k, $v ) = each %{ __PACKAGE__->template_variable->{ $ENV{CONTEXT_PREFIX} } } ) {
$vars->{$k} //= $v; # For the template
}
}
__PACKAGE__->request->content_type('text/html');
__PACKAGE__->my_template->{ $ENV{CONTEXT_PREFIX} }->process( $template, $vars )
|| croak 'Template process failed: ' . __PACKAGE__->my_template->{ $ENV{CONTEXT_PREFIX} }->error(), "\n";
return Apache2::Const::OK;
}
__PACKAGE__->meta->make_immutable;
1;
=pod
=head1 NAME
Apache::Hendrix - Provides a route-to-sub based web framework
=head1 SYNOPSIS
use Apache::Hendrix;
my $base = '/web_path/base'
base($base); # Base for routes form here below
template_variable->{base} = $base; # Base for templates "base"
template_config->{PRE_PROCESS} = 'header.tt';
template_config->{POST_PROCESS} = 'footer.tt';
template_config->{INCLUDE_PATH} = '/my/path/to/templates/';
get '/' => sub {
my ( $params, $apache_request, $this_sub_reference ) = @_;
my @articles = get_articles ..... ;
return template( 'index.tt', { articles => \@articles, page => 'news', ... } );
};
get '/thing/:param' => sub { ... }
( run in 1.122 second using v1.01-cache-2.11-cpan-13bb782fe5a )