Created
September 15, 2009 09:12
-
-
Save miyagawa/187200 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 Sledge::Pages::PSGI; | |
use strict; | |
sub import { | |
my $pkg = caller; | |
no strict 'refs'; | |
*{"$pkg\::dispatch_psgi"} = \&dispatch_psgi; | |
} | |
sub dispatch_psgi { | |
my($self, $page) = @_; | |
local *Sledge::Registrar::context = sub { $self }; | |
Sledge::Exception->do_trace(1) if $self->debug_level; | |
my($status, $headers, $content) = | |
( 400, [ ], "Bad Request" ); | |
eval { | |
$self->init_dispatch($page); | |
$self->invoke_hook('BEFORE_DISPATCH') unless $self->finished; | |
if ($self->is_post_request && ! $self->finished) { | |
my $postmeth = 'post_dispatch_' . $page; | |
$self->$postmeth() if $self->can($postmeth); | |
} | |
unless ($self->finished) { | |
my $method = 'dispatch_' . $page; | |
$self->$method(); | |
$self->invoke_hook('AFTER_DISPATCH'); | |
} | |
$status = 200; | |
while (my($k, $v) = each %{$self->{header_hash}}) { | |
$v = [ $v ] unless ref $v eq 'ARRAY'; | |
push @$headers, $k, $_ for @$v; | |
} | |
$content = $self->make_content; | |
push @$headers, "Content-Length", length $content; | |
push @$headers, "Content-Type", $self->charset->content_type; | |
$self->invoke_hook('AFTER_OUTPUT'); | |
$self->finished(1); | |
}; | |
if ($@) { | |
$content = "$@"; | |
} | |
$self->handle_exception($@) if $@; | |
$self->_destroy_me; | |
return [ $status, $headers, [ $content ] ]; | |
} | |
1; | |
__END__ | |
=head1 NAME | |
Sledge::Pages::PSGI - Run Sledge app on PSGI stack | |
=head1 SYNOPSIS | |
# your Pages.pm | |
package MyApp::Pages; | |
use Sledge::Pages::PSGI; | |
# index.psgi | |
use CGI::PSGI; | |
use MyApp::Pages::Hello; | |
sub { | |
my $env = shift; | |
local *ENV = $env; | |
MyApp::Pages::Hello->new->dispatch_psgi('index'); | |
} | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment