=head1 NAME

Dancer::Deployment - common ways to put your Dancer app into use

=head1 DESCRIPTION

Dancer has been designed to be flexible, and this flexibility extends to your
choices when deploying your Dancer app.


=head2 Running stand-alone

At the simplest, your Dancer app can run standalone, operating as its own
webserver using HTTP::Simple::PSGI.

Simply fire up your app:

    $ perl ./mysuperwebapp.pl
    >> Listening on 0.0.0.0:3000
    == Entering the dance floor ...

Point your browser at it, and away you go!

This option can be useful for small personal web apps or internal apps, but if
you want to make your app available to the world, it probably won't suit you.


=head3 Running stand-alone behind a proxy / load balancer

Another option would be to run your app stand-alone as described above, but then
use a proxy or load balancer to accept incoming requests (on the standard port
80, say) and feed them to your Dancer app.

This could be achieved using various software; examples would include:

=head4 Using Apache's mod_proxy

You could set up a VirtualHost for your web app, and proxy all requests through
to it:

    <VirtualHost mywebapp.example.com:80>
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    </VirtualHost>

Or, if you want your webapp to share an existing VirtualHost, you could have it
under a specified dir:

    ProxyPass /mywebapp/ http://localhost:3000/
    ProxyPassReverse /mywebapp/ http://localhost:3000/


=head4 Using perlbal

C<perlbal> is a single-threaded event-based server written in Perl supporting HTTP load
balancing, web serving, and a mix of the two, available from
L<http://www.danga.com/perlbal/>

It processes hundreds of millions of requests a day just for LiveJournal, Vox
and TypePad and dozens of other "Web 2.0" applications. 

It can also provide a management interface to let you see various information on
requests handled etc.

It could easily be used to handle requests for your Dancer apps, too.

It can be easily installed from CPAN:

    perl -MCPAN -e 'install Perlbal'

Once installed, you'll need to write a configuration file.  See the examples
provided with perlbal, but you'll probably want something like:

    CREATE POOL my_dancers
    POOL my_dancers ADD 10.0.0.10:3030
    POOL my_dancers ADD 10.0.0.11:3030
    POOL my_dancers ADD 10.0.0.12:3030
    POOL my_dancers ADD 10.0.0.13:3030

    CREATE SERVICE my_webapp
    SET listen          = 0.0.0.0:80
    SET role            = reverse_proxy
    SET pool            = my_dancers
    SET persist_client  = on
    SET persist_backend = on
    SET verify_backend  = on
    ENABLE balancer




=head4 Using balance

C<balance> is a simple load-balancer from Inlab Software, available from
L<http://www.inlab.de/balance.html>.

It could be used simply to hand requests to a standalone Dancer app. You could
even run several instances of your Dancer app, on the same machine or on several
machines, and use a machine running balance to distribute the requests between
them, for some serious heavy traffic handling!

To listen on port 80, and send requests to a Dancer app on port 3000:

    balance http localhost:3000

To listen on a specified IP only on port 80, and distribute requests between
multiple Dancer apps on multiple other machines:

    balance -b 10.0.0.1 80 10.0.0.2:3000 10.0.0.3:3000 10.0.0.4:3000



=head2 Running from Apache

You can run your Dancer app from Apache using the following examples:


=head3 Running from Apache with Plack

You can run your app from Apache using PSGI (Plack), with a config like the
following:

    <VirtualHost myapp.example.com>
        ServerName www.myapp.example.com
        ServerAlias myapp.example.com
        DocumentRoot /websites/myapp.example.com

        <Location />
            SetHandler perl-script
            PerlHandler Plack::Handler::Apache2
            PerlSetVar psgi_app /websites/myapp.example.com/app.psgi
        </Location>

        ErrorLog  /websites/myapp.example.com/logs/error_log
        CustomLog /websites/myapp.example.com/logs/access_log common
    </VirtualHost>


=head3 Running from Apache via FastCGI

You can run your Dancer app from Apache via FastCGI using the dispatch.fcgi
script written by the dancer helper script when you create your application
scaffolding:

    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot "/tmp/TestApp/public"

        <Directory "/tmp/TestApp/public">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
            AddHandler fastcgi-script .fcgi
        </Directory>

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /dispatch.fcgi [QSA,L]
    </VirtualHost>


=head3 Running from Apache under appdir

If you want to deploy multiple applications under the same VirtualHost, using
one application per directory for example, you can do the following.

This example uses the FastCGI dispatcher that comes with Dancer, but you should
be able to adapt this to use any other way of deployment described in this guide. 
The only purpose of this example is to show how to deploy multiple applications 
under the same base directory/virtualhost.

	<VirtualHost *:80>
		ServerName localhost
		DocumentRoot "/path/to/rootdir"
		RewriteEngine On
		RewriteCond %{REQUEST_FILENAME} !-f

		<Directory "/path/to/rootdir">
			AllowOverride None
			Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
			Order allow,deny
			Allow from all
			AddHandler fastcgi-script .fcgi
		</Directory>

		RewriteRule /App1(.*)$ /App1/public/dispatch.fcgi$1 [QSA,L]
		RewriteRule /App2(.*)$ /App2/public/dispatch.fcgi$1 [QSA,L]
		...
		RewriteRule /AppN(.*)$ /AppN/public/dispatch.fcgi$1 [QSA,L]
	</VirtualHost>

Of course, if your Apache configuration allows that, you can put the
RewriteRules in a .htaccess file directly within the application's directory,
which lets you add a new application without changing the Apache configuration.
