]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-httpd
lei: FD-passing and IPC basics
[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
17 my %httpds;
18 my $app;
19 my $refresh = sub {
20         if (@ARGV) {
21                 eval { $app = Plack::Util::load_psgi(@ARGV) };
22                 if ($@) {
23                         die $@,
24 "$0 runs in /, command-line paths must be absolute\n";
25                 }
26         } else {
27                 require PublicInbox::WWW;
28                 my $www = PublicInbox::WWW->new;
29                 $www->preload;
30                 $app = builder {
31                         eval { enable 'ReverseProxy' };
32                         $@ and warn
33 "Plack::Middleware::ReverseProxy missing,\n",
34 "URL generation for redirects may be wrong if behind a reverse proxy\n";
35
36                         enable 'Head';
37                         sub { $www->call(@_) };
38                 };
39         }
40 };
41
42 PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
43         sub ($$$) { # post_accept
44                 my ($client, $addr, $srv) = @_;
45                 my $fd = fileno($srv);
46                 my $h = $httpds{$fd} ||= PublicInbox::HTTPD->new($srv, $app);
47                 PublicInbox::HTTP->new($client, $addr, $h),
48         });