Serving Files with Perl/Plack

This post isn’t all that special, but it’s definitely a demo of how far Perl is coming along these days. It’s still a popular language that people have tired of complaining about, so they don’t, but everyone uses it when they need to finish something quickly.

Regardless, a question was posted around my office about how to serve some files. I decided to show my Perl solution:

# File: app.psgi
use Plack::App::File;

my $app = Plack::App::File->new(root => '/some/root')->to_app;

$app

Finally:

$ plackup app.psgi

With a few options in a init file, you could use something like the following:

$ plackup --daemonize --server=Starman app.psgi

If you wanted to serve several directories, you can change your app.psgi to cascade through a list of directories to search for the file:

# File: app.psgi
use Plack::App::File;

my $app = Plack::App::Cascade->new(
    Plack::App::File(root => '/some/root')->to_app,
    Plack::App::File(root => '/another/root/')->to_app,
);

$app

And then the same commands just work. If you have another ‘Plackable’ application (as I’ve taken to calling them), you can do the following with cascade:

# File: app.psgi
use Plack::App::File;

# If you create a new Catalyst app with Catalyst.pl, just look in
# the application.psgi file created in the Catalyst application root.
my $app = Plack::App::Cascade->new(
    Some::Other::Application->as_plack_application,
    Plack::App::File(root => '/some/root')->to_app,
    Plack::App::File(root => '/another/root/')->to_app,
);

$app

This could make lots of things that seem hard very, very easy.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: