#!/bin/sh
set -e

if [ "${NGINX_CACHE_ENABLED:-false}" = "true" ]; then
    NGINX_CACHE_CONFIG='        proxy_cache app_cache;
        proxy_cache_bypass $http_cache_control;
        proxy_cache_valid 200 1m;
        proxy_cache_use_stale error timeout updating;
        proxy_cache_lock on;
        add_header X-Cache-Status $upstream_cache_status;'
fi

export NGINX_CACHE_CONFIG
export NGINX_CACHE_MAX_SIZE="${NGINX_CACHE_MAX_SIZE:-1g}"

mkdir -p /etc/nginx/conf.d

envsubst '${NGINX_CACHE_CONFIG} ${NGINX_CACHE_MAX_SIZE}' \
    < /etc/nginx/templates/default.conf.template \
    > /etc/nginx/conf.d/default.conf

exec nginx -g 'daemon off;'
