Posts Tagged ‘CGI’

Using Plack to run a Dancer app under a CGI environment

Sawyer X on blogs.perl.org recently wondered how one could run a Dancer application under a 100% CGI environment.

Indeed, he came up quickly with a REST app in development stage and wanted to go on air with a plain old CGI script. He seems to think it’s not possible with Dancer but as Dancer supports PSGI/Plack, he’s wrong ;)

Thanks to the PSGI/Plack goodness, any application can be powered by a Plack-supported server. CGI is one of them and is implemented by Plack::Server::CGI.

By taking a quick look at the POD, we see that all we have to do is to write a pretty simple CGI script to bridge the Dancer app with Apache:

#!/usr/bin/perl

use Plack::Server::CGI;
use Plack::Util;

my $psgi = '/path/to/your/app/app.psgi';
my $app = Plack::Util::load_psgi($psgi);
Plack::Server::CGI->new->run($app);

We now have to tell Apache that everything should be dispatched to that very CGI script like the following:

<VirtualHost ....>
	<Directory "/path/to/your/app">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
		AddHandler cgi-script .cgi
	</Directory>

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule ^(.*)$ /dancer.cgi [QSA,L]

        [...]
</VirtualHost>

And… we’re done! Every request the Apache server catches is now served by Dancer through Plack::Server::CGI

All I have to say here is … Plack++ (or @miyagawa++ which seems to be the same ;-)

Tags: , ,
Posted in Programming 9 Comments »

Get Adobe Flash playerPlugin by wpburn.com wordpress themes