Skip to content

Commit 83ebc6d

Browse files
c-wtimfpark
authored andcommitted
Add ability to connect to Azure-hosted Postgres (#4)
* Extract copy/pasted postgres initialization code * Add SSL support (required for Azure Postgres)
1 parent c20a249 commit 83ebc6d

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

services/features.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ const async = require('async'),
55
common = require('service-utils'),
66
GeoPoint = require('geopoint'),
77
HttpStatus = require('http-status-codes'),
8-
pg = require('pg'),
9-
process = require('process'),
8+
postgres = require('./postgres'),
109
ServiceError = common.utils.ServiceError,
11-
turf = require('turf'),
12-
url = require('url');
10+
turf = require('turf');
1311

1412
let featureDatabasePool;
1513

@@ -146,23 +144,14 @@ function getByName(query, callback) {
146144
}
147145

148146
function init(callback) {
149-
if (!process.env.FEATURES_CONNECTION_STRING)
150-
return callback(new ServiceError(HttpStatus.INTERNAL_SERVER_ERROR, "FEATURES_CONNECTION_STRING configuration not provided as environment variable"));
147+
postgres.init(function(err, pool) {
148+
if (err) {
149+
return callback(err);
150+
}
151151

152-
const params = url.parse(process.env.FEATURES_CONNECTION_STRING);
153-
const auth = params.auth.split(':');
154-
155-
const config = {
156-
user: auth[0],
157-
password: auth[1],
158-
host: params.hostname,
159-
port: params.port,
160-
database: params.pathname.split('/')[1]
161-
};
162-
163-
featureDatabasePool = new pg.Pool(config);
164-
165-
return callback();
152+
featureDatabasePool = pool;
153+
return callback();
154+
});
166155
}
167156

168157
/*

services/postgres.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const pg = require('pg'),
2+
process = require('process'),
3+
querystring = require('querystring'),
4+
url = require('url');
5+
6+
function init(callback) {
7+
const connectionString = process.env.FEATURES_CONNECTION_STRING;
8+
9+
if (!connectionString) {
10+
const err = new ServiceError(HttpStatus.INTERNAL_SERVER_ERROR, "FEATURES_CONNECTION_STRING configuration not provided as environment variable");
11+
return callback(err);
12+
}
13+
14+
const params = url.parse(connectionString);
15+
const query = querystring.parse(params.query);
16+
const auth = params.auth.split(':');
17+
18+
const config = {
19+
user: auth[0],
20+
password: auth[1],
21+
host: params.hostname,
22+
port: params.port,
23+
ssl: query['ssl'],
24+
database: params.pathname.split('/')[1]
25+
};
26+
27+
const featureDatabasePool = new pg.Pool(config);
28+
29+
return callback(null, featureDatabasePool);
30+
}
31+
32+
module.exports = {
33+
init: init
34+
};

services/visits.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ const async = require('async'),
55
common = require('service-utils'),
66
HttpStatus = require('http-status-codes'),
77
log = common.services.log("featureService/services/visits"),
8-
pg = require('pg'),
9-
process = require('process'),
8+
postgres = require('./postgres'),
109
ServiceError = common.utils.ServiceError,
11-
url = require('url'),
1210
uuid = require('uuid/v4');
1311

1412
let featureTablePool;
@@ -80,27 +78,14 @@ function getByUserId(userId, callback) {
8078
}
8179

8280
function init(callback) {
83-
if (!process.env.FEATURES_CONNECTION_STRING)
84-
return callback(new ServiceError(HttpStatus.INTERNAL_SERVER_ERROR, "FEATURES_CONNECTION_STRING configuration not provided as environment variable"));
81+
postgres.init(function(err, pool) {
82+
if (err) {
83+
return callback(err);
84+
}
8585

86-
// POSTGRES CONNECTION CODE
87-
88-
log.info('connecting to features database using: ' + process.env.FEATURES_CONNECTION_STRING);
89-
90-
const params = url.parse(process.env.FEATURES_CONNECTION_STRING);
91-
const auth = params.auth.split(':');
92-
93-
const config = {
94-
user: auth[0],
95-
password: auth[1],
96-
host: params.hostname,
97-
port: params.port,
98-
database: params.pathname.split('/')[1]
99-
};
100-
101-
featureTablePool = new pg.Pool(config);
102-
103-
return callback();
86+
featureTablePool = pool;
87+
return callback();
88+
});
10489
}
10590

10691
function put(visits, callback) {

0 commit comments

Comments
 (0)