From ec499044458427bfcffa4983d9f51c7143b9a9b5 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 4 Aug 2016 11:47:16 -0300
Subject: [PATCH] Bug 17030: Make REST api available on packages with plack
 enabled

This patch is the starting point for making the REST api available
on Plack.

What it does:
- It creates the /api/v1/app.pl mount point in plack.psgi
- It enables the ProxyPass and ProxyPassReverse directives so it
  is reached through Plack.
- It sets rewrite rules so we can use the 'pretty' urls (i.e.
  /api/v1/patrons instead of /api/v1/app.pl/api/v1/patrons).

To test:
- Grab the following files, and put them in /etc/koha (overwrite the existing ones)
  debian/templates/apache-shared-intranet-plack.conf
  debian/templates/apache-shared-opac-plack.conf
- Tweak your /etc/koha/sites/kohadev/plack.psgi file so the API-related stuff
  is present on your file.
- Make sure Plack is enabled for the instance:
  $ sudo koha-plack --enable kohadev
  $ sudo koha-plack --restart kohadev
  $ sudo service apache2 restart
- Follow the previous patch test plan, but use this URLs (no pretty URL):

  http://localhost:8080/api/v1/app.pl/api/v1/patrons/50
  http://localhost:8081/api/v1/app.pl/api/v1/patrons/50
=> SUCCESS: You get a JSON response from the API [1]
- Not use this URLs:
  http://localhost:8080/api/v1/patrons/50
  http://localhost:8081/api/v1/patrons/50
=> SUCCESS: You get a JSON response from the API [1]
- Sign off :-D

[1] this patch made a bug visible (the session is lost when accessing the API through
Plack) but it shouldn't prevent its inclusion because the API right now is not even available
as default for developers to test or fix it.

Signed-off-by: Benjamin Rokseth <benjamin.rokseth@kul.oslo.kommune.no>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 debian/templates/apache-shared-intranet-plack.conf |  7 +++++--
 debian/templates/apache-shared-opac-plack.conf     |  7 +++++--
 debian/templates/plack.psgi                        | 16 ++++++++++------
 3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/debian/templates/apache-shared-intranet-plack.conf b/debian/templates/apache-shared-intranet-plack.conf
index 91e47f6..f77f640 100644
--- a/debian/templates/apache-shared-intranet-plack.conf
+++ b/debian/templates/apache-shared-intranet-plack.conf
@@ -28,8 +28,11 @@
         ProxyPassReverse /cgi-bin/koha "unix:/var/run/koha/${instance}/plack.sock|http://localhost/intranet"
 
         # Point the /api endpoint to Plack
-       # ProxyPass /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
-       # ProxyPassReverse /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
+        RewriteCond %{REQUEST_URI} !^/api/v[0-1]+/app.pl
+        RewriteRule ^/api/(v[0-9]+)/(.*)$ /api/$1/app.pl/api/$1/$2 [L,PT]
+
+        ProxyPass /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
+        ProxyPassReverse /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
 
     </IfModule>
 </IfVersion>
diff --git a/debian/templates/apache-shared-opac-plack.conf b/debian/templates/apache-shared-opac-plack.conf
index 95787d8..f68e3d2 100644
--- a/debian/templates/apache-shared-opac-plack.conf
+++ b/debian/templates/apache-shared-opac-plack.conf
@@ -18,8 +18,11 @@
         ProxyPassReverse /cgi-bin/koha "unix:/var/run/koha/${instance}/plack.sock|http://localhost/opac"
 
         # Point the /api endpoint to Plack
-       # ProxyPass /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
-       # ProxyPassReverse /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
+        RewriteCond %{REQUEST_URI} !^/api/v[0-1]+/app.pl
+        RewriteRule ^/api/(v[0-9]+)/(.*)$ /api/$1/app.pl/api/$1/$2 [L,PT]
+
+        ProxyPass /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
+        ProxyPassReverse /api "unix:/var/run/koha/${instance}/plack.sock|http://localhost/api"
 
     </IfModule>
 </IfVersion>
diff --git a/debian/templates/plack.psgi b/debian/templates/plack.psgi
index 704b91b..397998f 100644
--- a/debian/templates/plack.psgi
+++ b/debian/templates/plack.psgi
@@ -25,6 +25,8 @@ use Plack::App::CGIBin;
 use Plack::App::Directory;
 use Plack::App::URLMap;
 
+use Mojo::Server::PSGI;
+
 # Pre-load libraries
 use C4::Boolean;
 use C4::Branch;
@@ -60,16 +62,18 @@ my $opac = Plack::App::CGIBin->new(
     root => '/usr/share/koha/opac/cgi-bin/opac'
 )->to_app;
 
-# my $api  = Plack::App::CGIBin->new(
-#     root => '/usr/share/koha/api/'
-# )->to_app;
+my $apiv1  = builder {
+    my $server = Mojo::Server::PSGI->new;
+    $server->load_app('/usr/share/koha/api/v1/app.pl');
+    $server->to_psgi_app;
+};
 
 builder {
 
     enable "ReverseProxy";
     enable "Plack::Middleware::Static";
 
-    mount '/opac'     => $opac;
-    mount '/intranet' => $intranet;
-    # mount '/api'       => $api;
+    mount '/opac'          => $opac;
+    mount '/intranet'      => $intranet;
+    mount '/api/v1/app.pl' => $apiv1;
 };
-- 
2.8.1