Fixes #12, #14: added the capability to sync the `api-gateway-config` folder from the cloud ( S3 for now )
diff --git a/Dockerfile b/Dockerfile
index dde1efb..b60d0d2 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,43 +1,44 @@
 # apigateway
 #
-# VERSION               1.9.3.1
+# VERSION               1.9.7.3
 #
 # From https://hub.docker.com/_/alpine/
 #
 FROM alpine:latest
 
+# install dependencies
 RUN apk update \
     && apk add gcc tar libtool zlib jemalloc jemalloc-dev perl \ 
     make musl-dev openssl-dev pcre-dev g++ zlib-dev curl python \
-    perl-test-longstring perl-list-moreutils perl-http-message
+    perl-test-longstring perl-list-moreutils perl-http-message \
+    geoip-dev
 
-ENV OPENRESTY_VERSION 1.9.3.1
-ENV NAXSI_VERSION 0.53-2
-ENV PCRE_VERSION 8.37
-ENV TEST_NGINX_VERSION 0.24
-ENV HMAC_LUA_VERSION 1.0.0
-ENV REQUEST_VALIDATION_VERSION 1.0.1
+# openresty build
+ENV OPENRESTY_VERSION=1.9.7.3 \
+    NAXSI_VERSION=0.53-2 \
+    PCRE_VERSION=8.37 \
+    TEST_NGINX_VERSION=0.24 \
+    _prefix=/usr/local \
+    _exec_prefix=/usr/local \
+    _localstatedir=/var \
+    _sysconfdir=/etc \
+    _sbindir=/usr/local/sbin
 
-RUN mkdir -p /tmp/api-gateway/
-
-RUN  cd /tmp/api-gateway/ \
-     && curl -L https://github.com/nbs-system/naxsi/archive/${NAXSI_VERSION}.tar.gz -o /tmp/api-gateway/naxsi-${NAXSI_VERSION}.tar.gz \
-     && curl -L http://downloads.sourceforge.net/project/pcre/pcre/${PCRE_VERSION}/pcre-${PCRE_VERSION}.tar.gz -o /tmp/api-gateway/prce-${PCRE_VERSION}.tar.gz \
-     && curl -L https://github.com/adobe-apiplatform/apigateway/releases/download/openresty_backup_1.9.3.1/ngx_openresty-1.9.3.1.tar.gz -o /tmp/api-gateway/ngx_openresty-${OPENRESTY_VERSION}.tar.gz \
-     && curl -L https://github.com/adobe-apiplatform/api-gateway-request-validation/archive/${REQUEST_VALIDATION_VERSION}.tar.gz -o /tmp/api-gateway/api-gateway-request-validation-${REQUEST_VALIDATION_VERSION}.tar.gz \
-     && curl -L https://github.com/adobe-apiplatform/api-gateway-hmac/archive/${HMAC_LUA_VERSION}.tar.gz -o /tmp/api-gateway/api-gateway-hmac-${HMAC_LUA_VERSION}.tar.gz \
-     && tar -xf ./ngx_openresty-${OPENRESTY_VERSION}.tar.gz \
-     && tar -xf ./prce-${PCRE_VERSION}.tar.gz \
-     && tar -xf ./naxsi-${NAXSI_VERSION}.tar.gz \
-     && cd /tmp/api-gateway/ngx_openresty-${OPENRESTY_VERSION} \ 
+RUN  echo " ... adding Openresty, NGINX, NAXSI and PCRE" \
+     && mkdir -p /tmp/api-gateway \
      && readonly NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) \
      && echo "using up to $NPROC threads" \
