my_root # top dir of web space
docroot
# html files and the cgi instance scripts
sw
lib
CGI
HTML
# etc.
SW
ProjectClass.pm
Test
TestApp.pm
OtherApps
test
cnf
tmpl
upld
otherapps
cnf
tmpl
upld
####
#!/usr/bin/perl -T
# instance script
# somewhere under docroot
use strict;
use warnings;
#use CGI::Carp qw(fatalsToBrowser);
use File::Spec;
my ($my_root, $project, $lib);
BEGIN {
$ENV{DOCUMENT_ROOT} =~ m|([\w/-]+)/|;
$my_root = $1;
$project = 'sw';
$lib = File::Spec->catdir($my_root, $project, 'lib');
}
use lib $lib;
use SW::Test::TestApp;
$ENV{PATH} = '';
my $c = SW::Test::TestApp->new(
PARAMS => {
'root' => $my_root,
'project' => $project,
'app' => 'test',
},
);
$c->run;
####
package SW::Test::TestApp;
use strict;
use warnings;
use base 'SW::ProjectClass';
sub setup {
my ($self) = @_;
$self->error_mode(
'error_runmode'
);
$self->start_mode(
'm1'
);
$self->mode_param(
path_info=> 1,
param =>'rm'
);
$self->run_modes(
'm1' => 'start',
'm2' => 'show_log_records',
'm5' => 'show_session',
'm7' => 'get_cnf_value',
'm8' => 'force_error',
'm9' => 'send_email_test',
'm10' => 'path_info',
'm11' => 'upload_get',
'm12' => 'upload_process',
);
}
# and then all the run modes
# to exercise each plugin
sub start {
#
}
####
package SW::ProjectClass;
use strict;
use warnings;
use base 'CGI::Application';
use CGI::Simple;
use CGI::Application::Plugin::Config::MyNewt;
use CGI::Application::Plugin::DBH (qw/dbh_config dbh/);
use CGI::Application::Plugin::LogDispatch;
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::SQL::Library;
use MIME::Lite;
sub cgiapp_init {
my $self = shift;
my $app_path = File::Spec->catdir(
$self->param('root'),
$self->param('project'),
$self->param('app')
);
$self->tmpl_path(
File::Spec->catdir($app_path, 'tmpl'),
);
my $cnf_path = File::Spec->catdir($app_path, 'cnf');
my $cnf_file = join('.', $self->param('app'), 'cnf');
$self->config_init(
File::Spec->catfile($cnf_path, $cnf_file),
);
my $dsn = join ('',
"DBI:mysql:;mysql_read_default_file=",
File::Spec->catfile($cnf_path, 'db.cnf'),
);
$self->dbh_config(
$dsn, undef, undef, {RaiseError => 1}
);
$self->sql_lib_config(
File::Spec->catfile($cnf_path, 'sql.lib'),
);
$self->session_config(
CGI_SESSION_OPTIONS =>
[
"driver:MySQL;serializer:Storable",
$self->query,
{Handle=>$self->dbh}
],
DEFAULT_EXPIRY => '+1w',
COOKIE_PARAMS =>
{
-expires => '+24h',
-path => '/'
},
SEND_COOKIE => 1,
);
$self->log_config(
LOG_DISPATCH_MODULES => [
{
module => 'Log::Dispatch::DBI',
name => 'dbi',
dbh => $self->dbh,
table => 'log',
min_level => 'debug',
},
{
module => 'Log::Dispatch::Email::MailSend',
name => 'email',
to => [qw(my@email.com)],
subject => 'Log message from www.mywebsite.org.uk',
min_level => 'emerg'
}
]
);
}
sub cgiapp_get_query {
my $self = shift;
$CGI::Simple::DISABLE_UPLOADS = 0; # enable uploads
$CGI::Simple::POST_MAX = 1_048_576; # allow 1MB uploads
return CGI::Simple->new();
}
sub mime_email {
my ($self, $over_ride) = @_;
my %default = (
From => 'somewhere@mail.com',
To => 'webadmin@website.com',
Subject => 'website enquiry',
Type => 'text/html',
Data => undef,
);
@default{keys %{$over_ride}} = values %{$over_ride};
my $msg = MIME::Lite->new(%default,)
or die "Error creating MIME body: $!\n";
$msg->send or die "Error sending MIME body: $!\n";
return \%default;
}
sub error_runmode{
my ($self, $msg) = @_;
$self->log->emerg("Error: $msg");
my $tmpl_obj = $self->load_tmpl('error.html');
$tmpl_obj->param(msg => $msg,);
return $tmpl_obj->output;
}
1;