about summary refs log tree commit homepage
path: root/examples/newswww.psgi
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-02-04 11:00:26 +0000
committerEric Wong <e@80x24.org>2019-02-07 21:38:34 +0000
commit285b9b4d7de53b0d607be81fd608d40da82a2fad (patch)
treefc22ee6c0f357ca7d87f51a6b8e6c478c4a7d8a0 /examples/newswww.psgi
parent7eae167d1569bb952cfc06a66afd2dce588bb29b (diff)
downloadpublic-inbox-285b9b4d7de53b0d607be81fd608d40da82a2fad.tar.gz
Plack::Builder allows "mounting" on with hostnames as well as
path names to enable virtual hosting.  This example demonstrates
how port 80/443 for "news.example.com" can redirect browser
requests when somebody attempts to use a "nntp://" URL and
the software assumes "http://"
Diffstat (limited to 'examples/newswww.psgi')
-rw-r--r--examples/newswww.psgi48
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/newswww.psgi b/examples/newswww.psgi
new file mode 100644
index 00000000..0f667822
--- /dev/null
+++ b/examples/newswww.psgi
@@ -0,0 +1,48 @@
+#!/usr/bin/perl -w
+# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
+#
+# NewsWWW may be used independently of WWW.  This can be useful
+# for mapping HTTP/HTTPS requests to the hostname of an NNTP server
+# to redirect users to the proper HTTP/HTTPS endpoint for a given
+# inbox.  NewsWWW exists because people (or software) can mishandle
+# "nntp://" or "news://" URLs as "http://" (or "https://")
+#
+# Usage:
+#        plackup -I lib -o 127.0.0.1 -R lib -r examples/newswww.psgi
+use strict;
+use warnings;
+use Plack::Builder;
+use PublicInbox::WWW;
+use PublicInbox::NewsWWW;
+
+my $newswww = PublicInbox::NewsWWW->new;
+
+# Optional, (you may drop the "mount '/'" section below)
+my $www = PublicInbox::WWW->new;
+$www->preload;
+
+builder {
+        # HTTP/1.1 requests to "Host: news.example.com" will hit this:
+        mount 'http://news.example.com/' => builder {
+                enable 'Head';
+                sub { $newswww->call($_[0]) };
+        };
+
+        # rest of requests will hit this (optional) part for the
+        # regular PublicInbox::WWW code:
+        # see comments in examples/public-inbox.psgi for more info:
+        mount '/' => builder {
+                eval {
+                        enable 'Deflater',
+                                content_type => [ qw(
+                                        text/html
+                                        text/plain
+                                        application/atom+xml
+                                        )]
+                };
+                eval { enable 'ReverseProxy' };
+                enable 'Head';
+                sub { $www->call($_[0]) }
+        };
+}