Created
June 21, 2010 14:29
-
-
Save yko/446924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Lighter; | |
use strict; | |
use warnings; | |
use base 'Mojo::Base'; | |
#use Mojolicious::Commands; | |
use Mojolicious::Plugins; | |
use MojoX::Dispatcher::Routes; | |
use MojoX::Dispatcher::Static; | |
use MojoX::Renderer; | |
#use MojoX::Session::Cookie; | |
use MojoX::Types; | |
__PACKAGE__->attr(controller_class => 'Lighter::Controller'); | |
__PACKAGE__->attr(mode => sub { ($ENV{MOJO_MODE} || 'development') }); | |
__PACKAGE__->attr(plugins => sub { Mojolicious::Plugins->new }); | |
__PACKAGE__->attr(renderer => sub { MojoX::Renderer->new }); | |
__PACKAGE__->attr(routes => sub { MojoX::Dispatcher::Routes->new }); | |
__PACKAGE__->attr( | |
secret => sub { | |
my $self = shift; | |
# Warn developers about unsecure default | |
# $self->log->debug('Your secret passphrase needs to be changed!!!'); | |
# Application name | |
return ref $self; | |
} | |
); | |
use Mojo::Transaction::HTTP; | |
#__PACKAGE__->attr(session => sub { MojoX::Session::Cookie->new }); | |
__PACKAGE__->attr(static => sub { MojoX::Dispatcher::Static->new }); | |
__PACKAGE__->attr(types => sub { MojoX::Types->new }); | |
__PACKAGE__->attr( | |
build_tx_cb => sub { | |
sub { return Mojo::Transaction::HTTP->new } | |
} | |
); | |
#use Mojo::Home; | |
use Mojo::Log; | |
#__PACKAGE__->attr(home => sub { Mojo::Home->new }); | |
__PACKAGE__->attr(log => sub { Mojo::Log->new }); | |
our $CODENAME = 'Snowman'; | |
our $VERSION = '0.999925'; | |
sub new { | |
my $self = shift->SUPER::new(@_); | |
# Eat this! Haha! | |
$self->routes->controller_base_class('Lighter::Controller'); | |
# Transaction builder | |
$self->build_tx_cb( | |
sub { | |
my $self = shift; | |
# Build | |
my $tx = Mojo::Transaction::HTTP->new; | |
# Hook | |
$self->plugins->run_hook_reverse(after_build_tx => $tx); | |
return $tx; | |
} | |
); | |
# Namespace | |
$self->routes->namespace(ref $self); | |
# Types | |
$self->renderer->types($self->types); | |
# $self->static->types($self->types); | |
# Root | |
$self->renderer->root( | |
#$self->home->rel_dir( | |
'templates' #) | |
); | |
# $self->static->root($self->home->rel_dir('public')); | |
# Hide own controller methods | |
$self->routes->hide(qw/client cookie finish finished flash helper/); | |
$self->routes->hide(qw/param pause receive_message redirect_to render/); | |
$self->routes->hide(qw/render_data render_exception render_inner/); | |
$self->routes->hide(qw/render_json render_not_found render_partial/); | |
$self->routes->hide(qw/render_static render_text resume send_message/); | |
$self->routes->hide(qw/session signed_cookie url_for/); | |
# Mode | |
my $mode = $self->mode; | |
# Log file | |
# $self->log->path($self->home->rel_file("log/$mode.log")) | |
# if -w $self->home->rel_file('log'); | |
# Plugins | |
# $self->plugin('agent_condition'); | |
$self->plugin('default_helpers'); | |
$self->plugin('epl_renderer'); | |
$self->plugin('ep_renderer'); | |
# $self->plugin('request_timer'); | |
# $self->plugin('powered_by'); | |
# Reduced log output outside of development mode | |
# $self->log->level('error') unless $mode eq 'development'; | |
# Run mode | |
$mode = $mode . '_mode'; | |
$self->$mode(@_) if $self->can($mode); | |
# Startup | |
$self->startup(@_); | |
return $self; | |
} | |
# The default dispatchers with exception handling | |
sub dispatch { | |
my ($self, $c) = @_; | |
# Session | |
# $self->session->load($c); | |
# Hook | |
$self->plugins->run_hook(before_dispatch => $c); | |
# New request | |
my $path = $c->req->url->path || '/'; | |
my $ua = $c->req->headers->user_agent || 'Anonymojo'; | |
# $self->log->debug(qq/*** Request for "$path" from "$ua". ***/); | |
# Try to find a static file | |
# my $e = $self->static->dispatch($c); | |
# Hook | |
# $self->plugins->run_hook_reverse(after_static_dispatch => $c); | |
# Use routes if we don't have a response yet | |
my $e = $self->routes->dispatch($c);# if $e; | |
# Exception | |
if (ref $e) { $c->render_exception($e) } | |
# Nothing found | |
elsif ($e) { $c->render_not_found } | |
# Finish | |
$self->finish($c); | |
} | |
sub finish { | |
my ($self, $c) = @_; | |
# Already finished | |
return if $c->stash->{finished}; | |
# Paused | |
return if $c->tx->is_paused; | |
# Hook | |
$self->plugins->run_hook_reverse(after_dispatch => $c); | |
# Session | |
# $self->session->store($c); | |
# Finished | |
$c->stash->{finished} = 1; | |
} | |
# Bite my shiny metal ass! | |
sub handler { | |
my ($self, $tx) = @_; | |
# Load controller class | |
my $class = $self->controller_class; | |
if (my $e = Mojo::Loader->load($class)) { | |
# $self->log->error( | |
# ref $e | |
# ? qq/Can't load controller class "$class": $e/ | |
# : qq/Controller class "$class" doesn't exist./ | |
# ); | |
} | |
# Embedded application | |
my $stash = {}; | |
if ($tx->can('stash')) { | |
$stash = $tx->stash; | |
$tx = $tx->tx; | |
} | |
# Build default controller and process | |
eval { | |
$self->process($class->new(app => $self, stash => $stash, tx => $tx)); | |
}; | |
# $self->log->error("Processing request failed: $@") if $@; | |
} | |
sub plugin { | |
my $self = shift; | |
$self->plugins->load_plugin($self, @_); | |
} | |
# This will run for each request | |
sub process { shift->dispatch(@_) } | |
# Start command system | |
sub start { | |
my $class = shift; | |
# We can be called on class or instance | |
# $class = ref $class || $class; | |
# We are the application | |
# $ENV{MOJO_APP} ||= $class; | |
# Start! | |
# return Mojolicious::Commands->start(@_); | |
} | |
# This will run once at startup | |
sub startup { } | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment