Permalink
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...
1 parent 70a1c90 commit 07bdcb94499efed6476e87b191c5fee28a75eca6 @claesjac committed Dec 22, 2011
Showing with 23 additions and 2 deletions.
  1. +1 −1 Makefile.PL
  2. +22 −1 eg/json-rpc-simple.psgi
View
@@ -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
@@ -4,20 +4,41 @@ 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);
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;

0 comments on commit 07bdcb9

Please sign in to comment.