Run smolder on Plack

I want to run Smolder on Plack.Because I'm already running a lot of perl web app on damonetools+Plack::Server::Standalone::Prefork::Server::Starter+nginx, and I want to run Smolder on this envrionment too.

Smolder uses CGI::Application, then, I can port Smolder to Plack, very easy.Because we already have a CGI::Application::Emulate::PSGI.

Then, this is a complete code of smolder.psgi.Enjoy!

use strict;
use warnings;
use File::Spec::Functions qw(catdir catfile);
use File::Path qw(mkpath);
use Smolder::Conf qw(HtdocsDir DataDir);
use Smolder::DB;
use Plack::Builder;
use CGI::Application::Emulate::PSGI;

# initialize by configuration file
# Smolder::Conf->init_from_file($conf);

# initialize by configuration in .psgi
my %conf_values = (
    # DataDir => '',
    # TemplateDir => '',
    # SQLDir => '',
    # HtDocsDir => '',
);
Smolder::Conf->init(%conf_values)    if %conf_values;

# do we have a data directory? If not then create one
if ( not -e DataDir ) {
    mkpath(DataDir) or die sprintf( "Could not create %s: $!", DataDir );
}

# do we have a database? If not then create one
unless ( -e Smolder::DB->db_file ) {
    Smolder::DB->create_database;
}
else {
    # upgrade if we need to
    require Smolder::Upgrade;
    Smolder::Upgrade->new->upgrade();
}

# preload our perl modules
require Smolder::Dispatch;
require Smolder::Control;
require Smolder::Control::Admin;
require Smolder::Control::Admin::Developers;
require Smolder::Control::Admin::Projects;
require Smolder::Control::Developer;
require Smolder::Control::Developer::Prefs;
require Smolder::Control::Projects;
require Smolder::Control::Graphs;
require Smolder::Control::Public;
require Smolder::Control::Public::Auth;
require Smolder::Redirect;

# create PSGI application
builder {
    # handle static files
    enable "Plack::Middleware::Static",
            path => qr{^/(js|style|images|robots\.txt)/}, root => HtdocsDir();

    # inject application
    mount '/app/' => CGI::Application::Emulate::PSGI->handler(
        sub {
            Smolder::Dispatch->dispatch();
        }
    );
    mount '/' => CGI::Application::Emulate::PSGI->handler(
        sub {
            my $smolder = Smolder::Redirect->new();
            $smolder->run();
        }
    );
};

国内ウケわるそうなネタなんで、全部英語でかいたけど、要するに、Smolder だけ plack じゃなくて HTTP::Server::Simple + Net::Server という構成でうごいてるってのがキモチワルイので、Plack の上でうごかしてやったぜ、という話。

Published: 2010-01-10(Sun) 15:46