Preamble

As has been pointed out, PSGI/Plack is not just a pure perl HTTP server, but also a sort of universal ball-and-socket system for connecting many different types of web systems together in a precise, unified and standardised way.

Having said that, it is entirely possible, and also quite a good idea, to use Plack and Starman as a complete replacement for your existing web server system (I.E Apache), since it is both blazingly fast and very easy to get working.

Serving static content

So we want to serve up things like css files, images, javascript and standard html files using Plack and Starman.

The proceedure is the same as I covered in the previous thread entitled "meet joe plack".

We are going to be using two Plack modules to acheive this effect; Plack::Builder and Plack::App::File.

listing of action.psgi ---------------------- use Plack::Builder; use Plack::App::File; my $css = Plack::App::File->new(root => "/var/www/css"); my $js = Plack::App::File->new(root => "/var/www/js"); my $images = Plack::App::File->new(root => "/var/www/images"); my $ico = Plack::App::File->new(root => "/var/www"); my $html = Plack::App::File->new(root => "/var/www/htdocs"); my $app = builder { mount "/images" => $images; mount "/css" => $css; mount "/js" => $js; mount "/favicon.ico" => $ico; mount "/" => $html; };

So what's going on here then?

First of all, Plack::App::File takes the request URL and maps it to a standard <fh> onto that file.

Then Plack::Builder mounts the various url paths as specified using the builder DSL (domain specific language).

That's it! It really is as simple as that... using the above code you now have a complete replacement for your existing server capable of serving up static content with lightening speed. You can run the code using the same command line tool described in the last post;

plackup -s Starman -r action.psgi

You may want to serve dynamic content and or support multiple domain names, and I will be back with some more recipies and hints at how to do that in later posts!

Replies are listed 'Best First'.
Re: Serving Static Content using Plack
by jdrago999 (Pilgrim) on Oct 22, 2011 at 17:35 UTC

    Thanks for a nice, working example of how that comes together.

      Thank you. I'd been failing with Static, this recipe was simpler, comprehensible and actually worked for me!