-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-init-service.js
233 lines (214 loc) · 9.8 KB
/
app-init-service.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// requires
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var morgan = require('morgan');
var mongodb = require('mongodb');
var monk = require('monk');
var log4js = require('log4js');
var memAppender = require('log4js-memory-appender');
var yargs = require('yargs');
var util = require('util');
var str = require('string');
var loggerWrapper = require('log4js-function-designation-wrapper');
// modules
var appConstants = require('./app-constants');
var currencyExchangeJsonService = require('./currency-exchange-json-service');
// variables
var logger = null;
// functions
/********************************************************
* Builds the datastore connection string based on
* configuration values.
********************************************************/
var buildConnectionString = function buildConnectionString(applicationConfig){
var connectionString = applicationConfig.mongodbURL;
if(applicationConfig.mongodbName){
if(!str(connectionString).endsWith('/')) { connectionString += '/'; }
connectionString += applicationConfig.mongodbName;
}
return(connectionString);
};
/********************************************************
* Initializes the MongoDB datastore.
********************************************************/
var initDatastore = function initDatastore(applicationConfig)
{
var datastore = null;
// build the connection string
var connectionString = buildConnectionString(applicationConfig);
// console this out - as sensitive info present
console.log(util.format("Using connection string '%s' for mongodb.", connectionString));
datastore = monk(connectionString);
// add functions to help manage collections
datastore.getPullsCollection = function(){ return(this.get(applicationConfig.mongodbPullsCollectionName)); };
datastore.getEventsCollection = function(){ return(this.get(applicationConfig.mongodbEventsCollectionName)); };
datastore.getNotificationsCollection = function(){ return(this.get(applicationConfig.mongodbNotificationsCollectionName)); };
logger.info("Datastore initialised.");
return(datastore);
};
/********************************************************
* Initializes logging.
********************************************************/
var initLog4js = function initLog4js(applicationConfig, buffer)
{
var log4jsLogger = null;
// add the current working directory for support in openshift env
log4js.configure(applicationConfig.log4js, { cwd : applicationConfig.logsDir });
log4js.loadAppender('memory', memAppender({ buffer : buffer, maxBufferSize : applicationConfig.log4js.memoryAppender.bufferSize }));
log4js.addAppender(log4js.appenders.memory(null, applicationConfig.log4js.memoryAppender.timezoneOffset));
log4jsLogger = log4js.getLogger(appConstants.APP_NAME);
// assign as global logger
global.logger = log4jsLogger;
// init local logger
logger = loggerWrapper(global.logger, 'app-init-service');
// create local logger wrapper
logger.info("Log4js initialised.");
};
/********************************************************
* Initializes Yargs for command line argument processing.
********************************************************/
var initYargs = function initYargs(){
var argv = yargs
.usage('Usage: $0 --smtpHost [string] --smtpUser [string] --smtpPassword [string] --notifyAddresses [array]')
.example('$0 -smtpHost smtp.host.com --smptpUser username --smtpPassword password --notifyAddresses [email protected] [email protected]')
.describe({
'smtpHost' : 'SMTP host for sending notification emails',
'smtpUser' : 'username for SMTP access',
'smtpPassword' : 'password for SMTP access',
'notifyAddresses' : 'array of receipient addressess for notification emails'
})
.array('notifyAddresses')
.string(['smtpHost', 'smtpUser', 'smtpPassword'])
.demand(['smtpHost', 'smtpUser', 'smtpPassword', 'notifyAddresses'])
.argv;
return(argv);
};
/********************************************************
* Processes command line arguments.
********************************************************/
var processArguments = function processArguments(applicationConfig){
logger.info(util.format("Using '%s' configuration.", applicationConfig.environment));
// check if deployed locally or not
if(applicationConfig.environment === appConstants.ENVIRONMENT_LOCAL_NAME)
{
// proces the arguments using yargs
var argv = initYargs();
// add the information from arguments in to the config
logger.info("Overriding smtp configuration with command line arguments.");
applicationConfig.argumentSmtpHost = argv.smtpHost;
applicationConfig.argumentSmtpUser = argv.smtpUser;
applicationConfig.argumentSmtpPassword = argv.smtpPassword;
applicationConfig.argumentNotifyAddresses = argv.notifyAddresses;
}
};
/********************************************************
* Outputs configuration information to the console. It
* is not output to the logs as this is sensitive information
* that should not appear on the diagnostics page.
********************************************************/
var outputConfigToConsole = function outputConfigToConsole(applicationConfig){
console.log("Configuration ->");
console.log(applicationConfig);
};
/********************************************************
* Outputs environment information to the console. It
* is not output to the logs as this is sensitive information
* that should not appear on the diagnostics page.
********************************************************/
var outputEnvToConsole = function outputEnvToConsole(process){
console.log("Environment ->");
console.log(process.env);
};
/********************************************************
* Initializes the express application.
********************************************************/
var initExpress = function initExpress(app, routes, __dirname, applicationConfig){
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bootstrap', express.static(path.join(__dirname, 'node_modules/bootstrap/dist')));
app.use('/jquery', express.static(path.join(__dirname, 'node_modules/jquery/dist')));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// error handlers
// development and test error handler
// will print stacktrace
if (app.get('env') === appConstants.NODE_ENVIRONMENT_DEVELOPMENT_NAME || app.get('env') === appConstants.NODE_ENVIRONMENT_TEST_NAME) {
app.use(function(err, req, res, next) {
if(logger.valid()) {
logger.error({ designation: "express", functionDesignation: "middleware-error-handler", arguments: ["Express middleware error handler caught an error.", err, util.format("Original URL: '%s'.", req.originalUrl)] });
}
if(req.xhr){
res.status(err.status || 500).send({ message : err.message, error : err });
} else {
res.status(err.status || 500);
res.render('error', {
title: 'Error',
message: err.message,
error: err
});
}
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
if(logger.valid()) {
logger.error({ designation: "express", functionDesignation: "middleware-error-handler", arguments: ["Express middleware error handler caught an error.", err, util.format("Original URL: '%s'.", req.originalUrl)] });
}
if(req.xhr){
res.status(err.status || 500).send({ message : err.message, error: {} });
} else {
res.status(err.status || 500);
res.render('error', {
title: 'Error',
message: err.message,
error: {}
});
}
});
// conditionally beautify output
app.locals.pretty = applicationConfig.useExpressPrettyOutput;
logger.info("Express initialised.");
};
/********************************************************
* Initializes the express locals for use in routes and
* views.
********************************************************/
var initExpressLocals = function initExpressLocals(app, locals){
app.locals.context = locals;
logger.info("Express locals initialised.");
};
/********************************************************
* Initializes the bank product JSON service.
********************************************************/
var initCurrenctExchangeJsonService = function initCurrenctExchangeJsonService(applicationConfig, datastore){
currencyExchangeJsonService.configure(applicationConfig, datastore);
logger.info("Currency Exchange JSON Service initialised.");
return(currencyExchangeJsonService);
}
module.exports = {
initDatastore : initDatastore,
initLog4js : initLog4js,
processArguments : processArguments,
outputConfigToConsole : outputConfigToConsole,
outputEnvToConsole : outputEnvToConsole,
initExpress : initExpress,
initExpressLocals : initExpressLocals,
initCurrenctExchangeJsonService : initCurrenctExchangeJsonService
};