Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow for dynamic multi-instance servicing

By handing off a hash of hostname-to-KOHA_CONF mappings to the
Koha::Plack::Localize middleware via the host_map parameter,
a single running Plack instance can be used to service multiple
installations for distinct libraries. The absence of this
parameter causes the traditional $ENV{KOHA_CONF} to be used.

It may be useful in the future to also add a "host_mapper"
parameter which could be handed a coderef that would return
the appropriate config file based on any number of factors.
  • Loading branch information...
commit 1a470606ff74848146d560d00b0aa2a2e19cb35c 1 parent 6e4c71d
@ctfliblime ctfliblime authored
Showing with 30 additions and 9 deletions.
  1. +16 −4 Koha/Plack/Localize.pm
  2. +12 −0 Koha/Plack/Util.pm
  3. +2 −5 app.psgi
View
20 Koha/Plack/Localize.pm
@@ -2,16 +2,28 @@ package Koha::Plack::Localize;
use parent qw(Plack::Middleware);
use Koha;
-use C4::Context;
+use Plack::Util::Accessor qw(host_map);
+use Koha::Plack::Util;
sub call {
my ($self, $env) = @_;
+ my $config;
+ if (my $configs = $self->host_map) {
+ my $hostname = Koha::Plack::Util::GetCanonicalHostname($env);
+ $config = $configs->{$hostname};
+ }
+ else {
+ $config = $ENV{KOHA_CONF};
+ }
+
+ local %ENV;
+ require C4::Context;
local $C4::Context::context;
- $C4::Context::context = C4::Context->new();
- my $res = $self->app->($env);
- return $res;
+ $C4::Context::context = C4::Context->new($config);
+
+ $self->app->($env);
}
1;
View
12 Koha/Plack/Util.pm
@@ -0,0 +1,12 @@
+package Koha::Plack::Util;
+
+use Koha;
+
+sub GetCanonicalHostname {
+ my $env = shift;
+ my ($hostname) = split(/, /, $env->{HTTP_X_FORWARDED_HOST}//$env->{HTTP_HOST});
+ $hostname //= $env->{SERVER_NAME};
+ return $hostname;
+}
+
+1;
View
7 app.psgi
@@ -3,15 +3,12 @@
use Koha;
use Plack::App::CGIBin;
use Plack::Builder;
+use Koha::Plack::Util;
my $app = Plack::App::CGIBin->new(root => $ENV{PERL5LIB})->to_app;
sub is_staff {
- my $env = shift;
-
- my ($hostname) = split(/, /, $env->{HTTP_X_FORWARDED_HOST}//$env->{HTTP_HOST});
- $hostname //= $env->{SERVER_NAME};
-
+ my $hostname = Koha::Plack::Util::GetCanonicalHostname(shift);
return $hostname =~ /-staff\./;
}
Please sign in to comment.
Something went wrong with that request. Please try again.