forked from metacpan/metacpan-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.psgi
131 lines (110 loc) · 3.76 KB
/
app.psgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package MetaCPAN::Web::App; ## no critic (RequireFilenameMatchesPackage)
# ABSTRACT: Modern front-end for MetaCPAN
use strict;
use warnings;
# TODO: When we know everything will work reliably: $ENV{PLACK_ENV} ||= 'development';
#
use File::Basename;
use Config::ZOMG ();
use Log::Log4perl;
use File::Spec;
use File::Path ();
use Plack::Builder;
use Digest::SHA;
my $root_dir;
my $dev_mode;
my $config;
BEGIN {
$root_dir = File::Basename::dirname(__FILE__);
$dev_mode = $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development';
$config = Config::ZOMG->open(
name => 'MetaCPAN::Web',
path => $root_dir,
);
if ($dev_mode) {
$ENV{PLACK_SERVER} = 'Standalone';
$ENV{METACPAN_WEB_DEBUG} = 1;
}
my $log4perl_config
= File::Spec->rel2abs( $config->{log4perl_file} || 'log4perl.conf',
$root_dir );
Log::Log4perl::init($log4perl_config);
# use a unique package and tell l4p to ignore it when finding the warning location.
package MetaCPAN::Web::WarnHandler;
Log::Log4perl->wrapper_register(__PACKAGE__);
my $logger = Log::Log4perl->get_logger;
$SIG{__WARN__} = sub { $logger->warn(@_) };
$dev_mode and require Devel::Confess and Devel::Confess->import;
}
use lib "$root_dir/lib";
use MetaCPAN::Web;
my $tempdir = "$root_dir/var/tmp";
STDERR->autoflush;
# explicitly call ->to_app on every Plack::App::* for performance
builder {
enable sub {
my $app = shift;
sub {
my ($env) = @_;
if ( $env->{HTTP_FASTLY_SSL} ) {
$env->{HTTPS} = 'ON';
$env->{'psgi.url_scheme'} = 'https';
}
if ( my $host = $env->{HTTP_X_FORWARDED_HOST} ) {
$env->{HTTP_HOST} = $host;
}
if ( my $port = $env->{HTTP_X_FORWARDED_PORT} ) {
$env->{SERVER_PORT} = $port;
}
if ( my $addrs = $env->{HTTP_X_FORWARDED_FOR} ) {
my @addrs = map s/^\s+//r =~ s/\s+$//r, split /,/, $addrs;
$env->{REMOTE_ADDR} = $addrs[0];
}
$app->($env);
};
};
enable sub {
my $app = shift;
sub {
my ($env) = @_;
my $request_id = Digest::SHA::sha1_hex( join( "\0",
$env->{REMOTE_ADDR}, $env->{REQUEST_URI}, time, $$, rand, ) );
$env->{'MetaCPAN::Web.request_id'} = $request_id;
Log::Log4perl::MDC->remove;
Log::Log4perl::MDC->put( "request_id", $request_id );
Log::Log4perl::MDC->put( "ip", $env->{REMOTE_ADDR} );
Log::Log4perl::MDC->put( "method", $env->{REMOTE_METHOD} );
Log::Log4perl::MDC->put( "url", $env->{REQUEST_URI} );
Log::Log4perl::MDC->put( "referer", $env->{HTTP_REFERER} );
$app->($env);
};
};
enable 'Runtime';
unless ( $ENV{HARNESS_ACTIVE} or $0 =~ /\.t$/ ) {
my $scoreboard = "$tempdir/scoreboard";
File::Path::make_path($scoreboard);
enable 'ServerStatus::Lite' => (
path => '/server-status',
allow => ['127.0.0.1'],
scoreboard => $scoreboard,
);
}
enable '+MetaCPAN::Middleware::Static' => (
root => $root_dir,
dev_mode => $dev_mode,
temp_dir => $tempdir,
);
builder {
die 'cookie_secret not configured'
unless $config->{cookie_secret};
# Add session cookie here only
enable 'Session::Cookie::MetaCPAN' => (
session_key => 'metacpan_secure',
expires => 2**30,
secure => ( !$dev_mode ),
httponly => 1,
secret => $config->{cookie_secret},
);
MetaCPAN::Web->psgi_app;
};
};