Lines 7-12
use Mojo::Base 'Mojolicious';
Link Here
|
7 |
use Koha::Caches; |
7 |
use Koha::Caches; |
8 |
use Koha::Cache::Memory::Lite; |
8 |
use Koha::Cache::Memory::Lite; |
9 |
|
9 |
|
|
|
10 |
use C4::Context (); # Do not call import |
11 |
|
10 |
sub startup { |
12 |
sub startup { |
11 |
my ($self) = @_; |
13 |
my ($self) = @_; |
12 |
|
14 |
|
Lines 17-27
sub startup {
Link Here
|
17 |
# CGI::Compile |
19 |
# CGI::Compile |
18 |
$self->plugin('CGIBinKoha'); |
20 |
$self->plugin('CGIBinKoha'); |
19 |
|
21 |
|
|
|
22 |
# Plugin RESTV1 loads Koha::REST::V1 which loads C4::Context |
23 |
# This line make C4::Context::import return early to avoid warning about |
24 |
# config file not found |
25 |
# FIXME This is dirty... |
26 |
C4::Context->set_context({}); |
27 |
|
20 |
# Create routes for API |
28 |
# Create routes for API |
21 |
# FIXME This generates routes like this: /api/api/v1/... |
29 |
# FIXME This generates routes like this: /api/api/v1/... |
22 |
$self->plugin('RESTV1'); |
30 |
$self->plugin('RESTV1'); |
23 |
|
31 |
|
24 |
$self->hook(before_dispatch => \&_before_dispatch); |
32 |
$self->hook(before_dispatch => \&_before_dispatch); |
|
|
33 |
$self->hook(around_action => \&_around_action); |
25 |
|
34 |
|
26 |
# Everything after this comment is only needed to use Mojolicious |
35 |
# Everything after this comment is only needed to use Mojolicious |
27 |
# controllers. |
36 |
# controllers. |
Lines 55-63
sub _before_dispatch {
Link Here
|
55 |
|
64 |
|
56 |
$c->req->url->parse($url); |
65 |
$c->req->url->parse($url); |
57 |
|
66 |
|
58 |
# Flush caches before every request |
67 |
} |
59 |
Koha::Caches->flush_L1_caches(); |
68 |
|
|
|
69 |
sub _around_action { |
70 |
my ($next, $c, $action, $last) = @_; |
71 |
|
72 |
# Flush memory caches before every request |
73 |
my $caches = $Koha::Caches::singleton_caches; |
74 |
if ($caches) { |
75 |
foreach my $key (keys %$caches) { |
76 |
my $cache = $caches->{$key}; |
77 |
if (ref $cache->{cache} eq 'Cache::Memory') { |
78 |
$cache->flush_all; |
79 |
} |
80 |
$cache->flush_L1_cache; |
81 |
} |
82 |
} |
83 |
$Koha::Caches::singleton_caches = {}; |
60 |
Koha::Cache::Memory::Lite->flush(); |
84 |
Koha::Cache::Memory::Lite->flush(); |
|
|
85 |
|
86 |
# Set KOHA_CONF |
87 |
my $previous_koha_conf = $ENV{KOHA_CONF}; |
88 |
my $koha_conf = $c->req->headers->header('X-Koha-Conf'); |
89 |
$ENV{KOHA_CONF} = $koha_conf if $koha_conf; |
90 |
unless ($ENV{KOHA_CONF}) { |
91 |
die "KOHA_CONF is not set. Either set the environment variable or use HTTP header X-Koha-Conf"; |
92 |
} |
93 |
|
94 |
# Reload context and schema |
95 |
eval { C4::Context->restore_context; }; |
96 |
C4::Context->set_context(C4::Context->new); |
97 |
|
98 |
eval { Koha::Database->restore_schema }; |
99 |
Koha::Database->set_schema(Koha::Database->schema({new => 1})); |
100 |
|
101 |
my $ret = $next->(); |
102 |
|
103 |
# Just in case the app receive requests with and without X-Koha-Conf |
104 |
# Requests without X-Koha-Conf shouldn't be affected by the latest request |
105 |
# with X-Koha-Conf |
106 |
$ENV{KOHA_CONF} = $previous_koha_conf; |
107 |
|
108 |
return $ret; |
61 |
} |
109 |
} |
62 |
|
110 |
|
63 |
1; |
111 |
1; |