simpleStatic.pl

#!/usr/bin/perl
use strict;
use warnings FATAL => qw(all);

# Implements a basic static page server with the following schema:
# 
# - Web root is '/var/www'.
# - All images are in '/var/www/images'.
# - All stylesheets are in '/var/www/css'.
# - All javascript is in '/var/www/js'.
# - Everything outside of those 3 subdirectories is an html page.

use Sloop::Server;
use Sloop::Static;

# Use client_constructor to set a default max age for all content.
my $sloop = Sloop::Server->new (
    client_constructor => Sloop::Client::Regular->new (
        @_,
        maxage => 21600 # 6 hours
    )
);

$sloop->{handlers} = {
    '/' => sub {
    # Everything that doesn't match one of the other handlers will be here.
        Sloop::Static::directory (
            shift,
            path => '/var/www',
            mimetype => 'text/html; charset=UTF-8'
        );
    },
    css => sub {
        Sloop::Static::directory (
            shift,
            path => '/var/www/css',
            mimetype => 'text/css',
        # Don't index these.
            index => 1
        );
    },
    images => sub {
    # No fixed mimetype here as there may be several kinds of images.
        Sloop::Static::directory (
            shift,
            path => '/var/www/images',
        # Some images get updated more frequently.
            maxage => 3600
        );
    },
    js => sub {
        Sloop::Static::directory (
            shift,
            path => '/var/www/js',
            mimetype => 'application/javascript',
            index => 1
        );
    }
};

$sloop->run;