Skip to content

Commit

Permalink
refactored how to detect cloud providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Damm committed Jan 16, 2020
1 parent aecc60e commit 940576b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
19 changes: 8 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
@@ -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')
}
Expand Down Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions setup-database.js

This file was deleted.

12 changes: 12 additions & 0 deletions util-cloud-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const isRunningOnHeroku = () => {
return (process.env.DYNO) ? true : false
}

const isRunningOnGoogle = () => {
return (process.env.GAE_ENV === 'standard')
}

module.exports = {
isRunningOnHeroku,
isRunningOnGoogle
}
42 changes: 31 additions & 11 deletions util-pg.js → util-heroku-pg.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down Expand Up @@ -83,14 +110,7 @@ const readConfiguration = (client) => {
}

module.exports = {
convertToString,
generateSessionExirationDate,
createClient,
createTableIfNotExists,
truncateTable,
writeConfiguration,
readConfiguration,
createConfigurationIfNotExists,
getConfiguration,
setConfiguration,
validateConfiguration
setConfiguration
}

0 comments on commit 940576b

Please sign in to comment.