#=============================================================================== # # DESCRIPTION: HTTP server # # AUTHOR: Aliaksandr P. Zahatski, #=============================================================================== =head1 NAME Plosurin::HTTP - Serve web mode =head1 SYNOPSIS plosurin.p5 -t web =head1 DESCRIPTION Plosurin::HTTP - Serve web mode =cut package Plosurin::HTTP; use strict; use warnings; use base qw/WebDAO::Engine/; use Data::Dumper; use Plosurin; sub __any_path { my $self = shift; my $sess = shift; my ( $res, $epath ) = $self->SUPER::__any_path( $sess, @_ ); return $res if $res; my $path = join( "/", @_ ); my $r = $self->response(); #check pathes return $r->error404("File $path not found.") unless -e $path; if ( !-d $path ) { my %args = (); $args{-type} = $r->get_mime_for_filename( $path, 1 ); if ( !$args{-type} && -T $path ) { $args{-type} = 'text/plain'; } return $r->send_file( $path, %args ); } return $self->Index( subpath => $path ); } sub _traverse_ { my $self = shift; my ( $sess, @path ) = @_; unless ( scalar(@path) ) { return $self, $self->Index(); } return $self->SUPER::_traverse_( $sess, @path ); } =head2 _parse_file Parse .soy file and return code and template records Return ref to array [$code, @tmpls] or string if error =cut sub _parse_file { my $self = shift; my $fpath = shift; my $p = Plosurin->new; my $str; open FH, "<$fpath"; { undef $/; $str = }; close FH; my $file = $p->parse( $str, $fpath ) || return "Cant parse " . "
$str
"; my ( $code, @tmpls ); eval { ( $code, @tmpls ) = $p->as_perl5( { package => "Test::App" }, $file ); }; if ($@) { return "Erorr export: $@"; } return [ $code, @tmpls ]; } =head 2 RenderSoy param path = /path/to/file.soy tempalate = .name || [.html] =cut sub RenderSoy { my $self = shift; my %args = @_; my $r = $self->response; my $fpath = $args{path} || return $r->error404("Need argument !"); return $r->error404("File $fpath not found") unless -e $fpath; my $content = $self->_parse_file($fpath); unless ( ref($content) ) { return $r->error404("Error while parse $fpath: $content"); } my ( $code, @tmpls ) = @$content; #template to render my $name2render = $args{template} || ".html"; my $tmpl; foreach my $t (@tmpls) { next unless $t->{name} eq $name2render; $tmpl = $t; last; } #template not found -> error ! unless ($tmpl) { my $str; open FH, "<$fpath"; { undef $/; $str = }; close FH; return $r->error404( "Template $name2render not found in $fpath :
$str
"); } no warnings; my $f = eval("$code"); use warnings; if ($@) { return $r->error404( "Bad code for eval " . $@ . "
$code
" ); } no strict 'refs'; my $call_name = $tmpl->{package_name}; return $call_name->(); } sub Index { my $self = shift; my %arg = @_; my $list = ''; my $root_path = $arg{subpath} || '.'; my $r = $self->response; return $r->error404("File $root_path not found") unless -e $root_path; opendir DIR, $root_path; while ( my $el = readdir DIR ) { my $fpath = $root_path . "/" . $el; _log1 $self "List dir: $fpath"; if ( -d $fpath ) { $el = qq!$el!; } elsif ( $el =~ /\.soy$/ ) { #get list of tempaltes $el = qq!$el!; my $content = $self->_parse_file($fpath); unless ( ref($content) ) { $el .= qq!

$content

!; } else { my ( $code, @tmpls ) = @$content; foreach my $t (@tmpls) { $el .= qq!   $t->{name}!; } } } $list .= < REC } close DIR; <

List path: $root_path


$list

Plosurin v$Plosurin::VERSION

TXT } 1; __END__ =head1 SEE ALSO Closure Templates Documentation L Perl 6 implementation L =head1 AUTHOR Zahatski Aliaksandr, =head1 COPYRIGHT AND LICENSE Copyright (C) 2011 by Zahatski Aliaksandr This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut