Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

mod_perl & TT2 Architecture

by tadamec (Beadle)
on May 24, 2004 at 23:43 UTC ( [id://356084]=perlquestion: print w/replies, xml ) Need Help??

tadamec has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Monks!

I have a question regarding the architecture of web applications, particularly under mod_perl using the Template Toolkit.

In the past, applications I've worked on have ended up a jumble of application and presentation logic. I'd like to avoid the trap this time. I was wondering if I could get some input from the rest of you out there who've worked with this combination for effective design strategies.

I've been tasked to come up with an administration area for an "E-Business" and, possibly, redo their entire front end and shopping cart. The current application is written in ASP and is a nightmare of embedded HTML and sometimes-global sometimes-local variables. Making a minor tweak sometimes takes a week or two.

In order to make the site easily expandable, and keep related business logic together, I thought I'd make a generalized Apache handler to take care of sessions and user authentication, and then come up with some sort of dispatching scheme to will map URLs to Perl modules and methods. For example, "http://www.example.com/login" would be handled by the generalized Apache handler, and methods like "http://www.example.com/foo/bar" would be handled by a call to Foo->bar().

Before I commit to this radical change, I'd like to know if anybody has done something similar and, if so, how well did it work out for you? Is there a better method? Am I entirely off base?

Thanks for you input!

Replies are listed 'Best First'.
Re: mod_perl & TT2 Architecture
by perrin (Chancellor) on May 25, 2004 at 00:40 UTC
    You can read my article about how we did this for one application. In general, read some stuff about MVC (model-view-controller) for the web, look at some of the frameworks out there, and see what you like.

    By the way, there is no need to invent something that maps URLs to Perl modules. Apache already handles every conceivable mapping through httpd.conf, and if you run mod_perl you can use things like Apache::Dispatch to simplify it even more.

Re: mod_perl & TT2 Architecture
by lachoy (Parson) on May 25, 2004 at 00:12 UTC

    Not offbase at all. My standard recommendations are:

    • Going lightweight? Try Maypole (Simon's written a couple of articles on perl.com as well.)
    • Want something more heavyweight? (Authentication, security, distributed apps) Try OpenInteract (I'm biased about this one, naturally...)

    Chris
    M-x auto-bs-mode

Re: mod_perl & TT2 Architecture
by dragonchild (Archbishop) on May 25, 2004 at 01:40 UTC
    Another option is to use CGI::Application. While it is immediately set up to work under mod_registry and using HTML::Template, it is not very difficult at all to get it to work as mod_perl handlers and TT2. This is what I'm using right now and it's very slick.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: mod_perl & TT2 Architecture
by EvdB (Deacon) on May 25, 2004 at 07:45 UTC
    perrin metions using Apache::Dispatch, which is a good idea. However it is fairly easy to achieve in the code. The main advantage is that all the magic happens in the code and not in the httpd.conf file. This is good as it makes installation easier and you can kick in a whole load of error catching mechanisms.
    package Handler; use strict; use warnings; my %handlers = ( '/' => 'Handler::Index', '/logout' => 'Handler::Logout', '/details' => 'Handler::Details', ); sub handler ($$) { my $self = shift; my $r = shift; $self = $self->new( r => $r ) unless ref $self; my $uri = $r->uri; # User auth etc. can happen here. if ( my $handler = $handlers{ $uri } ) { eval "require $handler" || die "Error with $handler"; return $handler->handler( $r ); } # If not on list then it was not found. return NOT_FOUND; } ######### # httpd.conf <Location /> SetHandler perl-script PerlHandler Handler </Location>
    If any one has any comments on this I'd love to hear them.

    --tidiness is the memory loss of environmental mnemonics

      What have you accomplished by doing this? You've moved the configuration from a configuration file into your code. What you're doing here could be replaced by this snippet of conf file:
      <Location /> SetHandler perl-script PerlHandler Handler::Index </Location> <Location /logout> SetHandler perl-script PerlHandler Handler::Logout </Location> <Location /details> SetHandler perl-script PerlHandler Handler::Details </Location>
      Or, with mod_macro installed:
      <Macro MapURL $url $handler> <Location $url> SetHandler perl-script PerlHandler $handler </Location> </Macro> Use MapURL / Handler::Index Use MapURL /logout Handler::Logout Use MapURL /details Handler::Details
        Well - it looks prettier for a start...

        One advantage is that if the modules are distributed to others then the amount that they need to type into the httpd.conf file is reduced. It also means that the module authors get control of the url layout and so can change it without ever changing the httpd.conf file - ie easy upgrades. For something like a drop in blog or mail solution this would be vital.

        Another reason is that you can then ensure that the user has jumped through some hoops before letting them get as far as the next modules. This could be authenticating them or setting up variables based on their preferences.

        Also the layout does not need to come from a hash, it could come from an XML file. This opens up the possibility of allowing plugins to be added to a site automatically - the plugin would register itself in the XML and the handler would then know that it could dispatch to it. There is more needed to make this work (ie modding templates etc) but it allows for it.

        Finally it allows the _possibility_ of running the same code as a plain CGI (although the $r would have to be faked up). This would not be trivial but there would be one less obstacle.

Re: mod_perl & TT2 Architecture
by tadamec (Beadle) on May 25, 2004 at 04:46 UTC

    Thanks to everybody for their responses. I'll definately check out OpenInteract and CGI::Application. The article also looks really good; I don't know how I missed it on the mod_perl site.

    The accounting side of the application works fairly well via a set of stored procedures under SQL Server 2000, so I wanted to use that as a base as we built further functionality. Unfortunately, Class::DBI doesn't play well with the current release of FreeDTS and SQL Server; evidently bind variables aren't supported. This kind of takes Maypole out of the running unless I can reimpliment the T-SQL logic fairly quickly with perl modules.

    Great advice, and thanks again!

Re: mod_perl & TT2 Architecture
by tilly (Archbishop) on May 25, 2004 at 18:55 UTC
    Sorry to respond late, but better late than never.

    If you keep on having trouble on this kind of problem, then perhaps it would be valuable to do some post mortems, think about why past projects went wrong rather than trying to come up with idealized plans for how to do better.

    It doesn't matter how good the design that you come up with is if you can't get people to buy into it. Or if people say that they buy into it and then erode the design. Or if you erode the design and only afterwards realize that you have done so.

    At Re (tilly) 6: Code Critique I pointed out some of the factors which come into play and cause our well-laid plans to fall apart. You might read that, think about the past, and see if you can identify how to head off potential problems before they become real ones.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://356084]
Approved by rlb3
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found