Bug 30511: Don't lock up entire database while running koha-dump
[koha.git] / debian / templates / plack.psgi
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 use Modern::Perl;
19
20 use Plack::Builder;
21 use Plack::App::CGIBin;
22 use Plack::App::Directory;
23 use Plack::App::URLMap;
24 use Plack::Request;
25
26 use Mojo::Server::PSGI;
27
28 # Pre-load libraries
29 use C4::Koha;
30 use C4::Languages;
31 use C4::Letters;
32 use C4::Members;
33 use C4::XSLT;
34 use Koha::Caches;
35 use Koha::Cache::Memory::Lite;
36 use Koha::Database;
37 use Koha::DateUtils;
38 use Koha::Logger;
39
40 use Log::Log4perl;
41 use CGI qw(-utf8 ); # we will loose -utf8 under plack, otherwise
42 {
43     no warnings 'redefine';
44     my $old_new = \&CGI::new;
45     *CGI::new = sub {
46         my $q = $old_new->( @_ );
47         $CGI::PARAM_UTF8 = 1;
48         Koha::Caches->flush_L1_caches();
49         Koha::Cache::Memory::Lite->flush();
50         return $q;
51     };
52 }
53
54 my $home = $ENV{KOHA_HOME};
55 my $intranet = Plack::App::CGIBin->new(
56     root => $ENV{DEV_INSTALL}? $home: "$home/intranet/cgi-bin"
57 )->to_app;
58
59 my $opac = Plack::App::CGIBin->new(
60     root => $ENV{DEV_INSTALL}? "$home/opac": "$home/opac/cgi-bin/opac"
61 )->to_app;
62
63 my $apiv1  = builder {
64     my $server = Mojo::Server::PSGI->new;
65     $server->load_app("$home/api/v1/app.pl");
66     $server->to_psgi_app;
67 };
68
69 Koha::Logger->_init;
70
71 builder {
72     enable "ReverseProxy";
73     enable "Plack::Middleware::Static";
74
75     # + is required so Plack doesn't try to prefix Plack::Middleware::
76     enable "+Koha::Middleware::SetEnv";
77     enable "+Koha::Middleware::RealIP";
78
79     mount '/opac'          => builder {
80         #NOTE: it is important that these are relative links
81         enable 'ErrorDocument',
82             400 => 'errors/400.pl',
83             401 => 'errors/401.pl',
84             402 => 'errors/402.pl',
85             403 => 'errors/403.pl',
86             404 => 'errors/404.pl',
87             500 => 'errors/500.pl',
88             subrequest => 1;
89         #NOTE: Without this middleware to catch fatal errors, ErrorDocument won't be able to render a 500 document
90         #NOTE: This middleware must be closer to the PSGI app than ErrorDocument
91         enable "HTTPExceptions";
92         if ( Log::Log4perl->get_logger('plack-opac')->has_appenders ){
93             enable 'Log4perl', category => 'plack-opac';
94             enable 'LogWarn';
95         }
96         $opac;
97     };
98     mount '/intranet'      => builder {
99         #NOTE: it is important that these are relative links
100         enable 'ErrorDocument',
101             400 => 'errors/400.pl',
102             401 => 'errors/401.pl',
103             402 => 'errors/402.pl',
104             403 => 'errors/403.pl',
105             404 => 'errors/404.pl',
106             500 => 'errors/500.pl',
107             subrequest => 1;
108         #NOTE: Without this middleware to catch fatal errors, ErrorDocument won't be able to render a 500 document
109         #NOTE: This middleware must be closer to the PSGI app than ErrorDocument
110         enable "HTTPExceptions";
111         if ( Log::Log4perl->get_logger('plack-intranet')->has_appenders ){
112             enable 'Log4perl', category => 'plack-intranet';
113             enable 'LogWarn';
114         }
115         $intranet;
116     };
117     mount '/api/v1/app.pl' => builder {
118         if ( Log::Log4perl->get_logger('plack-api')->has_appenders ){
119             enable 'Log4perl', category => 'plack-api';
120             enable 'LogWarn';
121         }
122         $apiv1;
123     };
124 };