Bug 18110 - DBRev 16.12.00.028
[koha.git] / misc / plack / koha.psgi
bloba66b2ec3608f8a933589bec8cb23c4c9eddcdc64
1 #!/usr/bin/perl
2 use Plack::Builder;
3 use Plack::App::CGIBin;
4 use lib qw( ./lib );
5 use Plack::Middleware::Debug;
6 use Plack::App::Directory;
8 use CGI qw(-utf8 ); # we will lose -utf8 under plack
10 no warnings 'redefine';
11 my $old_new = \&CGI::new;
12 *CGI::new = sub {
13 my $q = $old_new->( @_ );
14 $CGI::PARAM_UTF8 = 1;
15 Koha::Caches->flush_L1_caches();
16 Koha::Cache::Memory::Lite->flush();
17 return $q;
21 BEGIN {
23 # override configuration from startup script below:
24 # (requires --reload option)
26 $ENV{PLACK_DEBUG} = 1; # toggle debugging
28 # memcache change requires restart
29 $ENV{MEMCACHED_SERVERS} = "localhost:11211";
30 #$ENV{MEMCACHED_DEBUG} = 0;
32 $ENV{PROFILE_PER_PAGE} = 1; # reset persistent and profile counters after each page, like CGI
33 #$ENV{INTRANET} = 1; # usually passed from script
35 #$ENV{DBI_AUTOPROXY}='dbi:Gofer:transport=null;cache=DBI::Util::CacheMemory'
37 } # BEGIN
39 use C4::Context;
40 use C4::Languages;
41 use C4::Members;
42 use C4::Boolean;
43 use C4::Letters;
44 use C4::Koha;
45 use C4::XSLT;
46 use Koha::DateUtils;
47 use Koha::Caches;
48 use Koha::Cache::Memory::Lite;
49 use Koha::Patron::Categories;
51 =for preload
52 use C4::Tags; # FIXME
53 =cut
55 use Devel::Size 0.77; # 0.71 doesn't work for Koha
56 my $watch_capture_regex = '(C4|Koha)';
58 sub watch_for_size {
59 my @watch =
60 map { s/^.*$watch_capture_regex/$1/; s/\//::/g; s/\.pm$//; $_ } # fix paths
61 grep { /$watch_capture_regex/ }
62 keys %INC
64 warn "# watch_for_size ",join(' ',@watch);
65 return @watch;
68 my $CGI_ROOT = $ENV{INTRANET} ? $ENV{INTRANETDIR} : $ENV{OPACDIR};
69 warn "# using Koha ", $ENV{INTRANET} ? 'intranet' : 'OPAC', " CGI from $CGI_ROOT\n";
70 my $app=Plack::App::CGIBin->new(root => $CGI_ROOT)->to_app;
71 my $home = sub {
72 return [ 302, [ Location => '/cgi-bin/koha/' . ( $ENV{INTRANET} ? 'mainpage.pl' : 'opac-main.pl' ) ] ];
75 builder {
77 # please don't use plugins which are under enable_if $ENV{PLACK_DEBUG} in production!
78 # they are known to leek memory
79 enable_if { $ENV{PLACK_DEBUG} } 'Debug', panels => [
80 qw(Environment Response Timer Memory),
81 # optional plugins (uncomment to enable) are sorted according to performance implact
82 # [ 'Devel::Size', for => \&watch_for_size ], # https://github.com/dpavlin/p5-plack-devel-debug-devel-size
83 # [ 'DBIProfile', profile => 2 ],
84 # [ 'DBITrace', level => 1 ], # a LOT of fine-graded SQL trace
85 # [ 'Profiler::NYTProf', exclude => [qw(.*\.css .*\.png .*\.ico .*\.js .*\.gif)] ],
88 # don't enable this plugin in production, since stack traces reveal too much information
89 # about system to potential attackers!
90 enable_if { $ENV{PLACK_DEBUG} } 'StackTrace';
92 # this enables plackup or starman to serve static files and provide working plack
93 # setup without need for front-end web server to serve static files
94 enable_if { $ENV{INTRANETDIR} } "Plack::Middleware::Static",
95 path => qr{^/(intranet|opac)-tmpl/},
96 root => "$ENV{INTRANETDIR}/koha-tmpl/";
98 # + is required so Plack doesn't try to prefix Plack::Middleware::
99 enable "+Koha::Middleware::SetEnv";
101 mount "/cgi-bin/koha" => $app;
102 mount "/" => $home;