четверг, 4 февраля 2010 г.

mason + lighttpd fastcgi

включаем mason в lighttpd

сначало готовим скрипт mason.cgi


#!/usr/bin/perl
use CGI::Fast;
use HTML::Mason::CGIHandler;
use URI;

{
package HTML::Mason::Commands;

## anything you want available to components
use Storable qw(freeze thaw);
use HTTP::BrowserDetect;

## An example of how to keep a $dbh persistent
##our $dbh = DBI->connect_cached(
## 'dbi:Pg:dbname=dbname'
## , 'username'
## , 'password'
## , {AutoCommit=>0, RaiseError=>1, PrintError=>1}
##) || die "Could not Connect to DB".$dbi::errstr ;

}

# lazily-instantiated variables
my $cgi;
my $h;

while ($cgi = new CGI::Fast())
{
## make sure it is alive! (if not it will reconnect)
## $HTML::Mason::Commands::dbh->ping;

my $uri = URI->new( $ENV{REQUEST_URI} );

## this is a hack, that emulates mod_perl behavior see notes at bottom
## You might not want this hack, and it might be worse than not having it
$uri->path( $uri->path . 'index.html' )
if $uri->path =~ /\/$/
;

$ENV{PATH_INFO} = $uri->path;
$ENV{QUERY_STRING} = $uri->query;
$ENV{REQUEST_URI} = "$uri";

# this is lazily instantiated because %ENV is not set at startup time
if (! $h) {
$h = HTML::Mason::CGIHandler->new(
comp_root => $ENV{MASON_COMP_ROOT}
, data_dir => $ENV{MASON_DATA_ROOT}
, error_mode => 'fatal'
, error_format => 'line'
## Three good globals dbh user and session
, allow_globals => [qw/$dbh $U $S/]
);
}

## hand off to mason
eval { $h->handle_cgi_object($cgi) };

## catch error
if ( my $raw_error = $@ ) {
warn $raw_error;
# print out a pretty system error page and log $raw_error
}
## things went well
else {
$HTML::Mason::Commands::dbh->commit;
}

}

exit 0;


добавляем в lighttpf.conf

fastcgi.map-extensions = ( ".css" => ".html", "/" => ".html" )
        index-file.names = ( "index.html" )
        url.access-deny = ( ".mhtml" ) ## add autohandler/dhandler if you'd like
        fastcgi.server = ( ".html" =>
        ((
                "socket" => "/tmp/fastcgi.socket",
                "bin-path" => "/etc/lighttpd/bin/mason.cgi",   # пусть до скрипта
                "check-local" => "disable",
                "bin-environment" => (
                "MASON_COMP_ROOT" => "/www/myproject",
                "MASON_DATA_ROOT" => "/www/myproject/data",
                ),
        ))
        )


index.html кладем в comp_root
простенький helloworld
<HTML>
   <HEAD>
   <TITLE> Hello <% $worldname %> world!!</TITLE>
   </HEAD>
   <BODY bgcolor="#FFFFFF">
   Hello World<BR>
   This is my <B><% $count %></B> Mason component.
   </BODY>
</HTML>
<%init>
   my $worldname = "Great";
   my $count = "first";
</%init>
<%args>
</%args>