-     && _prefix="/usr/local" \
-     && _exec_prefix="$_prefix" \
-     && _localstatedir="/var" \
-     && _sysconfdir="/etc" \
-     && _sbindir="$_exec_prefix/sbin" \
-     && echo "building debugging version of the api-gateway ... " \
+
+     && cd /tmp/api-gateway/ \
+     && curl -k -L https://github.com/nbs-system/naxsi/archive/${NAXSI_VERSION}.tar.gz -o /tmp/api-gateway/naxsi-${NAXSI_VERSION}.tar.gz \
+     && curl -k -L http://downloads.sourceforge.net/project/pcre/pcre/${PCRE_VERSION}/pcre-${PCRE_VERSION}.tar.gz -o /tmp/api-gateway/pcre-${PCRE_VERSION}.tar.gz \
+     && curl -k -L https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz -o /tmp/api-gateway/openresty-${OPENRESTY_VERSION}.tar.gz \
+     && tar -zxf ./openresty-${OPENRESTY_VERSION}.tar.gz \
+     && tar -zxf ./pcre-${PCRE_VERSION}.tar.gz \
+     && tar -zxf ./naxsi-${NAXSI_VERSION}.tar.gz \
+     && cd /tmp/api-gateway/openresty-${OPENRESTY_VERSION} \ 
+
+     && echo "        - building debugging version of the api-gateway ... " \
      && ./configure \
             --prefix=${_exec_prefix}/api-gateway \
             --sbin-path=${_sbindir}/api-gateway-debug \
@@ -48,8 +49,23 @@
             --lock-path=${_localstatedir}/run/api-gateway.lock \
             --add-module=../naxsi-${NAXSI_VERSION}/naxsi_src/ \
             --with-pcre=../pcre-${PCRE_VERSION}/ --with-pcre-jit \
+            --with-stream \
+            --with-stream_ssl_module \
             --with-http_ssl_module \
             --with-http_stub_status_module \
+            --with-http_realip_module \
+            --with-http_addition_module \
+            --with-http_sub_module \
+            --with-http_dav_module \
+            --with-http_geoip_module \
+            --with-http_gunzip_module  \
+            --with-http_gzip_static_module \
+            --with-http_auth_request_module \
+            --with-http_random_index_module \
+            --with-http_secure_link_module \
+            --with-http_degradation_module \
+            --with-http_auth_request_module  \
+            --with-http_v2_module \
             --with-luajit \
             --without-http_ssi_module \
             --without-http_userid_module \
@@ -59,7 +75,8 @@
             -j${NPROC} \
     && make -j${NPROC} \
     && make install \
-    && echo "building regular version of the api-gateway ... " \
+
+    && echo "        - building regular version of the api-gateway ... " \
     && ./configure \
             --prefix=${_exec_prefix}/api-gateway \
             --sbin-path=${_sbindir}/api-gateway \
@@ -70,8 +87,23 @@
             --lock-path=${_localstatedir}/run/api-gateway.lock \
             --add-module=../naxsi-${NAXSI_VERSION}/naxsi_src/ \
             --with-pcre=../pcre-${PCRE_VERSION}/ --with-pcre-jit \
+            --with-stream \
+            --with-stream_ssl_module \
             --with-http_ssl_module \
             --with-http_stub_status_module \
+            --with-http_realip_module \
+            --with-http_addition_module \
+            --with-http_sub_module \
+            --with-http_dav_module \
+            --with-http_geoip_module \
+            --with-http_gunzip_module  \
+            --with-http_gzip_static_module \
+            --with-http_auth_request_module \
+            --with-http_random_index_module \
+            --with-http_secure_link_module \
+            --with-http_degradation_module \
+            --with-http_auth_request_module  \
+            --with-http_v2_module \
             --with-luajit \
             --without-http_ssi_module \
             --without-http_userid_module \
@@ -80,27 +112,129 @@
             -j${NPROC} \
     && make -j${NPROC} \
     && make install \
