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