diff --git a/services/features.js b/services/features.js index 9171175..1949d6f 100644 --- a/services/features.js +++ b/services/features.js @@ -5,11 +5,9 @@ const async = require('async'), common = require('service-utils'), GeoPoint = require('geopoint'), HttpStatus = require('http-status-codes'), - pg = require('pg'), - process = require('process'), + postgres = require('./postgres'), ServiceError = common.utils.ServiceError, - turf = require('turf'), - url = require('url'); + turf = require('turf'); let featureDatabasePool; @@ -118,23 +116,14 @@ function getByName(query, callback) { } function init(callback) { - if (!process.env.FEATURES_CONNECTION_STRING) - return callback(new ServiceError(HttpStatus.INTERNAL_SERVER_ERROR, "FEATURES_CONNECTION_STRING configuration not provided as environment variable")); + postgres.init(function(err, pool) { + if (err) { + return callback(err); + } - const params = url.parse(process.env.FEATURES_CONNECTION_STRING); - const auth = params.auth.split(':'); - - const config = { - user: auth[0], - password: auth[1], - host: params.hostname, - port: params.port, - database: params.pathname.split('/')[1] - }; - - featureDatabasePool = new pg.Pool(config); - - return callback(); + featureDatabasePool = pool; + return callback(); + }); } /* diff --git a/services/postgres.js b/services/postgres.js new file mode 100644 index 0000000..975f63a --- /dev/null +++ b/services/postgres.js @@ -0,0 +1,34 @@ +const pg = require('pg'), + process = require('process'), + querystring = require('querystring'), + url = require('url'); + +function init(callback) { + const connectionString = process.env.FEATURES_CONNECTION_STRING; + + if (!connectionString) { + const err = new ServiceError(HttpStatus.INTERNAL_SERVER_ERROR, "FEATURES_CONNECTION_STRING configuration not provided as environment variable"); + return callback(err); + } + + const params = url.parse(connectionString); + const query = querystring.parse(params.query); + const auth = params.auth.split(':'); + + const config = { + user: auth[0], + password: auth[1], + host: params.hostname, + port: params.port, + ssl: query['ssl'], + database: params.pathname.split('/')[1] + }; + + const featureDatabasePool = new pg.Pool(config); + + return callback(null, featureDatabasePool); +} + +module.exports = { + init: init +}; diff --git a/services/visits.js b/services/visits.js index 3e28ba3..a719777 100644 --- a/services/visits.js +++ b/services/visits.js @@ -5,10 +5,8 @@ const async = require('async'), common = require('service-utils'), HttpStatus = require('http-status-codes'), log = common.services.log("featureService/services/visits"), - pg = require('pg'), - process = require('process'), + postgres = require('./postgres'), ServiceError = common.utils.ServiceError, - url = require('url'), uuid = require('uuid/v4'); let featureTablePool; @@ -80,27 +78,14 @@ function getByUserId(userId, callback) { } function init(callback) { - if (!process.env.FEATURES_CONNECTION_STRING) - return callback(new ServiceError(HttpStatus.INTERNAL_SERVER_ERROR, "FEATURES_CONNECTION_STRING configuration not provided as environment variable")); + postgres.init(function(err, pool) { + if (err) { + return callback(err); + } - // POSTGRES CONNECTION CODE - - log.info('connecting to features database using: ' + process.env.FEATURES_CONNECTION_STRING); - - const params = url.parse(process.env.FEATURES_CONNECTION_STRING); - const auth = params.auth.split(':'); - - const config = { - user: auth[0], - password: auth[1], - host: params.hostname, - port: params.port, - database: params.pathname.split('/')[1] - }; - - featureTablePool = new pg.Pool(config); - - return callback(); + featureTablePool = pool; + return callback(); + }); } function put(visits, callback) {