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