webgui/share/site.psgi
Scott Walters 7a994b59ce File assets should always give IO::File::WithPath objects to PSGI, instead of the current redirecting or streaming behavior. (#11688)
New API method:  WebGUI::Response::sendFile;  it, as appropriate, calls
setRedirect or setStreamedFile, depending on enableStreamingUploads config var.
setStreamedFile now kicks off the XSendfile process.
File.pm now uses this instead of trying to set both a redirect and a stream.
IO::File::WithPath blows up if a file doesn't exist so this raises an exception
now.
The http now no longer insist that '0' is not a valid filename to stream.
site.psgi, depending on enableStreamingUploads, enables either the Static
or XSendfile middleware.
2011-05-12 20:22:43 -04:00

73 lines
2.6 KiB
Perl

use strict;
use Plack::Builder;
use Plack::App::File;
use WebGUI;
builder {
my $wg = WebGUI->new( config => $ENV{WEBGUI_CONFIG} );
my $config = $wg->config;
my $streaming_uploads = $config->get('enableStreamingUploads'); # have to restart for changes to this to take effect
enable 'Log4perl', category => $config->getFilename, conf => WebGUI::Paths->logConfig;
enable 'SimpleContentFilter', filter => sub {
if ( utf8::is_utf8($_) ) {
utf8::encode($_);
}
};
# Reproduce URL handler functionality with middleware
enable '+WebGUI::Middleware::Snoop';
enable 'Status', path => qr{^/uploads/dictionaries}, status => 401;
# For PassThru, use Plack::Builder::mount
# Serve "Extras"
# Plack::Middleware::Static is fallback (you should be using something else to serve static files in production,
# unless you're using the corona Plack server, then it doesn't matter nearly so much)
my ( $extrasURL, $extrasPath ) = ( $config->get('extrasURL'), $config->get('extrasPath') );
enable_if { $streaming_uploads } 'XSendfile';
enable_if { ! $streaming_uploads } 'Static', root => "$extrasPath/", path => sub {s{^\Q$extrasURL/}{}};
# Open/close the WebGUI::Session at the outer-most onion layer
enable '+WebGUI::Middleware::Session', config => $config;
enable '+WebGUI::Middleware::HTTPExceptions';
enable 'ErrorDocument', 503 => $config->get('maintenancePage');
enable_if { ! $_[0]->{'webgui.debug'} } 'ErrorDocument', 500 => $config->get('maintenancePage');
enable '+WebGUI::Middleware::Maintenance';
# enable_if { $_[0]->{'webgui.debug'} } 'StackTrace';
enable_if { $_[0]->{'webgui.debug'} } '+WebGUI::Middleware::StackTrace';
enable_if { $_[0]->{'webgui.debug'} } 'Debug', panels => [
'Timer',
'Memory',
'Session',
'Parameters',
'PerlConfig',
[ 'MySQLTrace', skip_packages => qr/\AWebGUI::SQL(?:\z|::)/ ],
'Response',
'Logger',
];
enable_if { $_[0]->{'webgui.debug'} } '+WebGUI::Middleware::Debug::Environment';
enable_if { $_[0]->{'webgui.debug'} } '+WebGUI::Middleware::Debug::Performance';
# This one uses the Session object, so it comes after WebGUI::Middleware::Session
mount $config->get('uploadsURL') => builder {
enable '+WebGUI::Middleware::WGAccess';
Plack::App::File->new(root => $config->get('uploadsPath'));
};
# enable config defined Middleware
for my $mw ( @{ $config->get('plackMiddleware') || [] } ) {
enable $mw;
}
# Return the app
mount '/' => $wg->to_app;
};