miyagawa (owner)

Revisions

gist: 224677 Download_button fork
public
Public Clone URL: git://gist.github.com/224677.git
Embed All Files: show embed
Text #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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.cgi", $mt_app->("MT::App::CMS");
    mount "/mt-upgrade.cgi", $mt_app->("MT::App::Upgrader");
    # ...
};