[Bps-public-commit] rtx-rest branch, dev, updated. 4efa81110795892834cbf696de615d4abd93fc2d

Wallace Reis wreis at bestpractical.com
Thu Dec 18 14:16:56 EST 2014


The branch, dev has been updated
       via  4efa81110795892834cbf696de615d4abd93fc2d (commit)
       via  ef809a4225faf90f64245e84cf57acdfba62bbf4 (commit)
      from  2f44aed252cf0df9450f8c5e3ce6e2235d58d229 (commit)

Summary of changes:
 Makefile.PL                               |  3 +++
 lib/RT/Extension/REST2.pm                 | 45 +++++++++----------------------
 lib/RT/Extension/REST2/Dispatcher.pm      | 24 +++++++++++++++++
 lib/RT/Extension/REST2/Resource/Record.pm |  2 +-
 4 files changed, 41 insertions(+), 33 deletions(-)
 create mode 100644 lib/RT/Extension/REST2/Dispatcher.pm

- Log -----------------------------------------------------------------
commit ef809a4225faf90f64245e84cf57acdfba62bbf4
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Tue Dec 16 15:04:59 2014 -0200

    More docs update

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index 0647832..9edf2f5 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -33,7 +33,9 @@ May need root permissions
 
 Add this line:
 
-    Plugin('RT::Extension::REST2');
+    Set(@Plugins, qw(RT::Extension::REST2));
+
+or add C<RT::Extension::REST2> to your existing C<@Plugins> line.
 
 =item Clear your mason cache
 

commit 4efa81110795892834cbf696de615d4abd93fc2d
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Wed Dec 17 16:41:05 2014 -0200

    Use Web::Simple as dispatcher

diff --git a/Makefile.PL b/Makefile.PL
index 662a183..ecad65f 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -18,7 +18,10 @@ requires 'Plack::Builder';
 requires 'Scalar::Util';
 requires 'Sub::Exporter';
 requires 'Web::Machine' => '0.12';
+requires 'Web::Simple';
 requires 'Class::Method::Modifiers';
+requires 'Plack::Middleware::RequestHeaders';
+requires 'Plack::Middleware::ReverseProxyPath';
 
 sign;
 WriteAll;
diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index 9edf2f5..76799c7 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -9,7 +9,7 @@ our $DEFAULT_REST_PATH = '/REST/2.0';
 
 use UNIVERSAL::require;
 use Plack::Builder;
-use Web::Machine;
+use RT::Extension::REST2::Dispatcher;
 
 =encoding utf-8
 
@@ -204,24 +204,6 @@ handle them appropriately.
 
 # XXX TODO: API doc
 
-sub resources {
-    return qw(
-        Queue
-        Queues
-        Ticket
-        Tickets
-        User
-        Users
-        Download::CF
-    );
-}
-
-sub resource {
-    Web::Machine->new(
-        resource => "RT::Extension::REST2::Resource::$_[0]",
-    )->to_app;
-}
-
 sub to_psgi_app { shift->to_app(@_) }
 
 sub to_app {
@@ -229,21 +211,18 @@ sub to_app {
 
     RT::ConnectToDatabase();
 
+    my $rest_path = $class->rest_path;
+
     return builder {
         enable '+RT::Extension::REST2::Middleware::Log';
         enable '+RT::Extension::REST2::Middleware::Auth';
-
-        sub {
-            my ($env) = @_;
-            my $dispatch = builder {
-                for ($class->resources) {
-                    (my $path = lc $_) =~ s{::}{/}g;
-                    mount "/$path" => resource($_);
-                }
-                mount "/"       => sub { [ 404, ['Content-type' => 'text/plain'], ['Unknown resource'] ] };
-            };
-            $dispatch->(@_);
-        }
+        enable 'RequestHeaders',
+            set => [
+                'X-Forwarded-Script-Name' => '/',
+                'X-Traversal-Path' => $rest_path,
+            ];
+        enable 'ReverseProxyPath';
+        RT::Extension::REST2::Dispatcher->to_psgi_app;
     };
 }
 
diff --git a/lib/RT/Extension/REST2/Dispatcher.pm b/lib/RT/Extension/REST2/Dispatcher.pm
new file mode 100644
index 0000000..5bbfd89
--- /dev/null
+++ b/lib/RT/Extension/REST2/Dispatcher.pm
@@ -0,0 +1,24 @@
+package RT::Extension::REST2::Dispatcher;
+
+use strict;
+use warnings;
+use Web::Simple;
+use Web::Machine;
+
+sub dispatch_request {
+    my ($self) = @_;
+    sub (/*/*) {
+        my $resource_name = ucfirst lc $_[1];
+        my $resource = "RT::Extension::REST2::Resource::${resource_name}";
+        if ( $resource->require ) {
+            Web::Machine->new(
+                resource => $resource,
+            )->to_app;
+        }
+        else {
+            return undef;
+        }
+    },
+}
+
+1;
diff --git a/lib/RT/Extension/REST2/Resource/Record.pm b/lib/RT/Extension/REST2/Resource/Record.pm
index 607be90..c1380bf 100644
--- a/lib/RT/Extension/REST2/Resource/Record.pm
+++ b/lib/RT/Extension/REST2/Resource/Record.pm
@@ -36,7 +36,7 @@ sub _build_record_class {
 sub _build_record {
     my $self = shift;
     my $record = $self->record_class->new( $self->current_user );
-    my $id = bind_path('/:id', $self->request->path_info);
+    my ($type, $id) = bind_path('/:type/:id', $self->request->path_info);
     $record->Load($id) if $id;
     return $record;
 }

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list