Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add config file for psgi handler that lets you turn of automapping an…

…d manually define mappings. Also install pgsi when installing
  • Loading branch information...
commit 07bdcb94499efed6476e87b191c5fee28a75eca6 1 parent 70a1c90
@claesjac authored
Showing with 23 additions and 2 deletions.
  1. +1 −1  Makefile.PL
  2. +22 −1 eg/json-rpc-simple.psgi
View
2  Makefile.PL
@@ -16,5 +16,5 @@ WriteMakefile(
ABSTRACT_FROM => "lib/JSON/RPC/Simple.pm",
AUTHOR => "Claes Jakobsson <claesjac\@cpan.org>",
LICENSE => "perl",
- EXE_FILES => [qw(bin/jsonrpc-simple)],
+ EXE_FILES => [qw(bin/jsonrpc-simple eg/json-rpc-simple.psgi)],
);
View
23 eg/json-rpc-simple.psgi
@@ -4,12 +4,33 @@ use strict;
use warnings;
use Class::Load qw(try_load_class);
+use Config::Tiny;
+use File::Spec;
use JSON::RPC::Simple;
use Plack::Request;
# start with empty dispatcher as we'll populate this as we go
my $dispatcher = JSON::RPC::Simple->dispatch_to({});
+my $Autoload = 1;
+
+my $conf_path = $ENV{JSON_RPC_SIMPLE_DISPATCHER_CONF} || "etc/json-rpc-simple-psgi.conf";
+
+if (-e $conf_path) {
+ my $conf = Config::Tiny->read($conf_path) or die Config::Tiny->errstr();
+
+ $Autoload = $conf->{_}->{auto_load} if exists $conf->{_}->{auto_load};
+
+ my %mapping = %{$conf->{mappings} || {}};
+ for my $path (keys %mapping) {
+ my $pkg = $mapping{$path};
+ try_load_class($pkg);
+ my $target = $pkg->can("new") ? $pkg->new() : $pkg;
+ print STDERR "[PID $$]: mapping $path to $target\n";
+ $dispatcher->dispatch_to({ $path => $target });
+ }
+}
+
my $app = sub {
my $env = shift;
my $request = Plack::Request->new($env);
@@ -17,7 +38,7 @@ my $app = sub {
my $path = $request->path_info;
$path =~ s{::}{/}g;
- unless ($dispatcher->target($path)) {
+ if ($Autoload && !$dispatcher->target($path)) {
my $pkg = substr($path, 1);
$pkg =~ s{/}{::}g;
Please sign in to comment.
Something went wrong with that request. Please try again.