From 62b6b8552f156ae9b554f6378d7542c905b33016 Mon Sep 17 00:00:00 2001
From: Bastien Abadie <bastien@nextcairn.com>
Date: Mon, 16 Oct 2017 08:35:24 +0200
Subject: [PATCH] Use loris server instead of iipsrv

---
 docker-compose.yml |  35 +++++++++------
 loris/Dockerfile   |  43 ++++++++++++++++++
 loris/loris2.conf  | 108 +++++++++++++++++++++++++++++++++++++++++++++
 nginx/proxy.conf   |  23 +++-------
 4 files changed, 177 insertions(+), 32 deletions(-)
 create mode 100644 loris/Dockerfile
 create mode 100644 loris/loris2.conf

diff --git a/docker-compose.yml b/docker-compose.yml
index 2e5bb64e71..83dbad94c4 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,20 +3,31 @@ version: "3.2"
 services:
 
   # Our own build of IIPsrv
-  iipsrv:
-    build: iipsrv
+  #iipsrv:
+  #  build: iipsrv
 
-    depends_on:
-      - memcached
+  #  depends_on:
+  #    - memcached
+
+  #  volumes:
+  #    - type: volume
+  #      source: ./logs
+  #      target: /var/log
+  #    - type: bind
+  #      source: ./images
+  #      target: /images
+
+  # iipsrv cache
+  #memcached:
+  #  image: memcached:alpine
+
+  loris:
+    build: loris
 
     volumes:
-      - type: volume
-        source: ./logs
-        target: /var/log
       - type: bind
         source: ./images
-        target: /images
-
+        target: /loris/images
 
   # Nginx server as a gateway to iipsrv FCGI
   nginx:
@@ -27,8 +38,4 @@ services:
       - "9000:80"
 
     depends_on:
