Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (65 loc) · 2.07 KB
/
Copy pathapp.js
File metadata and controls
78 lines (65 loc) · 2.07 KB
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
/*
* Copyright (C) 2016 TopCoder Inc., All Rights Reserved.
*/
'use strict';
/**
* The application entry point
*
* @author TSCCODER
* @version 1.0
*/
require('./bootstrap');
const config = require('config');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const _ = require('lodash');
const helper = require('./common/helper');
const errorMiddleware = require('./common/ErrorMiddleware');
const winston = require('winston');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.set('port', config.PORT);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cors());
const apiRouter = express.Router();
// include io instance
apiRouter.use((req, res, next) => {
res.io = io;
next();
});
io.on('connection', (socket) => { // eslint-disable-line no-unused-vars
winston.info('socket connection established');
});
// load all routes
_.each(require('./routes'), (verbs, url) => {
_.each(verbs, (def, verb) => {
let actions = [
function (req, res, next) {
req.signature = def.controller + '#' + def.method;
next();
},
];
const method = require('./controllers/' + def.controller)[def.method];
if (!method) {
throw new Error(def.method + ' is undefined, for controller ' + def.controller);
}
if (def.middleware && def.middleware.length > 0) {
actions = actions.concat(def.middleware);
}
actions.push(method);
winston.info(`register ${verb} /api/v${config.API_VERSION}${url}`);
apiRouter[verb](`/api/v${config.API_VERSION}${url}`, helper.autoWrapExpress(actions));
});
});
app.use('/', apiRouter);
app.use(errorMiddleware());
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
http.listen(app.get('port'), () => {
winston.info(`Express server listening on port ${app.get('port')}`);
});
module.exports = app;