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!


In reply to Serving Static Content using Plack by Logicus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.