This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
96 lines (76 loc) · 2.63 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
/**
* @fileoverview Cloud CNC Core Server
*/
//Imports
const config = require('config');
const crypto = require('crypto');
const express = require('express');
const fs = require('fs');
const https = require('https');
const logger = require('./app/lib/logger.js');
const mongo = require('./app/lib/mongo.js').connect();
const redis = require('./app/lib/redis.js').connect();
const socket = require('./app/socket/server.js');
//Ensure storage directory exists
const storagePath = config.get('core.data.filesystem');
if (!fs.existsSync(storagePath))
{
logger.warn(`Storage directory (${storagePath}) does not exist, creating it!`);
fs.mkdirSync(storagePath);
}
//Ensure JWT secret exists
const secretPath = config.get('core.cryptography.secret');
if (!fs.existsSync(secretPath))
{
logger.warn(`JWT secret at ${secretPath} does not exist, creating it!`);
//Generate secret
const secret = crypto.randomBytes(512).toString('base64');
//Save
fs.writeFileSync(secretPath, secret);
}
//Ensure certificates exist
const certPath = config.get('core.cryptography.cert');
const keyPath = config.get('core.cryptography.key');
if (!fs.existsSync(certPath) || !fs.existsSync(keyPath))
{
logger.error(`TLS certificate at ${certPath} or key at ${keyPath} does not exist!
To create one with Open SSL (For development/testing purposes only!), run:
openssl req -x509 -newkey rsa:4096 -addext "subjectAltName = DNS:localhost, IP:127.0.0.1" -nodes -sha256 -days 365 -keyout ${keyPath} -out ${certPath}`);
process.exit(1);
}
//Get port number
const port = config.get('core.server.port');
//Express
const app = express();
const server = https.createServer({
cert: fs.readFileSync(certPath, 'utf8'),
key: fs.readFileSync(keyPath, 'utf8')
}, app).listen(port);
//Socket
const socketServer = socket.connect(server);
//Secondary imports (After databases are initialized)
const middleware = require('./app/middleware/index.js');
const routes = require('./app/routes/index.js');
//Middleware
app.use(middleware);
//Prevent leaking server information
app.disable('x-powered-by');
//Routes
app.use(routes);
//Shutdown
const shutdown = async () =>
{
logger.warn('Application shuting down!');
//Close servers
await new Promise((_, reject) => socketServer.close(reject));
await new Promise((_, reject) => server.close(reject));
//Close database connections
await new Promise((_, reject) => mongo.connection.close(reject));
await new Promise((_, reject) => redis.quit(reject));
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
process.on('exit', shutdown);
logger.info(`Started Cloud CNC Core, listening at :${port}`);
//Export
module.exports = server;