Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl PSGI does not serve static files automatically

Tags:

perl

psgi

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.

like image 745
daliaessam Avatar asked Mar 15 '26 04:03

daliaessam


1 Answers

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

like image 90
Miguel Prz Avatar answered Mar 16 '26 17:03

Miguel Prz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!