Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created November 6, 2009 01:11
Show Gist options
  • Save miyagawa/227584 to your computer and use it in GitHub Desktop.
Save miyagawa/227584 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use lib $ENV{MT_HOME} ? "$ENV{MT_HOME}/lib" : 'lib';
use MT::Bootstrap ();
use MT::App::CMS;
use CGI::PSGI;
use Plack::Builder;
use Plack::App::URLMap;
use Plack::App::File;
my $mt_app = sub {
my $app_class = shift;
eval "require $app_class";
return sub {
my $env = shift;
my $cgi = CGI::PSGI->new($env);
local *ENV = $env; # some MT::App method needs this
my $app = $app_class->new( CGIObject => $cgi );
MT->set_instance($app);
# Cheap hack to get the output
my($header_sent, $body);
local *MT::App::send_http_header = sub { $header_sent++ };
local *MT::App::print = sub { my $self = shift; $body .= "@_" if $header_sent };
$app->init_request(CGIObject => $cgi);
$app->{cookies} = do { $cgi->cookie; $cgi->{'.cookies'} }; # wtf
$app->run;
# copied from MT::App::send_http_header
my $type = $app->{response_content_type} || 'text/html';
if ( my $charset = $app->charset ) {
$type .= "; charset=$charset"
if ( $type =~ m!^text/! || $type =~ m!\+xml$! )
&& $type !~ /\bcharset\b/;
}
if ($app->{redirect}) {
$app->{cgi_headers}{-status} = 302;
$app->{cgi_headers}{-location} = $app->{redirect};
} else {
$app->{cgi_headers}{-status}
= ( $app->response_code || 200 )
. ( $app->{response_message} ? ' ' . $app->{response_message} : '' );
}
$app->{cgi_headers}{-type} = $type;
my($status, $headers) = $app->{query}->psgi_header( %{ $app->{cgi_headers} } );
return [ $status, $headers, [ $body ] ];
};
};
builder {
mount "/mt-static", builder {
Plack::App::File->new({ root => "mt-static" });
};
mount "/mt-upgrade.cgi", $mt_app->("MT::App::Upgrader");
mount "/mt-add-notify.cgi", $mt_app->("MT::App::NotifyList");
mount "/mt-check.cgi", $mt_app->("MT::App::Wizard");
mount "/mt-comments.cgi", $mt_app->("MT::App::Comments");
mount "/mt-feed.cgi", $mt_app->("MT::App::ActivityFeeds");
mount "/mt-ftsearch.cgi", $mt_app->("MT::App::Search");
mount "/mt-search.cgi", $mt_app->("MT::App::Search");
mount "/mt-tb.cgi", $mt_app->("MT::App::Trackback");
mount "/mt-upgrade.cgi", $mt_app->("MT::App::Upgrader");
mount "/mt-wizard.cgi", $mt_app->("MT::App::Wizard");
mount "/mt.cgi", $mt_app->("MT::App::CMS");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment