View | Details | Raw Unified | Return to bug 20582
Collapse All | Expand All

(-)a/Koha/App/Intranet.pm (+85 lines)
Line 0 Link Here
1
package Koha::App::Intranet;
2
3
# Copyright 2020 BibLibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Mojo::Base 'Mojolicious';
23
24
use Koha::Caches;
25
use Koha::Cache::Memory::Lite;
26
27
sub startup {
28
    my ($self) = @_;
29
30
    push @{$self->plugins->namespaces}, 'Koha::App::Plugin';
31
    push @{$self->static->paths}, $self->home->rel_file('koha-tmpl');
32
33
    # Create route for all CGI scripts, need to be loaded first because of
34
    # CGI::Compile
35
    $self->plugin('CGIBinKoha');
36
37
    # Create routes for API
38
    # FIXME This generates routes like this: /api/api/v1/...
39
    $self->plugin('RESTV1');
40
41
    $self->hook(before_dispatch => \&_before_dispatch);
42
    $self->hook(around_action => \&_around_action);
43
44
    my $r = $self->routes;
45
46
    $r->any('/')->to(cb => sub { shift->redirect_to('/cgi-bin/koha/mainpage.pl') });
47
}
48
49
sub _before_dispatch {
50
    my $c = shift;
51
52
    my $path = $c->req->url->path->to_string;
53
54
    # Remove Koha version from URL
55
    $path =~ s/_\d{2}\.\d{7}\.(js|css)/.$1/;
56
57
    # See FIXME above
58
    if ($path =~ m|^/api/v|) {
59
        $path = '/api' . $path;
60
    }
61
62
    $c->req->url->path->parse($path);
63
}
64
65
sub _around_action {
66
    my ($next, $c, $action, $last) = @_;
67
68
    # Flush memory caches before every request
69
    my $caches = $Koha::Caches::singleton_caches;
70
    if ($caches) {
71
        foreach my $key (keys %$caches) {
72
            my $cache = $caches->{$key};
73
            if (ref $cache->{cache} eq 'Cache::Memory') {
74
                $cache->flush_all;
75
            }
76
            $cache->flush_L1_cache;
77
        }
78
    }
79
    $Koha::Caches::singleton_caches = {};
80
    Koha::Cache::Memory::Lite->flush();
81
82
    return $next->();
83
}
84
85
1;
(-)a/Koha/App/Opac.pm (+85 lines)
Line 0 Link Here
1
package Koha::App::Opac;
2
3
# Copyright 2020 BibLibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Mojo::Base 'Mojolicious';
23
24
use Koha::Caches;
25
use Koha::Cache::Memory::Lite;
26
27
sub startup {
28
    my ($self) = @_;
29
30
    push @{$self->plugins->namespaces}, 'Koha::App::Plugin';
31
    push @{$self->static->paths}, $self->home->rel_file('koha-tmpl');
32
33
    # Create route for all CGI scripts, need to be loaded first because of
34
    # CGI::Compile
35
    $self->plugin('CGIBinKoha', opac => 1);
36
37
    # Create routes for API
38
    # FIXME This generates routes like this: /api/api/v1/...
39
    $self->plugin('RESTV1');
40
41
    $self->hook(before_dispatch => \&_before_dispatch);
42
    $self->hook(around_action => \&_around_action);
43
44
    my $r = $self->routes;
45
46
    $r->any('/')->to(cb => sub { shift->redirect_to('/cgi-bin/koha/opac-main.pl') });
47
}
48
49
sub _before_dispatch {
50
    my $c = shift;
51
52
    my $path = $c->req->url->path->to_string;
53
54
    # Remove Koha version from URL
55
    $path =~ s/_\d{2}\.\d{7}\.(js|css)/.$1/;
56
57
    # See FIXME above
58
    if ($path =~ m|^/api/v|) {
59
        $path = '/api' . $path;
60
    }
61
62
    $c->req->url->path->parse($path);
63
}
64
65
sub _around_action {
66
    my ($next, $c, $action, $last) = @_;
67
68
    # Flush memory caches before every request
69
    my $caches = $Koha::Caches::singleton_caches;
70
    if ($caches) {
71
        foreach my $key (keys %$caches) {
72
            my $cache = $caches->{$key};
73
            if (ref $cache->{cache} eq 'Cache::Memory') {
74
                $cache->flush_all;
75
            }
76
            $cache->flush_L1_cache;
77
        }
78
    }
79
    $Koha::Caches::singleton_caches = {};
80
    Koha::Cache::Memory::Lite->flush();
81
82
    return $next->();
83
}
84
85
1;
(-)a/Koha/App/Plugin/CGIBinKoha.pm (+100 lines)
Line 0 Link Here
1
package Koha::App::Plugin::CGIBinKoha;
2
3
# Copyright 2020 BibLibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Mojo::Base 'Mojolicious::Plugin';
23
24
use CGI::Compile;
25
use CGI::Emulate::PSGI;
26
use IO::Scalar;
27
28
sub register {
29
    my ($self, $app, $conf) = @_;
30
31
    my $opac = $conf->{opac};
32
33
    my $r = $app->routes;
34
35
    $r->any('/cgi-bin/koha/*script' => sub {
36
        my ($c) = @_;
37
38
        my $script = $c->stash('script');
39
40
        # Special case for installer which can takes a long time
41
        $c->inactivity_timeout(300) if $script eq 'installer/install.pl';
42
43
        # Remove trailing slash, if any (e.g. .../svc/config/systempreferences/)
44
        $script =~ s|/$||;
45
46
        if ($opac) {
47
            $script = "opac/$script";
48
        }
49
50
        unless (-e $c->app->home->rel_file($script)) {
51
            return $c->reply->not_found;
52
        }
53
54
        my $sub = CGI::Compile->compile($script);
55
        my $app = CGI::Emulate::PSGI->handler($sub);
56
        my $response = $app->($self->_psgi_env($c));
57
58
        $c->res->code($response->[0]);
59
        $c->res->headers->from_hash({ @{ $response->[1] } });
60
        $c->res->body(join('', @{$response->[2]}));
61
        $c->rendered;
62
    })->name('cgi');
63
}
64
65
sub _psgi_env {
66
    my ($self, $c) = @_;
67
68
    my $env = $c->req->env;
69
70
    my $body = $c->req->build_body;
71
    $env = {
72
        %$env,
73
        'psgi.input' => IO::Scalar->new(\$body),
74
        'psgi.errors' => *STDERR,
75
        REQUEST_METHOD => $c->req->method,
76
        QUERY_STRING => $c->req->url->query->to_string,
77
        SERVER_NAME => $c->req->url->to_abs->host,
78
        SERVER_PORT => $c->req->url->to_abs->port,
79
        SERVER_PROTOCOL => 'HTTP/1.1',
80
        CONTENT_LENGTH => $c->req->headers->content_length,
81
        CONTENT_TYPE => $c->req->headers->content_type,
82
        REMOTE_ADDR => $c->tx->remote_address,
83
        SCRIPT_NAME => $c->req->url->path->to_string,
84
    };
85
86
    # Starman sets PATH_INFO to the same value of SCRIPT_NAME, which confuses
87
    # CGI and causes the redirect after OPAC login to fail
88
    delete $env->{PATH_INFO} if ($env->{PATH_INFO} eq $env->{SCRIPT_NAME});
89
90
    for my $name (@{ $c->req->headers->names }) {
91
        my $value = $c->req->headers->header($name);
92
        $name =~ s/-/_/g;
93
        $name = 'HTTP_' . uc($name);
94
        $env->{$name} = $value;
95
    }
96
97
    return $env;
98
}
99
100
1;
(-)a/Koha/App/Plugin/RESTV1.pm (+32 lines)
Line 0 Link Here
1
package Koha::App::Plugin::RESTV1;
2
3
# Copyright 2020 BibLibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Mojo::Base 'Mojolicious::Plugin';
23
24
use Koha::REST::V1;
25
26
sub register {
27
    my ($self, $app) = @_;
28
29
    $app->routes->any('/api')->detour(app => Koha::REST::V1->new);
30
}
31
32
1;
(-)a/app.psgi (+52 lines)
Line 0 Link Here
1
# Copyright 2020 BibLibre
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# 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
# Koha is distributed in the hope that it will be useful, but
11
# 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 Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use FindBin;
21
use Plack::Builder;
22
use Mojo::Server::PSGI;
23
24
sub psgi_app {
25
    my ($script) = @_;
26
27
    my $server = Mojo::Server::PSGI->new;
28
    $server->load_app("$FindBin::Bin/$script");
29
30
    return $server->to_psgi_app;
31
}
32
33
my $opac = psgi_app('bin/opac');
34
my $intranet = psgi_app('bin/intranet');
35
36
my $opac_port = $ENV{KOHA_OPAC_PORT} || 5001;
37
my $intranet_port = $ENV{KOHA_INTRANET_PORT} || 5000;
38
my $port2app = {
39
    $opac_port => $opac,
40
    $intranet_port => $intranet,
41
};
42
43
builder {
44
    enable 'ReverseProxy';
45
    enable '+Koha::Middleware::SetEnv';
46
    enable '+Koha::Middleware::RealIP';
47
    sub {
48
        my $env = shift;
49
        my $app = $port2app->{$env->{SERVER_PORT}} || $intranet;
50
        $app->($env);
51
    };
52
}
(-)a/bin/intranet (+43 lines)
Line 0 Link Here
1
#!/usr/bin/env perl
2
3
# Copyright 2020 BibLibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
# Copyright 2020 BibLibre
20
#
21
# This file is part of Koha.
22
#
23
# Koha is free software; you can redistribute it and/or modify it
24
# under the terms of the GNU General Public License as published by
25
# the Free Software Foundation; either version 3 of the License, or
26
# (at your option) any later version.
27
#
28
# Koha is distributed in the hope that it will be useful, but
29
# WITHOUT ANY WARRANTY; without even the implied warranty of
30
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
# GNU General Public License for more details.
32
#
33
# You should have received a copy of the GNU General Public License
34
# along with Koha; if not, see <http://www.gnu.org/licenses>.
35
36
use Modern::Perl;
37
38
use FindBin;
39
use lib "$FindBin::Bin/..";
40
41
use Mojolicious::Commands;
42
43
Mojolicious::Commands->start_app('Koha::App::Intranet');
(-)a/bin/opac (+27 lines)
Line 0 Link Here
1
#!/usr/bin/env perl
2
3
# Copyright 2020 BibLibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use FindBin;
23
use lib "$FindBin::Bin/..";
24
25
use Mojolicious::Commands;
26
27
Mojolicious::Commands->start_app('Koha::App::Opac');
(-)a/etc/nginx.conf (-1 / +33 lines)
Line 0 Link Here
0
- 
1
# This config file assume you are using starman with the app.psgi that can be
2
# found in the root directory of Koha, and that it listens on ports 5000-5001
3
4
upstream intranet {
5
    server 127.0.0.1:5000;
6
}
7
upstream opac {
8
    server 127.0.0.1:5001;
9
}
10
11
server {
12
    listen 80;
13
    listen [::]:80;
14
15
    server_name intranet.koha-dev; # CHANGEME
16
17
    location / {
18
        include proxy_params;
19
        proxy_pass http://intranet;
20
    }
21
}
22
23
server {
24
    listen 80;
25
    listen [::]:80;
26
27
    server_name opac.koha-dev; # CHANGEME
28
29
    location / {
30
        include proxy_params;
31
        proxy_pass http://opac;
32
    }
33
}

Return to bug 20582