-    && echo " ... installing api-gateway-hmac ..." \
+
+    && echo "        - adding Nginx Test support" \
+    && curl -k -L https://github.com/openresty/test-nginx/archive/v${TEST_NGINX_VERSION}.tar.gz -o ${_prefix}/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
+    && cd ${_prefix} \
+    && tar -xf ${_prefix}/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
+    && rm ${_prefix}/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
+    && cp -r ${_prefix}/test-nginx-0.24/inc/* /usr/local/share/perl5/site_perl/ \
+
+    && ln -s ${_sbindir}/api-gateway-debug ${_sbindir}/nginx \
+    && cp /tmp/api-gateway/openresty-${OPENRESTY_VERSION}/build/install ${_prefix}/api-gateway/bin/resty-install \
+    && apk del g++ gcc make \
+    && rm -rf /var/cache/apk/* \
+    && rm -rf /tmp/api-gateway
+
+ENV CONFIG_SUPERVISOR_VERSION initial-poc
+ENV GOPATH /usr/lib/go/bin
+ENV GOBIN  /usr/lib/go/bin
+ENV PATH   $PATH:/usr/lib/go/bin
+RUN echo " ... installing api-gateway-config-supervisor  ... " \
+    && echo "http://dl-4.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
+    && apk update \
+    && apk add make git go \
+    && mkdir -p /tmp/api-gateway \
+    && curl -k -L https://github.com/adobe-apiplatform/api-gateway-config-supervisor/archive/${CONFIG_SUPERVISOR_VERSION}.tar.gz -o /tmp/api-gateway/api-gateway-config-supervisor-${CONFIG_SUPERVISOR_VERSION}.tar.gz \
+    && cd /tmp/api-gateway \
+    && tar -xf /tmp/api-gateway/api-gateway-config-supervisor-${CONFIG_SUPERVISOR_VERSION}.tar.gz \
+    && mkdir -p /tmp/go \
+    && mv /tmp/api-gateway/api-gateway-config-supervisor-${CONFIG_SUPERVISOR_VERSION}/* /tmp/go \
+    && cd /tmp/go \
+    && make setup \
+    && mkdir -p /tmp/go/Godeps/_workspace \
+    && ln -s /tmp/go/vendor /tmp/go/Godeps/_workspace/src \
+    && mkdir -p /tmp/go-src/src/github.com/adobe-apiplatform \
+    && ln -s /tmp/go /tmp/go-src/src/github.com/adobe-apiplatform/api-gateway-config-supervisor \
+    && GOPATH=/tmp/go/vendor:/tmp/go-src CGO_ENABLED=0 GOOS=linux /usr/lib/go/bin/godep  go build -ldflags "-s" -a -installsuffix cgo -o api-gateway-config-supervisor ./ \
+    && mv /tmp/go/api-gateway-config-supervisor /usr/local/sbin/ \
+
+    && echo "installing rclone sync ... " \
+    && go get github.com/ncw/rclone \
+    && mv /usr/lib/go/bin/rclone /usr/local/sbin/ \
+
+    && echo " cleaning up ... " \
+    && rm -rf /usr/lib/go/bin/src \
+    && rm -rf /tmp/go \
+    && rm -rf /tmp/go-src \
+    && rm -rf /usr/lib/go/bin/pkg/ \
+    && rm -rf /usr/lib/go/bin/godep \
+    && apk del make git go \
+    && rm -rf /var/cache/apk/*
+
+RUN echo " ... installing aws-cli ..." \
+    && apk update \
+    && apk add python \
+    && apk add py-pip \
+    && pip install --upgrade pip \
+    && pip install awscli
+
+ENV HMAC_LUA_VERSION 1.0.0
+RUN echo " ... installing api-gateway-hmac ..." \
+    && apk update \
+    && apk add make \
+    && mkdir -p /tmp/api-gateway \
+    && curl -k -L https://github.com/adobe-apiplatform/api-gateway-hmac/archive/${HMAC_LUA_VERSION}.tar.gz -o /tmp/api-gateway/api-gateway-hmac-${HMAC_LUA_VERSION}.tar.gz \
     && tar -xf /tmp/api-gateway/api-gateway-hmac-${HMAC_LUA_VERSION}.tar.gz -C /tmp/api-gateway/ \
     && cd /tmp/api-gateway/api-gateway-hmac-${HMAC_LUA_VERSION} \
+    && cp -r /usr/local/test-nginx-${TEST_NGINX_VERSION}/* ./test/resources/test-nginx/ \
+    && make test \
     && make install \
-         LUA_LIB_DIR=${_exec_prefix}/api-gateway/lualib \
-         INSTALL=/tmp/api-gateway/ngx_openresty-${OPENRESTY_VERSION}/build/install \
-    && echo " ... installing api-gateway-request-validation ..." \
+            LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
+            INSTALL=${_prefix}/api-gateway/bin/resty-install \
+    && rm -rf /tmp/api-gateway
+
+ENV REQUEST_VALIDATION_VERSION 1.0.2
+RUN echo " ... installing api-gateway-request-validation ..." \
+    && apk update \
+    && apk add make \
+    && mkdir -p /tmp/api-gateway \
+    && curl -k -L https://github.com/adobe-apiplatform/api-gateway-request-validation/archive/${REQUEST_VALIDATION_VERSION}.tar.gz -o /tmp/api-gateway/api-gateway-request-validation-${REQUEST_VALIDATION_VERSION}.tar.gz \
     && tar -xf /tmp/api-gateway/api-gateway-request-validation-${REQUEST_VALIDATION_VERSION}.tar.gz -C /tmp/api-gateway/ \
     && cd /tmp/api-gateway/api-gateway-request-validation-${REQUEST_VALIDATION_VERSION} \
+    && cp -r /usr/local/test-nginx-${TEST_NGINX_VERSION}/* ./test/resources/test-nginx/ \
+    && apk update && apk add redis \
+    && REDIS_SERVER=/usr/bin/redis-server make test \
     && make install \
-         LUA_LIB_DIR=${_exec_prefix}/api-gateway/lualib \
-         INSTALL=/tmp/api-gateway/ngx_openresty-${OPENRESTY_VERSION}/build/install \
+            LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
+            INSTALL=${_prefix}/api-gateway/bin/resty-install \
+    && apk del redis \
     && rm -rf /var/cache/apk/* \
-    && rm -rf /tmp/api-gateway \
-    && echo " ... adding Nginx Test support" \
-    && curl -L https://github.com/openresty/test-nginx/archive/v${TEST_NGINX_VERSION}.tar.gz -o /usr/local/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
-    && cd /usr/local/ \
-    && tar -xf /usr/local/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
-    && rm /usr/local/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
-    && ln -s /usr/local/sbin/api-gateway-debug /usr/local/sbin/nginx
+    && rm -rf /tmp/api-gateway
 
+ENV LUA_RESTY_HTTP_VERSION 0.07
+RUN echo " ... installing lua-resty-http..." \
+    && apk update \
+    && apk add make \
+    && mkdir -p /tmp/api-gateway \
+    && curl -k -L https://github.com/pintsized/lua-resty-http/archive/v${LUA_RESTY_HTTP_VERSION}.tar.gz -o /tmp/api-gateway/lua-resty-http-${LUA_RESTY_HTTP_VERSION}.tar.gz \
+    && tar -xf /tmp/api-gateway/lua-resty-http-${LUA_RESTY_HTTP_VERSION}.tar.gz -C /tmp/api-gateway/ \
+    && cd /tmp/api-gateway/lua-resty-http-${LUA_RESTY_HTTP_VERSION} \
+    && make install \
+            LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
+            INSTALL=${_prefix}/api-gateway/bin/resty-install \
+    && rm -rf /tmp/api-gateway
+
+ENV LUA_RESTY_IPUTILS_VERSION 0.2.0
+RUN echo " ... installing lua-resty-iputils..." \
+    && apk update \
+    && apk add make \
+    && mkdir -p /tmp/api-gateway \
+    && curl -k -L https://github.com/hamishforbes/lua-resty-iputils/archive/v${LUA_RESTY_IPUTILS_VERSION}.tar.gz -o /tmp/api-gateway/lua-resty-iputils-${LUA_RESTY_IPUTILS_VERSION}.tar.gz \
+    && tar -xf /tmp/api-gateway/lua-resty-iputils-${LUA_RESTY_IPUTILS_VERSION}.tar.gz -C /tmp/api-gateway/ \
+    && cd /tmp/api-gateway/lua-resty-iputils-${LUA_RESTY_IPUTILS_VERSION} \
+    && export LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
+    && export INSTALL=${_prefix}/api-gateway/bin/resty-install \
+    && $INSTALL -d ${LUA_LIB_DIR}/resty \
+    && $INSTALL lib/resty/*.lua ${LUA_LIB_DIR}/resty/ \
+    && rm -rf /tmp/api-gateway
+
+RUN \
+    curl -L -k -s -o /usr/local/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 \
+    && apk update \
+    && apk add gawk \
+    && chmod 755 /usr/local/bin/jq \
+    && rm -rf /var/cache/apk/*
 
 COPY init.sh /etc/init-container.sh
 ONBUILD COPY init.sh /etc/init-container.sh
diff --git a/README.md b/README.md
index b344848..37cb568 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@
 ```
 docker run --name="apigateway" \
             -p 80:80 \
-            -e "MARATHON_HOST=http://<marathon_host>:<port>/" \
+            -e "MARATHON_HOST=http://<marathon_host>:<port>" \
             -e "LOG_LEVEL=info" \
             adobeapiplatform/apigateway:latest
 ```
@@ -15,7 +15,7 @@
 This command starts an API Gateway that automatically discovers the services running in Marathon.
 The discovered services are exposed on individual VHosts as you can see in the [config file](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/conf.d/marathon_apis.conf#L36).
 
-#### Accessing a Marathon app
+#### Accessing a Marathon app locally
 
 For example, if you have an application named `hello-world` you can access it on its VHost in 2 ways:
 
@@ -25,6 +25,54 @@
 The [discovery script](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/marathon-service-discovery.sh) is provided as an example for a quick-start and it can be replaced with your favourite discovery mechanism.
 The script updates a [configuration file](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/environment.conf.d/api-gateway-upstreams.http.conf) containing all the NGINX upstreams that are used in the [config file](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/conf.d/marathon_apis.conf#L36).
 
+#### Accessing a Marathon app remotely
+
+All you need to do is to create a DNS entry like `*.api.example.com` or `*.gw.example.com` and have it resolve to the nodes running the Gateway.
+
+Assuming there is an application deployed in Marathon called `hello-world` it can be accessed at the URL: `http://hello-world.api.example.com`
+The Gateway is automatically proxying the request to the `hello-world` application in Marathon.
+
+If you call `http://my-custom-app.api.example.com` the Gateway will proxy to a Marathon app named `my-custom-app`.
+If the application is not deployed in Marathon a 5xx error is returned back to the client. This behaviour is ofcourse configurable.
+
+#### Running with a different configuration folder
+
+One way to simplify the configuration of the Gateway is to copy the `api-gateway-config` folder in S3, having all nodes syncing from that location.
+For the moment only `AWS S3` is supported but the plan is to add support for other clouds too.
+
+In order to work with S3 the Gateway needs to know the bucket. The command is similar to the one above with an extra environment variable named `REMOTE_CONFIG`
+```
+docker run --name="apigateway" \
+            -p 80:80 \
+            -e "MARATHON_HOST=http://<marathon_host>:<port>" \
+            -e "REMOTE_CONFIG=s3://api-gateway-config-bucket" \
+            -e "LOG_LEVEL=info" \
+            adobeapiplatform/apigateway:latest
+```
+
+`s3://api-gateway-config-bucket` should contain something similar to what's in the [api-gateway-config](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/) folder.
+For any customizations it would be good to start from there.
+There are only 2 files that won't be synchronised:
+* `/etc/api-gateway/conf.d/includes/resolvers.conf` . Read bellow for more info
+* `https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/environment.conf.d/api-gateway-upstreams.http.conf` which is automatically generated by the Marathon Discovery script.
+
+By default the access to the S3 bucket is done using IAM roles, but AWS Credentials can also be specified through environment variables:
+```
+docker run --name="apigateway" \
+            -p 80:80 \
+            -e "MARATHON_HOST=http://<marathon_host>:<port>" \
+            -e "REMOTE_CONFIG=s3://api-gateway-config-bucket" \
+            -e "AWS_ACCESS_KEY_ID=--change-me--" \
+            -e "AWS_SECRET_ACCESS_KEY=--change-me--" \
+            -e "LOG_LEVEL=info" \
+            adobeapiplatform/apigateway:latest
+```
+By default the remote config is checked for changes every `10s` and it's configurable through the `REMOTE_CONFIG_SYNC_INTERVAL` env variable.
+There are a few things to take into consideration when changing this value:
+* How often the configs really change
+* Cost. Some tools may make 1 API request per file to compare it. I.e. in S3 72 config files checked every `10s` costs `$7.46` but when checked every `30s` it's only `$2.4`, times number of GW nodes.
+* Average time for an API Request. When reloading the GW the existing NGINX processes handling active connections are kept in the background until the request completes. So reloading the Gateway too fast may have the side effect of keeping too many processes running at the same time. This may, or may not be a problem but it's good to be aware of it.
+
 #### Resolvers
 While starting up this container automatically creates the `/etc/api-gateway/conf.d/includes/resolvers.conf` config file using `/etc/resolv.conf` as the source.
 To learn more about the `resolver` directive in NGINX see the [docs](http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver).
diff --git a/api-gateway-config/marathon-service-discovery.sh b/api-gateway-config/marathon-service-discovery.sh
index dd0d265..93254a2 100755
--- a/api-gateway-config/marathon-service-discovery.sh
+++ b/api-gateway-config/marathon-service-discovery.sh
@@ -70,18 +70,8 @@
 fi
 
 # 2 check for changes
-changed_files=$(find /etc/api-gateway -type f -newer /var/run/apigateway-config-watcher.lastrun -print)
 cmp -s ${TMP_FILE} ${UPSTREAM_FILE}
 changed_upstreams=$?
-if [[ \( -n "${changed_files}" \) -o \( ${changed_upstreams} -gt 0 \) ]]; then
-    info_log "discovered changed files ..."
-    info_log ${changed_files}
+if [[ \( ${changed_upstreams} -gt 0 \) ]]; then
     cp ${TMP_FILE} ${UPSTREAM_FILE}
-    echo `date` > /var/run/apigateway-config-watcher.lastrun
-    info_log "reloading gateway ..."
-    api-gateway -t -p /usr/local/api-gateway/ -c /etc/api-gateway/api-gateway.conf && api-gateway -s reload
 fi
-echo `date` > /var/run/apigateway-config-watcher.lastrun
-
-# 2. diff with an existing one
-# cmp -b $TMP_FILE $UPSTREAM_FILE || (info_log "discovered a change..." && cp $TMP_FILE $UPSTREAM_FILE && info_log "reloading gateway ..." && api-gateway -t -p /usr/local/api-gateway/ -c /etc/api-gateway/api-gateway.conf && api-gateway -s reload)
\ No newline at end of file
diff --git a/init.sh b/init.sh
index e9fb869..98d64d5 100755
--- a/init.sh
+++ b/init.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 #/*
 # * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
 # *
@@ -21,10 +21,14 @@
 # * DEALINGS IN THE SOFTWARE.
 # *
 # */
