]> Sergey Matveev's repositories - public-inbox.git/blob - examples/newswww.psgi
Merge tag 'v1.6.1' into eidx
[public-inbox.git] / examples / newswww.psgi
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
4 #
5 # NewsWWW may be used independently of WWW.  This can be useful
6 # for mapping HTTP/HTTPS requests to the hostname of an NNTP server
7 # to redirect users to the proper HTTP/HTTPS endpoint for a given
8 # inbox.  NewsWWW exists because people (or software) can mishandle
9 # "nntp://" or "news://" URLs as "http://" (or "https://")
10 #
11 # Usage (development, with auto-reload):
12 #       plackup -I lib -o 127.0.0.1 -R lib -r examples/newswww.psgi
13 #
14 # Usage (production, with public-inbox-httpd(1)):
15 #       public-inbox-httpd [OPTIONS] /path/to/examples/newsww.psgi
16 use strict;
17 use warnings;
18 use Plack::Builder;
19 use PublicInbox::WWW;
20 use PublicInbox::NewsWWW;
21
22 my $newswww = PublicInbox::NewsWWW->new;
23
24 # Optional, (you may drop the "mount '/'" section below)
25 my $www = PublicInbox::WWW->new;
26 $www->preload;
27
28 builder {
29         # HTTP/1.1 requests to "Host: news.example.com" will hit this:
30         mount 'http://news.example.com/' => builder {
31                 enable 'Head';
32                 sub { $newswww->call($_[0]) };
33         };
34
35         # rest of requests will hit this (optional) part for the
36         # regular PublicInbox::WWW code:
37         # see comments in examples/public-inbox.psgi for more info:
38         mount '/' => builder {
39                 eval { enable 'ReverseProxy' };
40                 enable 'Head';
41                 sub { $www->call($_[0]) }
42         };
43 }