]> git.uio.no Git - usit-rt.git/blame - lib/RT/PlackRunner.pm
Master to 4.2.8
[usit-rt.git] / lib / RT / PlackRunner.pm
CommitLineData
af59614d
MKG
1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
320f0092 5# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
af59614d
MKG
6# <sales@bestpractical.com>
7#
8# (Except where explicitly superseded by other copyright notices)
9#
10#
11# LICENSE:
12#
13# This work is made available to you under the terms of Version 2 of
14# the GNU General Public License. A copy of that license should have
15# been provided with this software, but in any event can be snarfed
16# from www.gnu.org.
17#
18# This work is distributed in the hope that it will be useful, but
19# WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21# General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26# 02110-1301 or visit their web page on the internet at
27# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28#
29#
30# CONTRIBUTION SUBMISSION POLICY:
31#
32# (The following paragraph is not intended to limit the rights granted
33# to you to modify and distribute this software under the terms of
34# the GNU General Public License and is only of importance to you if
35# you choose to contribute your changes and enhancements to the
36# community by submitting them to Best Practical Solutions, LLC.)
37#
38# By intentionally submitting any modifications, corrections or
39# derivatives to this work, or any other work intended for use with
40# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41# you are the copyright holder for those contributions and you grant
42# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43# royalty-free, perpetual, license to use, copy, create derivative
44# works based on those contributions, and sublicense and distribute
45# those contributions and any derivatives thereof.
46#
47# END BPS TAGGED BLOCK }}}
48
49use warnings;
50use strict;
51
52package RT::PlackRunner;
53
54use base 'Plack::Runner';
55
56sub new {
57 my $class = shift;
58 return $class->SUPER::new( default_middleware => 0, @_ );
59}
60
61sub parse_options {
62 my $self = shift;
63 my @args = @_;
64 # handle "rt-server 8888" for back-compat, but complain about it
65 if (@args && $args[0] =~ m/^\d+$/) {
66 warn "Deprecated: please run $0 --port $ARGV[0] instead\n";
67 unshift @args, '--port';
68 }
69
70 $self->SUPER::parse_options(@args);
71
72 $self->{app} ||= $self->app;
73 $self->{server} ||= $self->loader->guess;
74
75 my %args = @{$self->{options}};
76 if ($self->{server} eq "FCGI") {
77 # We deal with the possible failure modes of this in ->run
78 } elsif ($args{port}) {
79 $self->{explicit_port} = 1;
80 my $old_app = $self->{app};
81 $self->{app} = sub {
82 my $env = shift;
83 $env->{'rt.explicit_port'} = $args{port};
84 $old_app->($env, @_);
85 };
86 } else {
87 $self->set_options(port => (RT->Config->Get('WebPort') || '8080'));
88 }
89}
90
91# Don't assume port 5000 with no port or socket supplied; this allows
92# the WebPort default to kick in (above), and also to provide useful
93# error messages when starting FCGI without any options.
94sub mangle_host_port_socket {
95 my $self = shift;
96 my ($host, $port, $socket, @listen) = @_;
97 return $self->SUPER::mangle_host_port_socket(@_)
98 if @listen or $port or $socket;
c33a4027
MKG
99
100 return host => $host, port => $port, socket => $socket,
101 @listen ? (listen => \@listen) : ();
af59614d
MKG
102}
103
104sub app {
105 require RT::Interface::Web::Handler;
106 my $app = RT::Interface::Web::Handler->PSGIApp;
107
108 if ($ENV{RT_TESTING}) {
109 my $screen_logger = $RT::Logger->remove('screen');
110 require Log::Dispatch::Perl;
111 $RT::Logger->add(
112 Log::Dispatch::Perl->new(
113 name => 'rttest',
114 min_level => $screen_logger->min_level,
115 action => {
116 error => 'warn',
117 critical => 'warn'
118 }
119 )
120 );
121 require Plack::Middleware::Test::StashWarnings;
122 $app = Plack::Middleware::Test::StashWarnings->wrap($app);
123 }
124
125 return $app;
126}
127
128sub run {
129 my $self = shift;
130
131 my %args = @{$self->{options}};
132
133 # Plack::Handler::FCGI has its own catch for this, but doesn't
134 # notice that listen is an empty list, and we can also provide a
135 # better error message.
c33a4027 136 if ($self->{server} eq "FCGI" and not -S STDIN and not @{$args{listen} || []}) {
af59614d
MKG
137 print STDERR "STDIN is not a socket, and no --listen, --socket, or --port provided\n";
138 exit 1;
139 }
140
141 eval { $self->SUPER::run(@_) };
142 my $err = $@;
143 exit 0 unless $err;
144
145 if ( $err =~ /listen/ ) {
146 print STDERR <<EOF;
147WARNING: RT couldn't start up a web server on port $args{port}.
148This is often the case if the port is already in use or you're running @{[$0]}
149as someone other than your system's "root" user. You may also specify a
150temporary port with: $0 --port <port>
151EOF
152
153 if ($self->{explicit_port}) {
154 print STDERR
155 "Please check your system configuration or choose another port\n\n";
156 }
157 exit 1;
158 } else {
159 die
160 "Something went wrong while trying to run RT's standalone web server:\n\t"
161 . $err;
162 }
163}
164
1651;