From 940576be093299e611e8760222e0fc57cd728957 Mon Sep 17 00:00:00 2001 From: Matthias Damm Date: Thu, 16 Jan 2020 08:10:48 +0100 Subject: [PATCH] refactored how to detect cloud providers --- app.js | 19 +++++++-------- setup-database.js | 20 ---------------- util-cloud-provider.js | 12 ++++++++++ util-pg.js => util-heroku-pg.js | 42 ++++++++++++++++++++++++--------- 4 files changed, 51 insertions(+), 42 deletions(-) delete mode 100755 setup-database.js create mode 100644 util-cloud-provider.js rename util-pg.js => util-heroku-pg.js (72%) diff --git a/app.js b/app.js index f311034..1c9e411 100755 --- a/app.js +++ b/app.js @@ -1,23 +1,19 @@ 'use-strict' +require('dotenv').config() + var express = require('express') var bodyParser = require('body-parser') var sessions = require('express-session') var compression = require('compression') +var { isRunningOnHeroku } = require('./util-cloud-provider') /* check if the application runs on heroku */ -var util - -if (process.env.DYNO) { - util = require('./util-pg.js') - - // check if configuration exists, if not create it - util.validateConfiguration((error) => { - if (error) { - require('./setup-database') - } - }) +let util +if (isRunningOnHeroku()) { + util = require('./util-heroku-pg.js') + util.createConfigurationIfNotExists() } else { util = require('./util-file.js') } @@ -72,6 +68,7 @@ app.use(function (req, res, next) { util.getConfiguration(function (err, configuration) { if (err) { res.status(500).json({stack: err.stack, message: err.message}) + } else { req.configuration = configuration req.util = util diff --git a/setup-database.js b/setup-database.js deleted file mode 100755 index da22d9e..0000000 --- a/setup-database.js +++ /dev/null @@ -1,20 +0,0 @@ -const { readConfigurationFromFile } = require('./util-file') -const { createClient, createTableIfNotExists, truncateTable, writeConfiguration } = require('./util-pg') - -const client = createClient() - -client.connect().then(() => { - return createTableIfNotExists(client) -}).then((result) => { - return truncateTable(client) -}).then((result) => { - return readConfigurationFromFile() -}).then((configurationAsString) => { - return writeConfiguration(client, configurationAsString) -}).then((result) => { - console.log('table successfully created, configuration saved') - process.exit(0) -}).catch((error) => { - console.log(error) - process.exit(1) -}).then(() => client.end()) diff --git a/util-cloud-provider.js b/util-cloud-provider.js new file mode 100644 index 0000000..1d13bcf --- /dev/null +++ b/util-cloud-provider.js @@ -0,0 +1,12 @@ +const isRunningOnHeroku = () => { + return (process.env.DYNO) ? true : false +} + +const isRunningOnGoogle = () => { + return (process.env.GAE_ENV === 'standard') +} + +module.exports = { + isRunningOnHeroku, + isRunningOnGoogle +} \ No newline at end of file diff --git a/util-pg.js b/util-heroku-pg.js similarity index 72% rename from util-pg.js rename to util-heroku-pg.js index e055a3d..7755466 100755 --- a/util-pg.js +++ b/util-heroku-pg.js @@ -1,7 +1,34 @@ const { Client } = require('pg') -const { convertToString, generateSessionExirationDate } = require('./util-common.js') +const { readConfigurationFromFile } = require('./util-file') + +const createConfigurationIfNotExists = () => { + + hasConfiguration((error) => { + if (error) { + const client = createClient() + + client.connect().then(() => { + return createTableIfNotExists(client) + }).then((result) => { + return truncateTable(client) + }).then((result) => { + return readConfigurationFromFile() + }).then((configurationAsString) => { + return writeConfiguration(client, configurationAsString) + }).then((result) => { + console.log('table successfully created, configuration saved') + process.exit(0) + }).catch((error) => { + console.log(error) + process.exit(1) + }).then(() => client.end()) -const validateConfiguration = (callback) => { + } + }) + +} + +const hasConfiguration = (callback) => { const client = createClient() client.connect().then(() => { @@ -83,14 +110,7 @@ const readConfiguration = (client) => { } module.exports = { - convertToString, - generateSessionExirationDate, - createClient, - createTableIfNotExists, - truncateTable, - writeConfiguration, - readConfiguration, + createConfigurationIfNotExists, getConfiguration, - setConfiguration, - validateConfiguration + setConfiguration } \ No newline at end of file