Qiang Li |
|
/usr/local/team/htdocs/ such as images/ , js/cvs update project_name
Alias /app1/ /usr/local/team/app1/htdocs/
<Directory "/usr/local/team/app1/htdocs">
<FilesMatch "^.*\.pl$">
Options +ExecCGI
SetHandler cgi-script
SetENV APP_ROOT /usr/local/team/app1
</FilesMatch>
</Directory>SetENV APP_ROOT /usr/local/team/app1/?$ENV{APP_ROOT} in your app to locate things. such as:
$self->tt_config(
TEMPLATE_OPTIONS => {
INCLUDE_PATH => "$ENV{APP_ROOT}/templates/",
})
# product.pl use MyApp::Product; my $webapp = WebApp->new(); $webapp->run();
# MyApp/Product.pm
package MyApp::Product;
use base 'CGI::Application';
sub setup {
my $self = shift;
$self->run_modes( [ qw(
list_products
view_product
save_product)
] );
}
sub list_products { # do stuff }
sub view_product { # do stuff }
sub save_product { # do stuff }
##### product.pl
use CGI qw(:standard);
my $page = param('page');
# do some header stuff here
if ($page eq 'list_products') {
&list_products;
} elsif ($page eq 'view_product') {
&view_product;
} elsif ($page eq 'save_product') {
&save_product;
} else {
# display a default page
}
report.pl ( the front-end script )
|
|
v
report.pm
( handle http requests,
dispatch actions to helper module )
| \
| \
v v
chk_acl.pm make_report.pm
( doesn't need to know about HTTP at all )
customer.pl product.pl order.pl
| | |
| | |
v v v
customer.pm product.pm order.pm
(check_acl,event_logging) (..) (..)
| | |
| | |
v v v
customer_related.pm product._related.pm order_related.pm
create/delete/edit customer create/delete/edit product create;/delete/edit order
customer.pl product.pl order.pl
| | |
| | |
v v v
-----------------------------------------------------------------------
| Base.pm ( check_acl, event_logging ) |
-----------------------------------------------------------------------
|
| Inherit check_acl,
| event_logging from Base.pm
v
--------------------------------------------------------------------------
customer.pm product.pm order.pm |
--------------------------------------------------------------------------
| | |
| | |
v v v
customer_related.pm product._related.pm order_related.pm
create/delete/edit customer create/delete/edit product create;/delete/edit order
package MyApp::Base;
use base 'CGI::Application';
sub cgiapp_prerun {
my $self = shift;
my $run_mode = shift;
# log action before it is executed
$self->event_log("$user is executing $run_mode");
# authorization - can user execute this runmode ?
my $authorized =
$self->check_permission_on_runmode( $user, $run_mode );
# user has no access, redirect to forbidden runmode
$self->prerun_mode('forbidden') unless $authorized ;
}
# another example
our $TEMPLATE_OPTIONS = {
INCLUDE_PATH => "$ENV{APP_ROOT}/templates",
};
MyApp::Base->tt_config( TEMPLATE_OPTIONS => $TEMPLATE_OPTIONS );
sub my_runmode {
my $self = shift;
# $query is the CGI object.
$query = $self->query;
}
package MyApp::Product;
sub list_products {
my $self = shift;
my $products = get_products();
return $self->tt_process( "my.tmpl" ,
{ products => $products } );
# OR process template
# 'MyApp/Product/list_products.tmpl'
# return $self->tt_process(
{ products => $products } );
}
use CGI::Application::Plugin::Stream (qw/stream_file/);
sub excel_report {
my $self = shift;
$self->header_add( -type => 'application/x-sc',
-attachment => "myfile.xls" );
$self->stream_file("/path/to/myfile.xls");
}
package MyApp::EnvConfig; use strict; use warnings; our $DBName = 'dbi:Oracle:qa'; our $DBUser = 'user'; our $DBPass = 'password'; 1;
package MyApp::Conf;
use base 'YuSis::Conf';
BEGIN {
__PACKAGE__->load_config(
"$ENV{APP_ROOT}/conf/my.conf" )
}
1;
package MyApp::Product;
use MyApp::Conf qw/MyConfig/;
print MyConfig;
package MyApp::DB::Product;
use base 'MyApp::DB';
sub list_products {
my $self = shift;
my $sql = q{ select id, name from product };
my $products =
$self->dbh->selectcol_arrayref( $sql,
{ Columns=>[1,2] });
# or use a DBIx::Simple handler
# my $products =
# $self->db->query( $sql )->map;
return $products; # { id => name. }
}
package MyApp::DB;
use DBI;
use DBIx::Simple;
use MyApp::EnvConfig;
sub dbh {
return DBI->connect_cached( $MyApp::EnvConfig::DBName,
$MyApp::EnvConfig::DBUser,
$MyApp::EnvConfig::DBPass,
{ RaiseError => 1, PrintError => 0 }
);
}
sub db {
return DBIx::Simple->new( dbh() );
}
1;
package MyApp::Base;
use base 'CGI::Application';
use YuSis::CAP::EventLogging qw/elog elog_config/;
sub cgiapp_prerun {
my $self = shift;
$self->elog_config(
{ dbh => MyApp::DB->dbh,
uname => $uname,
module_name => 'MyApp',
});
$self->elog->log_event('log_in_ok')
if user_is_valid() ;
}
project_skeleton.pl project_nameMyApp/lib/MyApp/Controller_1.pmMyApp/lib/MyApp/Controller_2.pmMyApp/lib/MyApp/DB/Model_1.pmMyApp/lib/MyApp/DB/Model_2.pm

no... that is not my baby :-)
http://www.perl.com/lpt/a/549http://www.perl.com/lpt/a/984