]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-httpd
Merge branch 'unsubscribe'
[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::Request;
13 use Plack::Builder;
14 my %httpds;
15 my $app;
16 my $refresh = sub {
17         if (@ARGV) {
18                 eval { $app = Plack::Util::load_psgi(@ARGV) };
19                 if ($@) {
20                         die $@,
21 "$0 runs in /, command-line paths must be absolute\n";
22                 }
23         } else {
24                 require PublicInbox::WWW;
25                 PublicInbox::WWW->preload;
26                 my $www = PublicInbox::WWW->new;
27                 $app = builder {
28                         eval {
29                                 enable 'Deflater',
30                                         content_type => [ qw(
31                                                 text/html
32                                                 text/plain
33                                                 application/atom+xml
34                                                 )]
35                         };
36                         $@ and warn
37 "Plack::Middleware::Deflater missing, bandwidth will be wasted\n";
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         });