Apache-Hendrix

 view release on metacpan or  search on metacpan

lib/Apache/Hendrix.pm  view on Meta::CPAN

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  { ... }

post '/post/:param' => sub { ... }

get qr/some.*regexp/ => sub { ... }

=head1 DETAILS

Provides simple methods to simplify web development.   Routes may be specified as "get", "post", "head", or "put" currently.  Raw perl structures returned will be converted into JSON.

=head1 METHODS

=over

base - sets the base url for calls from this point in the code forward

'get', 'post', 'post', 'head', 'any [optional array]' => sub - defines a route

template - call a template to be created

template_config - configure the template

template_variable - Set a variable to be used in a template


=back

=head1 REQUIRED LIBS

=over

=item Apache2::Const;

=item Apache2::Request;

=item Apache2::RequestIO;

=item Apache2::RequestRec;

=item Carp;



( run in 1.595 second using v1.01-cache-2.11-cpan-df04353d9ac )