-use IO::Handle;
-require PublicInbox::WWW;
-use CGI qw/-nosticky/;
-our $NO_SCRIPT_NAME;
-our %HTTP_CODES;
-BEGIN {
- $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
- CGI->compile if $ENV{MOD_PERL};
-
- %HTTP_CODES = (
- 200 => 'OK',
- 301 => 'Moved Permanently',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- 501 => 'Not Implemented',
- );
-}
-
-# some servers (Ruby webrick) include scheme://host[:port] here,
-# which confuses CGI.pm when generating self_url.
-# RFC 3875 does not mention REQUEST_URI at all,
-# so nuke it since CGI.pm functions without it.
-delete $ENV{REQUEST_URI};
-$ENV{SCRIPT_NAME} = '' if $NO_SCRIPT_NAME;
-my $req = CGI->new;
-my $ret = PublicInbox::WWW::run($req, $req->request_method);
-
-my $out = select;
-$out->binmode;
-if (@ARGV && $ARGV[0] eq 'static') {
- $out->write($ret->[2]->[0]); # only show the body
-} else { # CGI
- if (ref($ret) eq 'CODE') {
- $ret->(*dump_header);
- } else {
- my ($status, $headers, $body) = @$ret;
-
- dump_header([$status, $headers])->write($body->[0]);
+use Plack::Loader;
+use Plack::Builder;
+use Plack::Request;
+use Plack::Handler::CGI;
+use PublicInbox::WWW;
+BEGIN { PublicInbox::WWW->preload if $ENV{MOD_PERL} }
+my $www = PublicInbox::WWW->new;
+my $have_deflater = eval { require Plack::Middleware::Deflater; 1 };
+my $app = builder {
+ if ($have_deflater) {
+ enable 'Deflater',
+ content_type => [ 'text/html', 'text/plain',
+ 'application/atom+xml' ];