forked from formio/formio
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
387 lines (314 loc) · 12.1 KB
/
index.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
'use strict';
// Setup the Form.IO server.//
const express = require('express');
const cors = require('cors');
const router = express.Router();
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const bodyParser = require('body-parser');
const _ = require('lodash');
const events = require('events');
const Q = require('q');
const nunjucks = require('nunjucks');
const util = require('./src/util/util');
const log = require('debug')('formio:log');
const gc = require('expose-gc/function');
const formList = require('./src/resources/formList');
const logger = require('./src/util/logger')('formio:log');
const originalGetToken = util.Formio.getToken;
const originalEvalContext = util.Formio.Components.components.component.prototype.evalContext;
// Keep track of the formio interface.
router.formio = {};
// Allow libraries to use a single instance of mongoose.
router.formio.mongoose = mongoose;
// Use custom template delimiters.
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
// Allow custom configurations passed to the Form.IO server.
module.exports = function(config) {
// Give app a reference to our config.
router.formio.config = config;
// Add the middleware.
router.formio.middleware = require('./src/middleware/middleware')(router);
// Configure nunjucks to not watch any files
nunjucks.configure([], {
watch: false
});
// Allow events to be triggered.
router.formio.events = new events.EventEmitter();
router.formio.config.schema = require('./package.json').schema;
router.formio.log = (event, req, ...info) => {
const result = router.formio.hook.alter('log', event, req, ...info);
logger.info(event, ...info);
if (result) {
log(event, ...info);
}
};
router.formio.audit = (event, req, ...info) => {
if (config.audit) {
const result = router.formio.hook.alter('audit', info, event, req);
if (result) {
console.log(...result);
}
}
};
/**
* Initialize the formio server.
*/
router.init = function(hooks) {
const deferred = Q.defer();
// Hooks system during boot.
router.formio.hooks = hooks;
// Add the utils to the formio object.
router.formio.util = util;
// Get the hook system.
router.formio.hook = require('./src/util/hook')(router.formio);
router.formio.hook.alter('configFormio', {Formio: util.Formio});
// Get the encryption system.
router.formio.encrypt = require('./src/util/encrypt');
// Load the updates and attach them to the router.
router.formio.update = require('./src/db/index')(router.formio);
// Run the healthCheck sanity check on /health
/* eslint-disable max-statements */
router.formio.update.initialize(function(err, db) {
// If an error occurred, then reject the initialization.
if (err) {
return deferred.reject(err);
}
util.log('Initializing API Server.');
// Add the database connection to the router.
router.formio.db = db;
// Ensure we do not have memory leaks in core renderer
router.use((req, res, next) => {
util.Formio.forms = {};
util.Formio.cache = {};
util.Formio.Components.components.component.Validator.config = {
db: null,
token: null,
form: null,
submission: null
};
util.Formio.getToken = originalGetToken;
util.Formio.Components.components.component.prototype.evalContext = originalEvalContext;
try {
if (config.maxOldSpace) {
const heap = process.memoryUsage().heapTotal / 1024 / 1024;
if ((config.maxOldSpace * 0.8) < heap) {
gc();
}
}
}
catch (error) {
logger.error(error);
console.log(error);
}
next();
});
// Establish our url alias middleware.
if (!router.formio.hook.invoke('init', 'alias', router.formio)) {
router.use(router.formio.middleware.alias);
}
// Establish the parameters.
if (!router.formio.hook.invoke('init', 'params', router.formio)) {
router.use(router.formio.middleware.params);
}
// Add the db schema sanity check to each request.
router.use(router.formio.update.sanityCheck);
// Add Middleware necessary for REST API's
router.use(bodyParser.urlencoded({extended: true}));
router.use(bodyParser.json({
limit: '16mb'
}));
router.get("/checkpoint",(req,res)=>{
router.formio.resources.role.model.find().limit(5).then((data)=>{
if (data.length) {
return res.json({message:"formsflow-forms is ready"});
}
res.status(500);
res.json({message:"formsflow-forms is down"});
});
});
// getting form list
router.get("/form",router.formio.middleware.tokenVerify,(req,res)=>{
try {
formList(req,res,router);
}
catch (err) {
console.log(err);
}
});
// Error handler for malformed JSON
router.use((err, req, res, next) => {
if (err instanceof SyntaxError) {
res.status(400).send(err.message);
}
next();
});
// CORS Support
const corsRoute = cors(router.formio.hook.alter('cors'));
router.use((req, res, next) => {
if (req.url === '/') {
return next();
}
if (res.headersSent) {
return next();
}
corsRoute(req, res, next);
});
// Import our authentication models.
router.formio.auth = require('./src/authentication/index')(router);
// Perform token mutation before all requests.
if (!router.formio.hook.invoke('init', 'token', router.formio)) {
router.use(router.formio.middleware.tokenHandler);
}
// The get token handler
if (!router.formio.hook.invoke('init', 'getTempToken', router.formio)) {
router.get('/token', router.formio.auth.tempToken);
}
// The current user handler.
if (!router.formio.hook.invoke('init', 'logout', router.formio)) {
router.get('/logout', router.formio.auth.logout);
}
// The current user handler.
if (!router.formio.hook.invoke('init', 'current', router.formio)) {
router.get('/current', router.formio.hook.alter('currentUser', [router.formio.auth.currentUser]));
}
// The access handler.
if (!router.formio.hook.invoke('init', 'access', router.formio)) {
router.get('/access', router.formio.middleware.tokenVerify,router.formio.middleware.accessHandler);
}
// The public config handler.
if (!router.formio.hook.invoke('init', 'config', router.formio)) {
router.use('/config.json', router.formio.middleware.configHandler);
}
// Authorize all urls based on roles and permissions.
if (!router.formio.hook.invoke('init', 'perms', router.formio)) {
router.use(router.formio.middleware.permissionHandler);
}
let mongoUrl = config.mongo;
let mongoConfig = config.mongoConfig ? JSON.parse(config.mongoConfig) : {};
if (!mongoConfig.hasOwnProperty('connectTimeoutMS')) {
mongoConfig.connectTimeoutMS = 300000;
}
if (!mongoConfig.hasOwnProperty('socketTimeoutMS')) {
mongoConfig.socketTimeoutMS = 300000;
}
if (!mongoConfig.hasOwnProperty('useNewUrlParser')) {
mongoConfig.useNewUrlParser = true;
}
if (!mongoConfig.hasOwnProperty('keepAlive')) {
mongoConfig.keepAlive = true;
}
if (_.isArray(config.mongo)) {
mongoUrl = config.mongo.join(',');
}
if (config.mongoSA || config.mongoCA) {
mongoConfig.sslValidate = true;
mongoConfig.sslCA = config.mongoSA || config.mongoCA;
}
mongoConfig.useUnifiedTopology = true;
if (config.mongoSSL) {
mongoConfig = {
...mongoConfig,
...config.mongoSSL,
};
}
// Connect to MongoDB.
mongoose.connect(mongoUrl, mongoConfig );
// Trigger when the connection is made.
mongoose.connection.on('error', function(err) {
util.log(err.message);
deferred.reject(err.message);
});
// Called when the connection is made.
mongoose.connection.once('open', function() {
util.log(' > Mongo connection established.');
// Load the BaseModel.
router.formio.BaseModel = require('./src/models/BaseModel');
// Load the plugins.
router.formio.plugins = require('./src/plugins/plugins');
router.formio.schemas = {
PermissionSchema: require('./src/models/PermissionSchema')(router.formio),
AccessSchema: require('./src/models/AccessSchema')(router.formio),
FieldMatchAccessPermissionSchema: require('./src/models/FieldMatchAccessPermissionSchema')(router.formio),
};
// Get the models for our project.
const models = require('./src/models/models')(router);
// Load the Schemas.
router.formio.schemas = _.assign(router.formio.schemas, models.schemas);
// Load the Models.
router.formio.models = models.models;
// Load the Resources.
router.formio.resources = require('./src/resources/resources')(router);
// Load the request cache
router.formio.cache = require('./src/cache/cache')(router);
// return the form metadata
const metadataResource = require('./src/resources/formMetadata');
router.get('/form/:formId/metadata',metadataResource.getFormMetadata(router));
// Return the form components.
router.get('/form/:formId/components', function(req, res, next) {
router.formio.resources.form.model.findOne({_id: req.params.formId}, function(err, form) {
if (err) {
return next(err);
}
if (!form) {
return res.status(404).send('Form not found');
}
// If query params present, filter components that match params
const filter = Object.keys(req.query).length !== 0 ? _.omit(req.query, ['limit', 'skip']) : null;
res.json(
_(util.flattenComponents(form.components))
.filter(function(component) {
if (!filter) {
return true;
}
return _.reduce(filter, function(prev, value, prop) {
if (!value) {
return prev && _.has(component, prop);
}
const actualValue = _.property(prop)(component);
// loose equality so number values can match
return prev && actualValue == value || // eslint-disable-line eqeqeq
value === 'true' && actualValue === true ||
value === 'false' && actualValue === false;
}, true);
})
.values()
.value()
);
});
});
// Import the form actions.
router.formio.Action = router.formio.models.action;
router.formio.actions = require('./src/actions/actions')(router);
// Add submission data export capabilities.
require('./src/export/export')(router);
// Add the available templates.
router.formio.templates = {
default: _.cloneDeep(require('./src/templates/default.json'))
};
// Add the template functions.
router.formio.template = require('./src/templates/index')(router);
const swagger = require('./src/util/swagger');
// Show the swagger for the whole site.
router.get('/spec.json', function(req, res, next) {
swagger(req, router, function(spec) {
res.json(spec);
});
});
// Show the swagger for specific forms.
router.get('/form/:formId/spec.json', function(req, res, next) {
swagger(req, router, function(spec) {
res.json(spec);
});
});
require('./src/middleware/recaptcha')(router);
// Say we are done.
deferred.resolve(router.formio);
});
});
/* eslint-enable max-statements */
return deferred.promise;
};
// Return the router.
return router;
};