From 36c854ef1bdc3305c8f10c55688c137e6bb5e8c4 Mon Sep 17 00:00:00 2001 From: emil-lengman Date: Sat, 22 Aug 2020 20:44:32 +0200 Subject: [PATCH] move creating the config file to a js file --- docker-entrypoint.js | 105 +++++++++++++++++++++++++++++++++++++++++++ docker-entrypoint.sh | 76 +------------------------------ 2 files changed, 106 insertions(+), 75 deletions(-) create mode 100644 docker-entrypoint.js diff --git a/docker-entrypoint.js b/docker-entrypoint.js new file mode 100644 index 0000000..d902fb8 --- /dev/null +++ b/docker-entrypoint.js @@ -0,0 +1,105 @@ +const { promisify } = require("util"); +const { + HOST, + PORT, + KEY_LENGTH, + MAX_LENGTH, + STATIC_MAX_AGE, + RECOMPRESS_STATIC_ASSETS, + STORAGE_TYPE, + STORAGE_HOST, + STORAGE_PORT, + STORAGE_EXPIRE_SECONDS, + STORAGE_DB, + STORAGE_AWS_BUCKET, + STORAGE_AWS_REGION, + STORAGE_PG_PASSWORD, + STORAGE_PG_USERNAME, + LOGGING_LEVEL, + LOGGING_TYPE, + LOGGING_COLORIZE, + KEYGENERATOR_TYPE, + KEY_GENERATOR_KEYSPACE, + RATE_LIMITS_NORMAL_TOTAL_REQUESTS, + RATE_LIMITS_NORMAL_EVERY_SECONDS, + RATE_LIMITS_WHITELIST_TOTAL_REQUESTS, + RATE_LIMITS_WHITELIST_EVERY_SECONDS, + RATE_LIMITS_WHITELIST, + RATE_LIMITS_BLACKLIST_TOTAL_REQUESTS, + RATE_LIMITS_BLACKLIST_EVERY_SECONDS, + RATE_LIMITS_BLACKLIST, + DOCUMENTS, +} = process.env; + +const config = { + host: HOST, + port: PORT, + + keyLength: KEY_LENGTH, + + maxLength: MAX_LENGTH, + + staticMaxAge: STATIC_MAX_AGE, + + recompressStaticAssets: RECOMPRESS_STATIC_ASSETS, + + logging: [ + { + level: LOGGING_LEVEL, + type: LOGGING_TYPE, + colorize: LOGGING_COLORIZE, + }, + ], + + keyGenerator: { + type: KEYGENERATOR_TYPE, + keyspace: KEY_GENERATOR_KEYSPACE, + }, + + rateLimits: { + whitelist: RATE_LIMITS_WHITELIST ? RATE_LIMITS_WHITELIST.split(",") : [], + blacklist: RATE_LIMITS_BLACKLIST ? RATE_LIMITS_BLACKLIST.split(",") : [], + categories: { + normal: { + totalRequests: RATE_LIMITS_NORMAL_TOTAL_REQUESTS, + every: RATE_LIMITS_NORMAL_EVERY_SECONDS, + }, + whitelist: + RATE_LIMITS_WHITELIST_EVERY_SECONDS || + RATE_LIMITS_WHITELIST_TOTAL_REQUESTS + ? { + totalRequests: RATE_LIMITS_WHITELIST_TOTAL_REQUESTS, + every: RATE_LIMITS_WHITELIST_EVERY_SECONDS, + } + : null, + blacklist: + RATE_LIMITS_BLACKLIST_EVERY_SECONDS || + RATE_LIMITS_BLACKLIST_TOTAL_REQUESTS + ? { + totalRequests: RATE_LIMITS_WHITELIST_TOTAL_REQUESTS, + every: RATE_LIMITS_BLACKLIST_EVERY_SECONDS, + } + : null, + }, + }, + + storage: { + type: STORAGE_TYPE, + host: STORAGE_HOST, + port: STORAGE_PORT, + expire: STORAGE_EXPIRE_SECONDS, + bucket: STORAGE_AWS_BUCKET, + region: STORAGE_AWS_REGION, + connectionUrl: `postgres://${STORAGE_PG_USERNAME}:${STORAGE_PG_PASSWORD}@${STORAGE_HOST}:${STORAGE_PORT}/${STORAGE_DB}`, + db: STORAGE_DB, + }, + + documents: DOCUMENTS + ? DOCUMENTS.split(",").reduce((acc, item) => { + const keyAndValueArray = item.replace(/\s/g, "").split("="); + return { ...acc, [keyAndValueArray[0]]: keyAndValueArray[1] }; + }, {}) + : null, +}; + +console.log(JSON.stringify(config)); diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index d9a99a1..8e4e5a1 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -3,81 +3,7 @@ # We use this file to translate environmental variables to .env files used by the application set -e -function create_document_string () { - IFS=',' read -ra doc_array <<< "$DOCUMENTS" - document_string="{" - - for i in "${doc_array[@]}" - do - IFS='=' read -ra document <<< "$i" - document_string+="\"${document[0]}\": \"${document[1]}\"," - done - - # Remove trailing "," - [[ "$document_string" == *, ]] && document_string=${document_string::${#document_string}-1} - document_string+="}" - echo $document_string -} - -document_string=$(create_document_string) - -echo " -{ - - \"host\": \"$HOST\", - \"port\": ${PORT}, - - \"keyLength\": $KEY_LENGTH, - - \"maxLength\": $MAX_LENGTH, - - \"staticMaxAge\": $STATIC_MAX_AGE, - - \"recompressStaticAssets\": $RECOMPRESS_STATIC_ASSETS, - - \"logging\": [ - { - \"level\": \"$LOGGING_LEVEL\", - \"type\": \"$LOGGING_TYPE\", - \"colorize\": $LOGGING_COLORIZE - } - ], - - \"keyGenerator\": { - \"type\": \"$KEYGENERATOR_TYPE\" - }, - - \"rateLimits\": { - - \"categories\": { - \"normal\": { - \"totalRequests\": $RATELIMITS_NORMAL_TOTAL_REQUESTS, - \"every\": $RATELIMITS_NORMAL_EVERY_SECONDS - }, - \"whitelist\": { - \"totalRequests\": $RATELIMITS_WHITELIST_TOTAL_REQUESTS, - \"every\": $RATELIMITS_WHITELIST_EVERY_SECONDS - }, - \"blacklist\": { - \"totalRequests\": $RATELIMITS_BLACKLIST_TOTAL_REQUESTS, - \"every\": $RATELIMITS_BLACKLIST_EVERY_SECONDS - } - } - }, - - \"storage\": { - \"type\": \"$STORAGE_TYPE\", - \"host\": \"$STORAGE_HOST\", - \"port\": $STORAGE_PORT, - \"expire\": $STORAGE_EXPIRE_SECONDS, - \"db\": $STORAGE_DB - }, - - \"documents\": $document_string - -} -" > config.js - +node ./docker-entrypoint.js > ./config.js exec "$@" \ No newline at end of file