]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-httpd
make Plack optional for non-WWW and non-httpd users
[public-inbox.git] / script / public-inbox-httpd
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016-2020 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 PublicInbox::Daemon;
8 BEGIN {
9         for (qw(Plack::Builder Plack::Util)) {
10                 eval("require $_") or die "E: Plack is required for $0\n";
11         }
12         Plack::Builder->import;
13         require PublicInbox::HTTP;
14         require PublicInbox::HTTPD;
15 }
16 my %httpds;
17 my $app;
18 my $refresh = sub {
19         if (@ARGV) {
20                 eval { $app = Plack::Util::load_psgi(@ARGV) };
21                 if ($@) {
22                         die $@,
23 "$0 runs in /, command-line paths must be absolute\n";
24                 }
25         } else {
26                 require PublicInbox::WWW;
27                 my $www = PublicInbox::WWW->new;
28                 $www->preload;
29                 $app = builder {
30                         eval {
31                                 enable 'Deflater',
32                                         content_type => [ qw(
33                                                 text/html
34                                                 text/plain
35                                                 application/atom+xml
36                                                 )]
37                         };
38
39                         eval { enable 'ReverseProxy' };
40                         $@ and warn
41 "Plack::Middleware::ReverseProxy missing,\n",
42 "URL generation for redirects may be wrong if behind a reverse proxy\n";
43
44                         enable 'Head';
45                         sub { $www->call(@_) };
46                 };
47         }
48 };
49
50 PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
51         sub ($$$) { # post_accept
52                 my ($client, $addr, $srv) = @_;
53                 my $fd = fileno($srv);
54                 my $h = $httpds{$fd} ||= PublicInbox::HTTPD->new($srv, $app);
55                 PublicInbox::HTTP->new($client, $addr, $h),
56         });