]> Sergey Matveev's repositories - public-inbox.git/blob - examples/public-inbox.psgi
examples/grok-pull.post_update_hook: move url_base to the top
[public-inbox.git] / examples / public-inbox.psgi
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
4 # Note: this is part of our test suite, update t/plack.t if this changes
5 # Usage: plackup [OPTIONS] /path/to/this/file
6 #
7 # A startup command for development which monitors changes:
8 #       plackup -I lib -o 127.0.0.1 -R lib -r examples/public-inbox.psgi
9 #
10 # .psgi paths may also be passed to public-inbox-httpd(1) for
11 # production deployments:
12 #       public-inbox-httpd [OPTIONS] /path/to/examples/public-inbox.psgi
13 use strict;
14 use warnings;
15 use PublicInbox::WWW;
16 use Plack::Builder;
17 my $www = PublicInbox::WWW->new;
18 $www->preload;
19
20 # share the public-inbox code itself:
21 my $src = $ENV{SRC_GIT_DIR}; # '/path/to/public-inbox.git'
22 $src = PublicInbox::Git->new($src) if defined $src;
23
24 builder {
25         eval {
26                 enable 'Deflater',
27                         content_type => [ qw(
28                                 text/html
29                                 text/plain
30                                 application/atom+xml
31                                 )]
32         };
33
34         # Enable to ensure redirects and Atom feed URLs are generated
35         # properly when running behind a reverse proxy server which
36         # sets the X-Forwarded-Proto request header.
37         # See Plack::Middleware::ReverseProxy documentation for details
38         eval { enable 'ReverseProxy' };
39         $@ and warn
40 "Plack::Middleware::ReverseProxy missing,\n",
41 "URL generation for redirects may be wrong if behind a reverse proxy\n";
42
43         # Optional: Log timing information for requests to track performance.
44         # Logging to STDOUT is recommended since public-inbox-httpd knows
45         # how to reopen it via SIGUSR1 after log rotation.
46         # enable 'AccessLog::Timed',
47         #       logger => sub { syswrite(STDOUT, $_[0]) },
48         #       format => '%t "%r" %>s %b %D';
49
50         enable 'Head';
51         sub {
52                 my ($env) = @_;
53                 # share public-inbox.git code!
54                 if ($src && $env->{PATH_INFO} =~
55                                 m!\A/(?:public-inbox(?:\.git)?/)?
56                                 ($PublicInbox::GitHTTPBackend::ANY)\z!xo) {
57                         PublicInbox::GitHTTPBackend::serve($env, $src, $1);
58                 } else {
59                         $www->call($env);
60                 }
61         };
62 }