-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
240 lines (173 loc) · 5.87 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
'use strict';
const http = require('http');
const https = require('https');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const fs = require('fs');
const cors = require('cors');
const multer = require('multer');
const cookieParser = require('cookie-parser');
const expressSession = require('express-session');
const passport = require('passport');
const flash = require('connect-flash');
const config = require('./config/config');
const serviceLoader = require('./loader/service_loader');
const controllerLoader = require('./loader/controller_loader');
const local_login = require('./passport/local_login');
// logger
const logger = require('./util/logger');
//===== Socket.IO =====//
const socketio = require('socket.io');
//=====================//
const sessionMiddleware = expressSession({
secret: 'my key',
resave: true,
saveUninitialized: true
});
// load external_loader
const external_loader = require('./loader/external_loader');
// Declaration of createApp function
const createApp = () => {
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//console.log('View engine is set to ejs.');
app.use(cors());
app.use('/', express.static(path.join(__dirname, 'public')));
//app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.urlencoded({
parameterLimit: 50000000,
limit: '50mb',
extended:true
}));
app.use(bodyParser.json({
parameterLimit: 50000000,
limit: '50mb',
extended:true
}));
const upload = initUpload();
app.use(cookieParser());
app.use(sessionMiddleware);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
passport.use('local-login', local_login);
const router = express.Router();
app.use('/', router);
// load registered services
serviceLoader.load();
logger.info('service loader called.');
// load registered controllers
controllerLoader.load(router, upload);
logger.info('controller loader called.');
// load external interface
external_loader.init(app, config);
initSwagger(app);
return app;
};
// Declaration of initUpload function
const initUpload = () => {
const storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, 'uploads');
},
filename: function(req, file, callback) {
const extension = path.extname(file.originalname);
const basename = path.basename(file.originalname, extension);
callback(null, basename + Date.now() + extension);
}
});
const upload = multer({
storage: storage,
limits: {
files: 10,
fileSize: 1024 * 1024 * 1024
}
});
return upload;
};
const initSwagger = (app) => {
const expressSwagger = require('express-swagger-generator')(app);
let options = {
swaggerDefinition: {
info: {
description: 'This is a boilerplate server',
title: 'UbiAccess',
version: '1.0.0',
},
host: 'localhost:7001',
basePath: '/',
produces: [
"application/json",
"application/xml"
],
schemes: ['http', 'https'],
securityDefinitions: {
JWT: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
description: "",
}
}
},
basedir: __dirname, //app absolute path
files: ['./controllers/**/*.js'] //Path to the API handle folder
};
expressSwagger(options);
}
var options = {
key: fs.readFileSync('private.pem'),
cert: fs.readFileSync('cert.pem'),
passphrase: '123456'
};
let namespace;
// Declaration of main function
const main = () => {
const app = createApp();
let server;
if (config.server.https) {
server = https.createServer(options, app);
} else {
server = http.createServer(app);
}
const serverCallback = () => {
let baseUrl;
if (config.server.https) {
logger.info('Https Server started.');
if (config.server.host) {
baseUrl = 'https://' + config.server.host + ':' + config.server.port;
} else {
baseUrl = 'https://localhost:' + config.server.port;
}
} else {
logger.info('Http Server started.');
if (config.server.host) {
baseUrl = 'http://' + config.server.host + ':' + config.server.port;
} else {
baseUrl = 'http://localhost:' + config.server.port;
}
}
logger.info('Base URL -> ' + baseUrl);
const serverHost = server.address().address;
const serverPort = server.address().port;
namespace = serverHost + '_' + serverPort + '_' + process.pid;
logger.info('Namespace -> ' + namespace);
// load registered socket.io handlers
if (config.socketio && config.socketio.active) {
// socketio_loader for socket.io
const socketioLoader = require('./loader/socketio_loader');
socketioLoader.load(server, app, sessionMiddleware, socketio, namespace);
logger.info('socket.io loader called.');
}
};
if (config.server.host) {
server.listen(config.server.port, config.server.host, config.server.backlog, serverCallback);
} else {
server.listen(config.server.port, serverCallback);
}
return server;
};
// Call main function
const webServer = main();