Any-Daemon-HTTP

 view release on metacpan or  search on metacpan

lib/Any/Daemon/HTTP/VirtualHost.pod  view on Meta::CPAN

  }

  sub ah($$$$)
  {   my ($self, $session, $request, $uri, $tree) = @_;
      return HTTP::Response->new(...);
  }

  package My::Service::Session;
  use parent 'Any::Daemon::HTTP::Session';

=head2 URI Rewrite

For each request, the L<rewrite()|Any::Daemon::HTTP::VirtualHost/"Basic daemon actions"> method is called to see whether a
rewrite of the URI is required.  The method must return the original URI
object (the only parameter) or a new URI object.

B<. Example: usage>

  my $vhost = Any::Daemon::HTTP::VirtualHost
    ->new(..., rewrite => \&rewrite);

  my $vhost = My::Service     # see above
    ->new(..., rewrite => 'rewrite');

  my $vhost = My::Service     # see above
    ->new(..., rewrite => \%lookup_table);

B<. Example: rewrite URI>

  my %lookup =
    ( '/'     => '/index-en.html'
    , '/news' => '/news/2013/index.html'
    );

  sub rewrite($)
  {  my ($vhost, $uri) = @_;
     # when called as method, $vhost --> $self

     # with lookup table
     $uri = URI->new_abs($lookup{$uri->path}, $uri)
         if exists $lookup{$uri->path};

     # whole directory trees
     $uri = URI->new_abs('/somewhere/else'.$1, $uri)
         if $uri->path =~ m!^/some/dir(/.*|$)!;
     
     $uri;
  }

=head2 Using Template Toolkit

Connecting this server to the popular Template Toolkit web-page
framework is quite simple:

  # Use TT only for pages under /status
  $vhost->addHandler('/status' => 'ttStatus');

  sub ttStatus($$$$)
  {   my ($self, $session, $request, $uri, $tree) = @_;;

      # Often, this object is global or an attribute
      my $template = Template->new(...);

      my $output;
      my $values = {...};  # collect the values
      $template->process($fn, $values, \$output)
          or die $template->error, "\n";

      HTTP::Response->new(HTTP_OK, undef
        , ['Content-Type' => 'text/html']
        , "$output"
        );
  }

See Log::Report::Template if you need translations as well.

=head1 SEE ALSO

This module is part of Any-Daemon-HTTP distribution version 0.30,
built on April 06, 2020. Website: F<http://perl.overmeer.net/any-daemon/>

=head1 LICENSE

Copyrights 2013-2020 by [Mark Overmeer]. For other contributors see ChangeLog.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>



( run in 2.091 seconds using v1.01-cache-2.11-cpan-5735350b133 )