-      - iipsrv
-
-  # iipsrv cache
-  memcached:
-    image: memcached:alpine
+      - loris
diff --git a/loris/Dockerfile b/loris/Dockerfile
new file mode 100644
index 0000000000..8db0d601e7
--- /dev/null
+++ b/loris/Dockerfile
@@ -0,0 +1,43 @@
+FROM debian
+
+MAINTAINER Bastien Abadie, NextCairn <bastien@nextcairn.com>
+
+# Install dependencies
+RUN apt-get update -y
+RUN apt-get install -y wget tar python python-pip python-setuptools libjpeg62-turbo-dev libfreetype6-dev zlib1g-dev liblcms2-dev liblcms2-utils libtiff5-dev python-dev libwebp-dev
+RUN pip install Pillow gunicorn
+
+# Add loris user and conf
+RUN addgroup loris
+RUN adduser --home=/loris --ingroup=loris --disabled-password loris
+RUN mkdir -p /loris/cache /loris/tmp /loris/www /loris/logs
+VOLUME "/loris/images"
+COPY loris2.conf /etc/loris2.conf
+
+# Download loris source code
+WORKDIR /tmp/loris
+RUN wget https://github.com/loris-imageserver/loris/archive/v2.2.0.tar.gz -O loris.tar.gz
+RUN tar xzf loris.tar.gz --strip-components 1
+
+# Build loris
+RUN pip install -r requirements.txt
+RUN python setup.py install \
+	--image-cache=/loris/cache \
+	--info-cache=/loris/cache \
+	--tmp-dir=/loris/tmp \
+	--www-dir=/loris/www \
+	--log-dir=/loris/logs \
+	--source-images=/loris/images
+RUN ln -s /loris/www/loris2.wsgi /loris/www/loris2.py
+
+# bind test server to 0.0.0.0
+#RUN sed -i -- 's/localhost/0.0.0.0/g' webapp.py
+#RUN sed -i 's/app = create_app(debug=True)/app = create_app(debug=False, config_file_path=conf_fp)/g' webapp.py
+
+# Cleanup
+WORKDIR /loris
+RUN rm -rf /tmp/loris
+
+# Run loris on port 9000
+EXPOSE 9000
+CMD ["gunicorn", "--bind=0.0.0.0:9000", "--chdir=/loris/www", "loris2"]
diff --git a/loris/loris2.conf b/loris/loris2.conf
new file mode 100644
index 0000000000..e3fe670cd0
--- /dev/null
+++ b/loris/loris2.conf
@@ -0,0 +1,108 @@
+[loris.Loris]
+tmp_dp = '/tmp/loris2/tmp' # r--
+www_dp = '/var/www/loris2' # r-x
+run_as_user = 'loris'
+run_as_group = 'loris'
+enable_caching = True
+redirect_canonical_image_request = False
+redirect_id_slash_to_info = True
+
+# max_size_above_full restricts interpolation of images on the server.
+# Default value 200 means that a user cannot request image sizes greater than
+# 200% of original image size (width or height).
+# Set this value to 100 to disallow interpolation. Set to 0 to remove
+# size restriction.
+max_size_above_full = 200
+
+[logging]
+log_to = 'console'    # 'console'|'file'
+log_level = 'INFO' # 'DEBUG'|'INFO'|'WARNING'|'ERROR'|'CRITICAL'
+#log_dir = '/var/log/loris2' # rw-
+#max_size = 5242880 # 5 MB
+#max_backups = 5
+format = '%(asctime)s (%(name)s) [%(levelname)s]: %(message)s'
+
+[resolver]
+impl = 'loris.resolver.SimpleFSResolver'
+src_img_root = '/usr/local/share/images' # r--
+
+#Example of one version of SimpleHTTResolver config
+
+#[resolver]
+#impl = 'loris.resolver.SimpleHTTPResolver'
+#source_prefix='https://<server>/fedora/objects/'
+#source_suffix='/datastreams/accessMaster/content'
+#cache_root='/usr/local/share/images/loris'
+#user='<if needed else remove this line>'
+#pw='<if needed else remove this line>'
+#cert='<SSL client cert for authentication>'
+#key='<SSL client key for authentication>'
+#ssl_check='<Check for SSL errors. Defaults to True. Set to False to ignore issues with self signed certificates>'
+
+# Sample config for TemplateHTTResolver config
+# [resolver]
+# impl = 'loris.resolver.TemplateHTTPResolver'
+# cache_root='/usr/local/share/images/loris'
+## optional settings
+# delimiter = "|" # optional delimiter for splitting identifier, allowing for n-values to be inserted into the template
+# default_format
+# head_resolvable = False
+# templates = 'a, b, fedora, devfedora, fedora_obj_ds'
+# [[a]]
+# url='http://example.edu/images/%s'
+# [[b]]
+# url='http://example.edu/images-elsewhere/%s'
+## optional overrides for requests using this template
+# user='otheruser'
+# pw='secret'
+# [[fedora]]
+# url='http://<server>/fedora/objects/%s/datastreams/accessMaster/content'
+## optional overrides for requests using this template
+# cert='/path/to/client.pem'
+# key='/path/to/client.key'
+# [[fedora_obj_ds]]
+# url = 'http://<server>/fedora/objects/%s/datastreams/%s/content' # as used with delimiter option below
+
+[img.ImageCache]
+# must be on the same volume as tmp_dp, for atomic move operations
+# will crash if on another volume
+cache_dp = '/var/cache/loris' # rwx
+
+[img_info.InfoCache]
+# must be on the same volume as tmp_dp, for atomic move operations
+# will crash if on another volume
+cache_dp = '/var/cache/loris' # rwx
+
+[transforms]
+dither_bitonal_images = False
+target_formats = ['jpg','png','gif','webp']
+
+    [[jpg]]
+    impl = 'JPG_Transformer'
+
+    [[tif]]
+    impl = 'TIF_Transformer'
+
+    [[png]]
+    impl = 'PNG_Transformer'
+
+#    [[jp2]]
+#    impl = 'KakaduJP2Transformer'
+#    tmp_dp = '/tmp/loris/tmp/jp2' # rwx
+#    kdu_expand = '/usr/local/bin/kdu_expand' # r-x
+#    kdu_libs = '/usr/local/lib' # r--
+#    num_threads = '4' # string!
+#    mkfifo = '/usr/bin/mkfifo' # r-x
+#    map_profile_to_srgb = False
+#    srgb_profile_fp = '/usr/share/color/icc/colord/sRGB.icc' # r--
+
+   [[jp2]]
+   src_format = 'jp2'
+   impl = 'OPJ_JP2Transformer'
+   tmp_dp = '/tmp/loris/tmp/jp2' # rwx
+   opj_decompress = '/usr/local/bin/opj_decompress' # r-x
+   opj_libs = '/usr/local/lib' # r--
+   mkfifo = '/usr/bin/mkfifo' # r-x
+   map_profile_to_srgb = True
+   srgb_profile_fp = '/usr/share/color/icc/colord/sRGB.icc' # r--
+
diff --git a/nginx/proxy.conf b/nginx/proxy.conf
index 9e8cf316f1..9f613e1cef 100644
--- a/nginx/proxy.conf
+++ b/nginx/proxy.conf
@@ -1,26 +1,13 @@
-upstream iip {
-	server iipsrv:9000;
+upstream loris {
+	server loris:9000;
 }
 
 server {
   listen 80;
 
 	location / {
-		add_header "Content-Type" "text/plain";
-		return 200 "Horae";
-	}
-
-  location /images {
-    fastcgi_param   PATH_INFO /iipsrv.fcgi; #$fastcgi_script_name;
-    fastcgi_param   REQUEST_METHOD $request_method;
-    fastcgi_param   QUERY_STRING $query_string;
-    fastcgi_param   CONTENT_TYPE $content_type;
-    fastcgi_param   CONTENT_LENGTH $content_length;
-    fastcgi_param   SERVER_PROTOCOL $server_protocol;
-    fastcgi_param   REQUEST_URI $request_uri;
-    fastcgi_param   HTTPS $https if_not_empty;
-
-		# Forward to iipsrv instance
-    fastcgi_pass    iip;
+    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+    proxy_redirect off;
+    proxy_pass http://loris;
   }
 }
-- 
GitLab