Bug 20582: Fix a cache issue in Koha::App::{Opac,Intranet}
[koha.git] / Koha / App / Opac.pm
blob89ff94c5ec7f73bb1c524eb3bfc4c6992a31768e
1 package Koha::App::Opac;
3 # Copyright 2020 BibLibre
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use Mojo::Base 'Mojolicious';
24 use Koha::Caches;
25 use Koha::Cache::Memory::Lite;
27 sub startup {
28 my ($self) = @_;
30 push @{$self->plugins->namespaces}, 'Koha::App::Plugin';
31 push @{$self->static->paths}, $self->home->rel_file('koha-tmpl');
33 # Create route for all CGI scripts, need to be loaded first because of
34 # CGI::Compile
35 $self->plugin('CGIBinKoha', opac => 1);
37 # Create routes for API
38 # FIXME This generates routes like this: /api/api/v1/...
39 $self->plugin('RESTV1');
41 $self->hook(before_dispatch => \&_before_dispatch);
42 $self->hook(around_action => \&_around_action);
44 my $r = $self->routes;
46 $r->any('/')->to(cb => sub { shift->redirect_to('/cgi-bin/koha/opac-main.pl') });
49 sub _before_dispatch {
50 my $c = shift;
52 my $path = $c->req->url->path->to_string;
54 # Remove Koha version from URL
55 $path =~ s/_\d{2}\.\d{7}\.(js|css)/.$1/;
57 # See FIXME above
58 if ($path =~ m|^/api/v|) {
59 $path = '/api' . $path;
62 $c->req->url->path->parse($path);
65 sub _around_action {
66 my ($next, $c, $action, $last) = @_;
68 # Flush memory caches before every request
69 Koha::Caches->flush_L1_caches();
70 Koha::Cache::Memory::Lite->flush();
72 return $next->();