-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
104 lines (101 loc) · 3.71 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var uuid = require('node-uuid');
var cookie = require('cookie');
var cookieParser = require('restify-cookies');
var nconf = require('nconf');
global._ROOT = process.env.PWD;
nconf.argv()
.env()
.file({file: global._ROOT + '/config/config.json'});
var nconfENV = nconf.get('NODE_ENV') || 'dev';
global.DB_CONFIG = nconf.get(nconfENV + ":db");
checkIfRequiredTablesExists();
var restify = require('restify');
var epsOptions = require('./controllers/eps_options/options');
var reward = require('./controllers/eps_options/reward');
var epsTest = require('./controllers/eps_options/eps_test');
var logger = require('./log/logger');
var server = restify.createServer({
name: 'epsilon-ab-service',
log: logger,
versions: ['1.0.0']
});
server.use(restify.bodyParser());
server.use(restify.queryParser());
server.use(cookieParser.parse);
server.use(restify.requestLogger());
server.use(function (req, res, next) {
req.connection.setTimeout(10000);
next();
});
//Delete any timers added to logger
server.use(function (req, res, next) {
res.on("header", function () {
delete logger._times[req.log.fields.req_id];
});
next();
});
//Set unique id for each request, if the request have a cookie by name unique_id
//use it else generate a unique request id
server.use(function (req, res, next) {
var uniqueId = uuid.v1();
if (req.headers.cookie) {
var cookies = cookie.parse(req.headers.cookie);
uniqueId = cookies.unique_id || uniqueId;
}
req.req_id = uniqueId || req.req_id;
res.setHeader('X-Request-Id', req.req_id);
next();
});
//Accept only json content types
server.pre(function (req, res, next) {
res.setHeader('content-type', 'application/json');
if (!req.is('application/json')) {
res.send(400, {"ERROR": 'Invalid content-type'});
return;
}
if (!req.accepts('application/json')) {
res.send(400, {"ERROR": 'Invalid accept-type'});
return;
}
next();
});
var port = process.env.PORT || '5000';
server.use(function (req, res, next) {
res.on("header", function () {
delete logger._times[req.log.fields.req_id];
});
next();
});
server.get('/epsOption', epsOptions.get);
server.put('/epsTest/reward', reward.put);
server.put('/epsTest/conversion', epsTest.userConverted);
server.post('/epsTest', epsTest.create);
server.get('/epsTest/:id', epsTest.findById);
server.get('/epsTest', epsTest.findAll);
server.post('/epsTest/:id/options/:optionNumber', epsTest.update);
server.get('/epsTest/:name/stats', epsTest.stats);
server.listen(port, function () {
logger.info('epsilon ab server listening on port ' + port);
});
function checkIfRequiredTablesExists() {
if (global.DB_CONFIG['default']['name'] === 'mysql') {
var mysql = require('mysql');
global._MYSQL_CONNECTION_POOL = mysql.createPool(global.DB_CONFIG['default']);
var mysqlClient = require(global._ROOT + '/db/pgsql/mysql_client');
new Promise(function (resolve, reject) {
mysqlClient.haveAllRequiredTables(global._MYSQL_CONNECTION_POOL, reject, resolve);
}).then(function (res) {
if (res['tableCount'] != 3) {
console.log("**ATTENTION SOME TABLES ARE MISSING IN THE SCHEMA. CREATE THE TABLES SPECIFIED UNDER schema/mysql/**");
}
});
} else if (global.DB_CONFIG['default']['name'] === 'pgsql') {
var pgClient = require(global._ROOT + '/db/pgsql/pgsql');
pgClient.haveAllRequiredTables()
.then(function (rows) {
if (!rows || rows.length != 3) {
console.log("**ATTENTION SOME TABLES ARE MISSING IN THE SCHEMA. CREATE THE TABLES SPECIFIED UNDER schema/pgsql/**");
}
})
}
}