]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-httpd
www: remove Plack::Request dependency entirely
[public-inbox.git] / script / public-inbox-httpd
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Standalone HTTP server for public-inbox.
6 use strict;
7 use warnings;
8 use Plack::Util;
9 use PublicInbox::Daemon;
10 use PublicInbox::HTTP;
11 use PublicInbox::HTTPD;
12 use Plack::Builder;
13 my %httpds;
14 my $app;
15 my $refresh = sub {
16         if (@ARGV) {
17                 eval { $app = Plack::Util::load_psgi(@ARGV) };
18                 if ($@) {
19                         die $@,
20 "$0 runs in /, command-line paths must be absolute\n";
21                 }
22         } else {
23                 require PublicInbox::WWW;
24                 PublicInbox::WWW->preload;
25                 my $www = PublicInbox::WWW->new;
26                 $app = builder {
27                         eval {
28                                 enable 'Deflater',
29                                         content_type => [ qw(
30                                                 text/html
31                                                 text/plain
32                                                 application/atom+xml
33                                                 )]
34                         };
35                         $@ and warn
36 "Plack::Middleware::Deflater missing, bandwidth will be wasted\n";
37
38                         eval { enable 'ReverseProxy' };
39                         $@ and warn
40 "Plack::Middleware::ReverseProxy missing,\n",
41 "URL generation for redirects may be wrong if behind a reverse proxy\n";
42
43                         enable 'Head';
44                         sub { $www->call(@_) };
45                 };
46         }
47 };
48
49 PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
50         sub ($$$) { # post_accept
51                 my ($client, $addr, $srv) = @_;
52                 my $fd = fileno($srv);
53                 my $h = $httpds{$fd} ||= PublicInbox::HTTPD->new($srv, $app);
54                 PublicInbox::HTTP->new($client, $addr, $h),
55         });