perlpsgi

Perl PSGI does not serve static files automatically


I am practicing with Perl and PSGI/Plack. Just trying the simple PSGI example app:

app.psgi

#!/usr/bin/perl

my $counter = 0;

my $app = sub {
    my $env = shift;
    my $path = $env->{PATH_INFO} || $env->{REQUEST_URI};
    $counter++;
    my $content = "Hellow world.\nCounter=$counter\nPath: $path\n";
    return [ 200, [ 'Content-type', 'text/plain' ], [ $content ] ]
};

then run it by:

plackup app.psgi

if I point the browser to any path like /news/world:

http://localhost:5000/new/world

this is ok, I get the $path variable set to /news/world and I will handle the cgi response.

the problem if I point to static files like logo.png

http://localhost:5000/logo.png

I also get the $path variable set to /logo.png

The question is, why the plackup server does not serve the static image file logo.png automatically.

Do I have to do this manually? if so does that means on every request I have to ping the file system first check if -f $path.

This means I am building a complete server handler not just my script handler. What I am not understanding.


Solution

  • Use the Plack::Middleware::Static module. It allows your app to serve static files from root directory.