Bug 11998: Do not clear syspref cache in psgi file
[koha.git] / misc / plack / koha.psgi
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;
7
8 use CGI qw(-utf8 ); # we will loose -utf8 under plack
9 {
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         return $q;
16     };
17 }
18
19 BEGIN {
20
21 # override configuration from startup script below:
22 # (requires --reload option)
23
24 $ENV{PLACK_DEBUG} = 1; # toggle debugging
25
26 # memcache change requires restart
27 $ENV{MEMCACHED_SERVERS} = "localhost:11211";
28 #$ENV{MEMCACHED_DEBUG} = 0;
29
30 $ENV{PROFILE_PER_PAGE} = 1; # reset persistent and profile counters after each page, like CGI
31 #$ENV{INTRANET} = 1; # usually passed from script
32
33 #$ENV{DBI_AUTOPROXY}='dbi:Gofer:transport=null;cache=DBI::Util::CacheMemory'
34
35 } # BEGIN
36
37 use C4::Context;
38 use C4::Languages;
39 use C4::Members;
40 use C4::Boolean;
41 use C4::Letters;
42 use C4::Koha;
43 use C4::XSLT;
44 use C4::Branch;
45 use C4::Category;
46 use Koha::DateUtils;
47 =for preload
48 use C4::Tags; # FIXME
49 =cut
50
51 use Devel::Size 0.77; # 0.71 doesn't work for Koha
52 my $watch_capture_regex = '(C4|Koha)';
53
54 C4::Context->disable_syspref_cache;
55
56 sub watch_for_size {
57         my @watch =
58         map { s/^.*$watch_capture_regex/$1/; s/\//::/g; s/\.pm$//; $_ } # fix paths
59         grep { /$watch_capture_regex/ }
60         keys %INC
61         ;
62         warn "# watch_for_size ",join(' ',@watch);
63         return @watch;
64 };
65
66 my $CGI_ROOT = $ENV{INTRANET} ? $ENV{INTRANETDIR} : $ENV{OPACDIR};
67 warn "# using Koha ", $ENV{INTRANET} ? 'intranet' : 'OPAC', " CGI from $CGI_ROOT\n";
68 my $app=Plack::App::CGIBin->new(root => $CGI_ROOT);
69 my $home = sub {
70         return [ 302, [ Location => '/cgi-bin/koha/' . ( $ENV{INTRANET} ? 'mainpage.pl' : 'opac-main.pl' ) ] ];
71 };
72
73 builder {
74
75         # please don't use plugins which are under enable_if $ENV{PLACK_DEBUG} in production!
76         # they are known to leek memory
77         enable_if { $ENV{PLACK_DEBUG} } 'Debug',  panels => [
78                 qw(Environment Response Timer Memory),
79                 # optional plugins (uncomment to enable) are sorted according to performance implact
80 #               [ 'Devel::Size', for => \&watch_for_size ], # https://github.com/dpavlin/p5-plack-devel-debug-devel-size
81 #               [ 'DBIProfile', profile => 2 ],
82 #               [ 'DBITrace', level => 1 ], # a LOT of fine-graded SQL trace
83 #               [ 'Profiler::NYTProf', exclude => [qw(.*\.css .*\.png .*\.ico .*\.js .*\.gif)] ],
84         ];
85
86         # don't enable this plugin in production, since stack traces reveal too much information
87         # about system to potential attackers!
88         enable_if { $ENV{PLACK_DEBUG} } 'StackTrace';
89
90         # this enables plackup or starman to serve static files and provide working plack
91         # setup without need for front-end web server to serve static files
92         enable_if { $ENV{INTRANETDIR} } "Plack::Middleware::Static",
93                 path => qr{^/(intranet|opac)-tmpl/},
94                 root => "$ENV{INTRANETDIR}/koha-tmpl/";
95
96         mount "/cgi-bin/koha" => $app;
97         mount "/" => $home;
98
99 };