View on
MetaCPAN
Flavio Poletti > Dancer-Middleware-Rebase-0.8.0 > Dancer::Middleware::Rebase

Download:
Dancer-Middleware-Rebase-0.8.0.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.8.0   Source  

NAME ^

Dancer::Middleware::Rebase - a Plack middleware to be used for Dancer

VERSION ^

version 0.8.0

DESCRIPTION ^

This is a Plack::Middleware specifically geared to the Dancer framework. The goal is to let you rebase your application easily, i.e. let you move your application that usually lives in http://example.com/ into http://example.com/some/prefix/ or even http://whatever.example.com/other/prefix/.

This can be particularly useful in a reverse-proxy deployment, where the application is called by the proxy HTTP server and thus lives in a different namespace with respect to the one available to the end user.

Suppose for example that you have a reverse-proxy deployment, where the end user calls route /homepage in your application using the URI http://example.com/app/homepage. If you are using Apache with the following configuration:

   ProxyPass        /app/ http://internal:3000/
   ProxyPassReverse /app/ http://internal:3000/

then the route in your application will be called as http://internal:3000/homepage. This leads to two problems:

You might think that you can address the latter problem with a proper Apache configuration:

   ProxyPass        /app/ http://internal:3000/app/
   ProxyPassReverse /app/ http://internal:3000/app/

but with this configuration you receive a request towards http://internal:3000/app/homepage, which is not going to work smoothly for different reasons:

Dancer::Middleware::Rebase addresses all these problems at the same time. You can set a base URI that will be propagated in the $env passed to your application. In particular, it will set all the proper variables that are then used by Dancer::Request methods base() and uri_for() in order to establish the URI where all stuff can be referred.

In case you like keeping the prefix part in the Apache configuration, anyway, you still have the problem of stripping it before giving it to the application. In this case, you can set a strip parameter to eliminate the prefix from the PATH_INFO component of $env.

CONFIGURATION ^

This module is a Plack::Middleware, so you have to configure it inside plack_middlewares like this:

   plack_middlewares:
      -
         - "+Dancer::Middleware::Rebase"
         - base
         - "http://example.com/app"
         - strip
         - 1

Please note that you have to put a plus sign before the module name, otherwise Plack will think that it is a name to be referred to the Plack::Middleware namespace.

You can set the following options:

base

the URI that has to be set as the base one. This will be what you eventually get when you call request.base in your code and in your templates, and it is also used by uri_for.

strip

either a true value or a string that starts with /. In the first case, the path portion of the base URI will be used as a prefix to be stripped from PATH_INFO, otherwise the specified string is used. You should only need the first approach, anyway.

AUTHOR ^

Flavio Poletti <polettix@cpan.org>

COPYRIGHT AND LICENSE ^

Copyright (C) 2011 by Flavio Poletti <polettix@cpan.org>.

This module is free software. You can redistribute it and/or modify it under the terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.

syntax highlighting: