Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions services/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
});
}

/*
Expand Down
34 changes: 34 additions & 0 deletions services/postgres.js
Original file line number Diff line number Diff line change
@@ -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
};
31 changes: 8 additions & 23 deletions services/visits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down