-debug_mode=$(echo $DEBUG)
+debug_mode=${DEBUG}
 log_level=${LOG_LEVEL:-warn}
-marathon_host=$(echo $MARATHON_HOST)
+marathon_host=${MARATHON_HOST}
 sleep_duration=${MARATHON_POLL_INTERVAL:-5}
+# location for a remote /etc/api-gateway folder.
+# i.e s3://api-gateway-config
+remote_config=${REMOTE_CONFIG}
+remote_config_sync_interval=${REMOTE_CONFIG_SYNC_INTERVAL:-10s}
 
 echo "Starting api-gateway ..."
 if [ "${debug_mode}" == "true" ]; then
@@ -39,6 +43,23 @@
 echo resolver $(awk 'BEGIN{ORS=" "} /nameserver/{print $2}' /etc/resolv.conf | sed "s/ $/;/g") > /etc/api-gateway/conf.d/includes/resolvers.conf
 echo "   ...  with dns $(cat /etc/api-gateway/conf.d/includes/resolvers.conf)"
 
+sync_cmd="echo checking for changes ..."
+if [[ -n "${remote_config}" ]]; then
+    echo "   ... using a remote config from: ${remote_config}"
+    if [[ "${remote_config}" =~ ^s3://.+ ]]; then
+      sync_cmd="aws s3 sync --exclude *resolvers.conf --exclude *environment.conf.d/*vars.server.conf --exclude *environment.conf.d/*upstreams.http.conf --delete ${remote_config} /etc/api-gateway/"
+      echo "   ... syncing from s3 using command ${sync_cmd}"
+    else
+      echo "   ... but this REMOTE_CONFIG is not supported "
+    fi
+fi
+api-gateway-config-supervisor \
+        --reload-cmd="api-gateway -s reload" \
+        --sync-folder=/etc/api-gateway \
+        --sync-interval=${remote_config_sync_interval} \
+        --sync-cmd="${sync_cmd}" \
+        --http-addr=127.0.0.1:8888 &
+
 if [[ -n "${marathon_host}" ]]; then
     echo "  ... starting Marathon Service Discovery on ${marathon_host}"
     touch /var/run/apigateway-config-watcher.lastrun
@@ -55,7 +76,7 @@
     done &
 fi
 
-echo "   ...  testing configuration "
+echo "   ... testing configuration "
 api-gateway -t -p /usr/local/api-gateway/ -c /etc/api-gateway/api-gateway.conf
 
 echo "   ... using log level: '${log_level}'. Override it with -e 'LOG_LEVEL=<level>' "