-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
313 lines (237 loc) · 7.03 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* eslint-disable semi */
'use strict';
const express = require("express");
const http = require("http");
const socket = require('socket.io');
const swaggerTools = require('swagger-tools');
const path = require('path');
const yaml = require('js-yaml');
const fs = require('fs');
const paginate = require("./lib/mongopage");
const helmet = require('helmet');
const compression = require('compression');
const bodyParser = require('body-parser');
const moment = require('moment');
const debug = require('debug')('restfulmodel:app');
const mongojs = require('mongojs');
const Users = require('./lib/Users');
/**
* Build the main application
* @param {object} config - The module config
* @returns {Promise}
* @author César Casas
*/
function app(config) {
var self = this;
debug("init....");
self.main = {
config: config,
db: mongojs(config.get('db.host'), config.get('db.collections')),
restEndpoint: config.get('service.protocol') + config.get('service.host') + config.get('service.pathname'),
sockets: {}
};
/*
*Inject paginate method for all collections
*/
paginate(self.main.db);
return new Promise((resolve, reject)=> {
self.swaggerDoc()
.then(()=> {
return self.getApp();
})
.then(()=> {
return self.io();
})
.then(()=> {
return self.announce();
})
.then(()=> {
return self.libs();
})
.then(()=> {
return self.controllers();
})
.then(()=> {
return self.routers();
})
.then(()=> {
debug("Setup finish, run...");
resolve(self.main);
})
.catch((err)=> {
console.log("Error init: ", err);
});
});
}
/**
* inject swagger doc into main object.
* @returns {Promise}
*/
app.prototype.swaggerDoc = function () {
var self = this;
debug("running swaggerDoc");
return new Promise((resolve, reject)=> {
var swaggerFile = path.join(__dirname, '/api/swagger/swagger.yaml');
var swaggerString = fs.readFileSync(swaggerFile, 'utf8');
var swaggerDoc = yaml.safeLoad(swaggerString);
swaggerDoc.host = self.main.config.get('service.host');
swaggerDoc.basePath = self.main.config.get('service.pathname');
self.main.swaggerDoc = swaggerDoc;
resolve({swaggerDoc: swaggerDoc});
});
}
/**
* Create the express instance an inject into main property the instance and server (http)
* @returns {Promise}
*/
app.prototype.getApp = function () {
var self = this;
debug("getApp...");
return new Promise((resolve, reject)=> {
self.main.app = express();
/**
* Sessions
*/
self.main.app.set('trust proxy', 1);
self.main.app.use(helmet());
self.main.app.use(compression());
self.main.server = http.createServer(self.main.app);
resolve({app: self.main.app, server: self.main.server});
});
}
/**
* create socket.io instance and inject into main object
* @returns {Promise}
*/
app.prototype.io = function () {
var self = this;
debug("io...");
return new Promise((resolve, reject)=> {
let pathName = self.main.config.get('service.pathname');
debug(pathName + '/socket.io');
self.main.io = socket.listen(self.main.server);
let io = self.main.io;
io.on('connection', (socket)=> {
debug("Socket.io connected: " + socket.id);
self.main.sockets[socket.id] = socket;
self.main.sockets[socket.created] = new Date();
socket.on('disconnect', ()=> {
delete self.main.sockets[socket.id];
});
});
resolve({io: self.main.io});
});
}
/**
* create redisClient or sentinel instance, use own redis lib (return sentinel instance or redisClient). Inject the instance into main object.
* @returns {Promise}
*/
/**
* create redisClient or sentinel instance, use own redis lib (return sentinel instance or redisClient). Inject the instance into main object.
* @returns {Promise}
*/
/*
app.prototype.redisClient = function () {
var self = this;
debug("redisClient");
let redisConf = self.main.config.get('redis');
return new Promise((resolve, reject)=> {
if (redisConf.hosts.length == 1) self.main.redis = redis.createClient(redisConf.hosts);
else self.main.redisClient = redis.createClient(self.main.config.get('redis').hosts, self.main.config.get('redis').mastername);
resolve({redisClient: self.main.redisClient});
});
}*/
/**
* Socket.io emit message. Using into controllers/index.js for wrapHandler
* @returns {Promise}
*/
app.prototype.announce = function () {
var self = this;
debug("announce...");
return new Promise((resolve, reject)=> {
self.main.announce = function () {
var args = Array.prototype.slice.apply(arguments);
self.main.io.sockets.emit.apply(self.main.io.sockets, args);
};
resolve({announce: self.main.announce});
});
}
/**
* Create the common lib instances for all REST Application
* @returns {Promise}
*/
app.prototype.libs = function () {
var self = this;
return new Promise((resolve, reject)=> {
self.main.libs = {};
self.main.libs.http = http;
self.main.libs.moment = moment;
self.main.libs.users = new Users(self.main);
resolve(self.main.libs);
});
}
app.prototype.controllers = function () {
var self = this;
var controllers = {};
debug("controllers...");
return new Promise((resolve, reject)=> {
self.main.controllers = require('./controllers')(self.main);
resolve(self.main.controllers);
});
}
app.prototype.routers = function () {
var self = this;
debug("routers...");
return new Promise((resolve, reject)=> {
var app = self.main.app;
var options = {
controllers: self.main.controllers
};
app.set('basePath', self.main.swaggerDoc.basePath);
var formatValidationError = function formatValidationError(err, req, res, next) {
var error = {
code: 'validation_error',
message: err.message,
details: err.results ? err.results.errors : null
};
res.json({error: error});
};
function initMiddleWare(middleware, callback) {
debug('initializating middleware');
app.use((req, res, next)=> {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.setHeader('Access-Control-Allow-Credentials', true);
if (req.method === 'OPTIONS') return res.end();
if (req.headers && req.headers['x-forwarded-for']) {
let parts = req.headers['x-forwarded-for'].split(",");
req.realip = parts[0];
} else {
req.realip = req.ip;
}
next();
});
app.use(middleware.swaggerMetadata());
app.use(middleware.swaggerValidator(), formatValidationError);
app.use(middleware.swaggerRouter(options));
app.use((err, req, res, next) => {
res.status(500);
res.send(err);
res.end();
});
app.use(middleware.swaggerUi({
apiDocs: self.main.config.get('service.pathname') + '/api-docs',
swaggerUi: self.main.config.get('service.pathname') + '/docs'
}));
app.use(express.static('public'));
callback();
}
swaggerTools.initializeMiddleware(self.main.swaggerDoc, (swaggerMiddleware) => {
initMiddleWare(swaggerMiddleware, (err) => {
resolve();
});
});
});
}
module.exports = app;