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