tiny_monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, monks. Finally, I was able to generate a web page using PSGI /Plack. However, I have noticed that my CSS failed to work properly. The css file was on my browser, but it was empty. It seems to me that there is a disconnect between PSGI and the document root (/Library/WebServer/Documents) where I currently keep my static files. Below is a snippet of the code that I used,

#!/usr/bin/perl use strict; use warnings; use diagnostics; use template; my $app = sub { my $html = get_html(); return [ 200, ['Content-Type' => 'text/html'], [$html], ]; }; sub get_html{ # open template file open(my $reader,'< :encoding(UTF-8)',"/library/webserver/documents +/html.html"); my $input; do{$input .= $_} while(<$reader>); # Initialize HTML template my $template = Template->new(); my $vars = {greeting => 'Howdy, Cowboy!',debt => '$1.50',sender => + 'John Wayne'}; my $html; $template->process(\$input,$vars,\$html) || die "Template process failed: ", $template->error(), "\n"; return $html; }

The filename of the above code has a .psgi extension and I ran it using the plackup command. I named my HTML template html.html:

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xml:lang='en' xmlns='http://www.w3.org/1999/xhtml' lang='en-US' +> <head> <title>Loan Shark</title> <link rel='stylesheet' type='text/css' href='funky.css' media='' /> </head> <body> <p><span id='try'>[% GET greeting %]</span>, It has come to our attention that your account is in arrears to the sum of [% GET debt %]. Please settle your account before the end of the year or you will face + severe embarrassment. Yours truly, [% sender %]</p> </body> </html>

I used Plack::App::File hoping that my css file would be served properly but it didn't work.

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Plack::App::File; my $app = Plack::App::File->new(root => "/Library/Webserver/Documents" +)->to_app;

After running Plack::App::File, I again checked the browser. The css file contained only a copy of the generated HTML. I tried using Plack::App::Directory and, similarly, yielded the same unsuccessful result. I'd greatly appreciate it if anyone could share an insight into this problem. Thank you.

Replies are listed 'Best First'.
Re: CSS and PSGI
by tangent (Parson) on Aug 19, 2015 at 14:08 UTC
    You will need to configure Plack to serve up static files - there is a good tutorial in the Plack Advent Calendar - see Day 17: Serving static files from your application. It has a few examples.

    If you reference your CSS file as "/static/funky.css" in your template, then this should work:

    use Plack::Builder; my $app = sub { ... }; builder { enable "Static", path => qr!^/static!, root => '/Library/WebServer +/Documents'; $app; }
    Update: minor edits

      tangent, thank you for suggesting a solution. I am a bit, embarrassed for I am not familiar yet with the Perl lingo. I'm afraid that I may have to ask for a little clarification. Does the suggested code have to be run in a new .psgi file? or should I just add the code you suggested to the .psgi file I originally created? Also, what does the 3 dots in the my $app = sub { ...}; mean? Is that also part of the code that I should just write in verbatim? Thank you for being patient.

        Certainly no need to be embarassed, how else do we learn but by asking questions? I could have been a bit clearer too! You just need to add the code to your existing file, the my $app = sub { ... }; is a placeholder for what you have already. The important thing is to put the 'builder' part at the very end, so:
        use strict; use warnings; use diagnostics; use Template; use Plack::Builder; my $app = sub { my $html = get_html(); # etc }; sub get_html { # etc } builder { enable "Static", path => qr!^/static!, root => '/Library/WebServer +/Documents'; $app; }
A reply falls below the community's threshold of quality. You may see it by logging in.