Skip to content

Instantly share code, notes, and snippets.

@cybersiddhu
Created August 3, 2009 15:53
Show Gist options
  • Save cybersiddhu/160629 to your computer and use it in GitHub Desktop.
Save cybersiddhu/160629 to your computer and use it in GitHub Desktop.
Application module for DictyREST
package DictyREST;
use strict;
use warnings;
use base 'Mojolicious';
use Config::Simple;
use Carp;
use File::Spec::Functions;
use DictyREST::Renderer::TT;
use DictyREST::Renderer::JSON;
use DictyREST::Helper;
__PACKAGE__->attr( 'config', default => sub { Config::Simple->new() } );
__PACKAGE__->attr('template_path');
__PACKAGE__->attr( 'has_config', default => 0 );
__PACKAGE__->attr( 'helper', default => sub { DictyREST::Helper->new() } );
# This will run once at startup
sub startup {
my ($self) = @_;
my $router = $self->routes();
#routing setup
my $base = $router->namespace();
$router->namespace( $base . '::Controller' );
$router->route('/gene/:id/:tab/:subid/:section')
->to( controller => 'tab', action => 'sub_section' );
$router->route('/gene/:id/:tab/:section')
->to( controller => 'tab', action => 'section' );
$router->route('/gene/:id/:tab')
->to( controller => 'page', action => 'tab' );
$router->route('/gene/:id')->to( controller => 'page', action => 'index' );
#config file setup
$self->set_config();
#set up various renderer
$self->set_renderer();
}
#set up config file usually look under conf folder
#supports similar profile as log file
sub set_config {
my ( $self, $c ) = @_;
my $folder = $self->home->rel_dir('conf');
if ( !-e $folder ) {
return;
}
$self->log->debug(qq/got folder $folder/);
#now the file name, default which is developmental mode resolves to <name>.conf. For
#test and production it will be <name>.test.conf and <name>.production.conf respectively.
my $mode = $self->mode();
my $suffix = '.conf';
if ( $mode eq 'production' or $mode eq 'test' ) {
$suffix = '.' . $mode . '.conf';
}
opendir my $conf, $folder or confess "cannot open folder $!:$folder";
my ($file) = grep {/$suffix$/} readdir $conf;
closedir $conf;
$self->log->debug(qq/got config file $file/);
$self->config->read( catfile( $folder, $file ) );
$self->has_config(1);
}
sub set_renderer {
my ($self) = @_;
#try to set the default template path for TT
#keep in mind this setup is separate from the Mojo's default template path
#if something not specifically is not set it defaults to Mojo's default
$self->template_path( $self->renderer->root );
if ( $self->has_config and $self->config->param('default.template_path') ) {
$self->template_path( $self->config->param('default.template_path') );
}
my $tpath = $self->template_path;
$self->log->debug(qq/default template path for TT $tpath/);
my $tt = DictyREST::Renderer::TT->new(
path => $self->template_path,
option => {
PRE_PROCESS => $self->config->param('genepage.header') || '',
POST_PROCESS => $self->config->param('genepage.footer') || '',
},
);
my $json = DictyREST::Renderer::JSON->new();
$self->renderer->add_handler(
tt => $tt->build(),
json => $json->build(),
);
$self->renderer->default_handler('tt');
}
1;
__END__
=head1 NAME
DictyREST - Web Framework
=head1 SYNOPSIS
use base 'DictyREST';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action')
->to(controller => 'foo', action => 'bar');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment