-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitServer.js
67 lines (54 loc) · 1.77 KB
/
initServer.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
const { readFileSync } = require('fs');
const initServer = async (config, app) => {
const logger = config.logger.createLogger('initServer');
try {
const serverObject = {};
if (config.app.server.protocol == 'https') {
//https server
const https = require('https');
// loading SSL certificates
serverObject.server = https.createServer({
key: readFileSync('config/ssl/private.key'),
cert: readFileSync('config/ssl/certificate.crt'),
ca: readFileSync('config/ssl/ca_bundle.crt')
}, app);
} else {
//http server init
const http = require('http');
serverObject.server = http.createServer(app);
}
if (config.app.server.socket) {
//init socket io
const socket = require('socket.io');
if (config.app.server.socket.port) {
//if port is present then init socket on that port
serverObject.io = socket(config.app.server.socket.port);
} else {
// else use the provieded server to init the socket
serverObject.io = socket(serverObject.server);
}
}
if (config.app.db) {
//mongo db initiaization.
const mongoose = require('mongoose');
mongoose.Promise = Promise;
//if authentication available, then use the following.
const auth = config.app.db.username && config.app.db.password ? {
user: config.app.db.username,
pass: config.app.db.password,
auth: {
authDB: 'admin'
}
} : {};
//connecting to mongoose.
await mongoose.connect(`mongodb://${config.app.db.host}/${config.app.db.database}`, {
...auth,
useNewUrlParser: true
});
}
return serverObject;
} catch (error) {
logger.error(error);
}
};
module.exports = initServer;