Posts Tagged ‘Plack’

PSGI/Plack the ultimate Perl Web Server

Google has to realize like we all do, that Plack is the utlimate Perl Web Server.

The first slot will be hard to reach though, because of that sourceforge page which has “perlwebserver” in its hostname.

Tags: , ,
Posted in Programming Comments Off

Getting rid of CGI.pm in Dancer’s code

Yes, I finally took the decision to stop using CGI.pm in Dancer’s code. It was not an easy decision to take, mainly because of the following reasons:

  • it’s in the core (I’m a “less-CPAN-deps-ships-better” freak when it’s about Dancer!)
  • it provides a stable and well-known way of dealing with the HTTP interface (params, methods, path infos …)
  • and well, to add a third reason, why should I want to change it if it works?

Well, actually, we did find a bug recently in CGI::PSGI that prevented a Dancer app from being deployed under Plack::Server::Apache2. Oh, of course, the bug was almost instantly fixed when it has been discovered (the #plack people are really reactive).

But that enlightened something: CGI.pm is old, really old, and that means, when you’re using it, you take with you a lots of lines of code coming from years ago. Moreover it is well known that CGI.pm does addresses different matters, such as providing a request interface and generating HTML content as well. Conceptually, it sucks.

After thinking about all that, I decided to drop CGI.pm out of Dancer’s code. I then have the option to use the pretty fresh Plack::Request interface. It looks really nice and does exactly what you can expect from it.

But, it’s important for me that Dancer can be used without Plack. I want the dependency barrier to be as low as possible:
If a user wants to test Dancer, he should not have to install Plack in my opinion. If he likes it, and wants to goes on production, then yes, Plack is a must-go. He then installs Plack and goes on with Dancer’s app.psgi.

So depending on Plack::Request was one more dependency I’d rather not add to Dancer.

With all this in mind I came to the decision to write Dancer::Request, reading carefully the PSGI specs (miyagawa++ again).

It was all good except for processing the POST data. There’s so much work to do when it’s about multipart form, url-encoded form, or even file uploads that I chose not to push the “reinvent-the-wheel” thing too far, and I decided to copycat Plack::Request on this topic: using HTTP::Body.

This module does the job pretty well, with an object-oriented interface.

The conclusion might look like a failure (I rewrote something that worked by adding a new dependecy.) But deep inside I’m happy with this refactoring; because Dancer is now on a way where all of its code will be fresh, based on PSGI specs and won’t ever rely on CGI.pm to do something.

I hope Dancer’s users/developers will agree with that. feel free to comment on this post if you want to share your point of views. Yes, I know this might trigger a troll or two… ;)

Tags: , ,
Posted in Programming 4 Comments »

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 »

Mounting a Dancer app into Apache + PSGI/Plack

EDIT : This is going to change as soon as CGI::PSGI is available, the “patched CGI” way won’t be needed anymore, see miyagawa’s post for details

Now that Dancer 0.9904 is released, I’ve took the time to upgrade my Dancer configuration for the official website from a proxy_balancer configuration to an Apache2 + PSGI/Plack architecture.

Here is how to proceed if you like to power your Dancer app up with Apache and PSGI/Plack.

First of all, you need Plack, at the time of this writing, it’s not yet in CPAN, so you should go to miyagawa’s place. Grab also his patched version of CGI.pm. Install both of them and you’re ready to continue.

Next step is to make sure your app is ready for a Plack run, the best thing to do is to run the helper that comes with 0.9904, it will write all the files needed in your application directory if they’re not already present.

Here’s all the files you should have:

$ dancer -a mywebapp
+ mywebapp
+ mywebapp/views
+ mywebapp/views/index.tt
+ mywebapp/views/layouts
+ mywebapp/views/layouts/main.tt
+ mywebapp/environments
+ mywebapp/environments/development.yml
+ mywebapp/environments/production.yml
+ mywebapp/config.yml
+ mywebapp/mywebapp.pm
+ mywebapp/mywebapp.pl
+ mywebapp/app.psgi
+ mywebapp/public
+ mywebapp/public/css
+ mywebapp/public/css/style.css
+ mywebapp/public/css/error.css
+ mywebapp/public/images
+ mywebapp/public/404.html
+ mywebapp/public/500.html

Note that your route handlers should be defined in mywebapp.pm, and not directly in the standalone server mywebapp.pl.

You should also have an app.psgi file in your application directory.

Make sure everything is allright by running the standalone webserver:

$ ./mywebapp.pl
>> Listening on 127.0.0.1:3000
== Entering the development dance floor ...

Ok, now run it with Plack’s standalone server, to make sure the PSGI file is ok:

$ /path/to/Plack/scripts/plackup &
$ curl -I http://localhost:8080/
HTTP/1.0 200 OK
Content-Length: 0
Content-Type: text/html

Great, you’re ready to go. The last step is to mount the application in your Apache configuration, for instance with a VirtualHost:

<VirtualHost webapp.domain.com>
    ServerName webapp.domain.com
    DocumentRoot /srv/webapp.domain.com/public

    <Location />
        SetHandler perl-script
        PerlHandler Plack::Impl::Apache2
        PerlSetVar psgi_app /srv/webapp.domain.com/app.psgi
    </Location>
</VirtualHost>

Restart Apache and enjoy your dances.

Tags: , , ,
Posted in Programming 2 Comments »

Perl Dancer 0.9904 released

I’ve just published a new version of Dancer. It’s basically a PSGI/Plack support release but also provides a new piece of sugar: the bootstraping helper.

One can now create a new application from scractch with a single command, as explained on the website.

A big thank to Tatsuhiko Miyagawa who helped me when I was working on the PSGI/Plack support and to Sebastien Deseille who worked on the helper script.

Next step is to write some documentation about the Apache/PSGI integration and prepare the slides for the upcoming OSDCfr event where I’ll be talking about Dancer.

Tags: , , ,
Posted in Programming Comments Off

Get Adobe Flash playerPlugin by wpburn.com wordpress themes