-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
273 lines (231 loc) · 8.42 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
// get environment variables
require('dotenv').config();
// boostrap utility functions
require('./lib/util');
// import dependencies
const express = require('express');
const app = express();
const session = require('express-session'); //secure cookies
const compression = require('compression');
const debug = require('debug')('default');
const path = require('path');
const morgan = require('morgan')
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const helmet = require('helmet');
const actuator = require('express-actuator');
const schedule = require('node-schedule');
const Archive = require('./models/common/Archive');
const System = require('./models/common/System');
const MQService = require('./models/mq/MQService');
const constants = require('./lib/util/constants');
const fuseHelper = require('./lib/util/fuse');
const reportingRouter = require('./routes/report');
global.__basedir = __dirname;
// connect to database
const db = require('./models/db');
db.on('error', function(err) { throw err; });
db.once('open', function() {
debug(`Successfully connected to Mongo (${process.env.MONGODB_HOST})`);
// ready to start
app.emit('ready');
});
app.on('ready', init);
function init() {
// register middleware
app.use(helmet());
app.use(compression());
app.use(morgan('dev'));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// parse request body as plaintext if no content-type is set
app.use(function(req, res, next) {
if (!req.get('content-type')) {
req.headers['content-type'] = 'text/plain';
}
return next();
});
//secure cookies
app.use(session({
cookieName: 'mockiatoSession',
secret: process.env.MOCKIATO_SECRET,
resave: false,
saveUninitialized: true,
httpOnly: true, // dont let browser javascript access cookie ever
secure: true, // only use cookie over https
ephemeral: true // delete this cookie while browser close
}));
// parse request body based on content-type
app.use(bodyParser.text({ limit: '5mb', type: [ 'application/x-www-form-urlencoded', 'application/soap+xml', 'application/xml', 'text/xml', 'text/plain' ]}));
app.use(bodyParser.json({ limit: '5mb', type: [ 'application/json' ]}));
//app.use(bodyParser.urlencoded({ extended: false }));
// expose health info
app.use(actuator('/api/admin'));
const ProtectedRoutes = express.Router();
app.use('/api-docs', ProtectedRoutes);
var jwt2 = require('express-jwt');
// expose swagger ui for internal api docs
const YAML = require('yamljs');
const apiDocs = YAML.load('./api-docs.yml');
const swaggerUI = require('swagger-ui-express');
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(apiDocs), jwt2({ secret: process.env.MOCKIATO_SECRET }),
function (req, res) {
if (!req.user.admin) return res.sendStatus(401);
res.sendStatus(200);
});
// configure auth
const passport = require('passport');
const passportOpts = { session: false };
const authType = process.env.MOCKIATO_AUTH || 'local';
// setup local authentication
if (authType === 'local') {
debug('Using local auth strategy');
const local = require('./lib/auth/local');
app.use('/register', local);
}
// setup ldap authentication
else if (authType === 'ldapauth') {
debug('Using LDAP auth strategy');
require('./lib/auth/ldap');
}
// route to retrieve login token
const jwt = require('jsonwebtoken');
const secret = process.env.MOCKIATO_SECRET;
app.set('secret', secret);
app.post('/api/login', passport.authenticate(authType, passportOpts),
function(req, res) {
const user = {
uid: req.user.uid,
mail: req.user.mail
};
if (authType === 'ldapauth') {
// create new user on first login
user.uid = req.user.sAMAccountName;
mongoose.model('User').findOne({uid: user.uid}, function(err, foundUser) {
if (err) {
debug(err);
return;
}
if (!foundUser) {
mongoose.model('User').create(user,
function(err, newUser) {
if (err) {
debug(err);
return;
}
});
}
});
}
// create access token
const token = jwt.sign(user, app.get('secret'), {
expiresIn: '1d'
});
res.json({
success: true,
token: token
});
});
//When a Ldap user try to register externally. Show message.
app.get('/register', function (req, res) {
const htmlView = constants.ORG_USR_REGISTER_VIEW;
res.set('Content-Type', 'text/html');
res.send(htmlView);
});
// expose API and virtual SOAP / REST services
const virtual = require('./routes/virtual');
const api = require('./routes/services');
const invoke = require('./routes/invoke');
const restclient = require('./routes/restClient');
// register SOAP / REST virts from DB
virtual.registerAllRRPairsForAllServices();
app.use('/api/services', api);
app.use('/virtual', virtual.router);
app.use('/virtual', invoke.router);
app.use('/restClient', restclient);
// initialize recording routers
const recorder = require('./routes/recording');
const recorderController = require('./controllers/recorderController');
app.use('/recording',recorder.recordingRouter);
app.use('/api/recording',recorder.apiRouter);
app.use('/api/report/',reportingRouter.router);
const mqController = require('./controllers/mqController');
mqController.registerAllMQServices();
// register new virts on all threads
if (process.env.MOCKIATO_MODE !== 'single') {
process.on('message', function(message) {
const msg = message.data;
if(msg.service){
const service = msg.service;
const action = msg.action;
debug(action);
if (service.type !== 'MQ') {
virtual.deregisterService(service);
invoke.deregisterServiceInvoke(service);
if (action === 'register') {
virtual.registerService(service);
if(service.liveInvocation && service.liveInvocation.enabled){
invoke.registerServiceInvoke(service);
}
}
}
else {
mqController.deregisterMQService(service);
if (action === 'register') {
mqController.registerMQService(service);
}
}
}else if(msg.recorder){
const rec = msg.recorder;
const action = msg.action;
if(action === 'register'){
recorderController.registerRecorder(rec);
}else if(action === 'deregister'){
recorderController.deregisterRecorder(rec);
}
}
});
}
// running a cronjob only in master cluster (single instance)
/* if (process.env.MOCKIATO_MODE !== 'single' && process.env.NODE_APP_INSTANCE === '0') {
let priorDate_30days = new Date(new Date().setDate(new Date().getDate() - 30));
let rule = process.env.MOCKIATO_ARCHIVE;
schedule.scheduleJob(rule, function(){
console.log('******cron job run for cleaning Archive collection by only master cluster (single process)****** Node Instance Id :- --'+ process.env.NODE_APP_INSTANCE);
const query = { 'createdAt': { $lt: priorDate_30days } };
Archive.find(query, function(error, services) {
if (error) debug(error);
Archive.remove(query, function(error, removed) {
if (error) debug(error);
});
console.log('No. of Documents deleted from Archive:- '+services.length);
});
});
} */
// expose api methods for users and groups
const systems = require('./routes/systems');
app.use('/api/systems', systems);
const users = require('./routes/users');
app.use('/api/users', users);
const mqinfo = require('./routes/mqInfo');
app.use('/api/mqinfo', mqinfo);
// handle no match responses
app.use(/\/((?!recording).)*/,function(req, res, next) {
if (!req.msgContainer) {
req.msgContainer = {};
req.msgContainer.reqMatched = false;
req.msgContainer.reason = `Path ${req.path} could not be found`;
}
return res.status(404).json(req.msgContainer);
});
// handle internal errors
app.use(/\/((?!recording).)*/,function(err, req, res) {
debug(err.message);
return res.status(500).send(err.message);
});
// ready for testing (see test/test.js)
fuseHelper.fuseAllFiles();
app.emit('started');
}
module.exports = app;