initial files
This commit is contained in:
253
server.js
Normal file
253
server.js
Normal file
@@ -0,0 +1,253 @@
|
||||
//#region Modules
|
||||
const { dirname } = require('path');
|
||||
const path = require('path');
|
||||
const https = require('https');
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
var { create } = require('express-handlebars');
|
||||
const cookieParser = require('cookie-parser');
|
||||
var fs = require('fs');
|
||||
var os = require('os');
|
||||
var favicon = require('serve-favicon');
|
||||
const Sequelize = require('sequelize');
|
||||
const { Server } = require('socket.io');
|
||||
const { on } = require('cluster');
|
||||
// const { start } = require('repl');
|
||||
// const WebSocket = require('ws');
|
||||
//#endregion
|
||||
|
||||
|
||||
require('module-alias/register'); // define paths in package.json
|
||||
process.env.TZ = 'Europe/Berlin';
|
||||
|
||||
//#region Paths
|
||||
app.locals.path = {
|
||||
root: dirname(require.main.filename),
|
||||
plugins: `${dirname(require.main.filename)}/plugins`,
|
||||
public: `${dirname(require.main.filename)}/public`,
|
||||
source: `${dirname(require.main.filename)}/src`,
|
||||
}
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region Server configuration
|
||||
app.locals.stylesheet = JSON.parse(fs.readFileSync(`${app.locals.path.source}/models/stylesheet.json`, 'utf-8'));
|
||||
app.locals.configuration = JSON.parse(fs.readFileSync(`${app.locals.path.source}/models/configuration.json`, 'utf-8'));
|
||||
app.locals.package = JSON.parse(fs.readFileSync(`${app.locals.path.root}/package.json`, 'utf-8'));
|
||||
|
||||
app.locals.startMenuItems = [ ];
|
||||
|
||||
|
||||
|
||||
(async () => {
|
||||
// const server = https.createServer({
|
||||
// key: fs.readFileSync(`${app.locals.path.source}/secure/${app.locals.configuration.certificate.key}`),
|
||||
// cert: fs.readFileSync(`${app.locals.path.source}/secure/${app.locals.configuration.certificate.chain}`),
|
||||
// pfx: fs.readFileSync(`${app.locals.path.source}/secure/${app.locals.configuration.certificate.pfx}`),
|
||||
// passphrase: "password",
|
||||
// //cert: fs.readFileSync(`${app.locals.path.source}/secure/${app.locals.configuration.certificate.chain}`),
|
||||
// }, app);
|
||||
const securePath = `${app.locals.path.source}/secure`;
|
||||
const certConfig = app.locals.configuration.certificate;
|
||||
|
||||
let httpsOptions = {};
|
||||
|
||||
if (certConfig.pfx) {
|
||||
httpsOptions = {
|
||||
pfx: fs.readFileSync(`${securePath}/${certConfig.pfx}`),
|
||||
passphrase: certConfig.passphrase
|
||||
};
|
||||
} else {
|
||||
httpsOptions = {
|
||||
key: fs.readFileSync(`${securePath}/${certConfig.key}`),
|
||||
cert: fs.readFileSync(`${securePath}/${certConfig.chain}`)
|
||||
};
|
||||
}
|
||||
|
||||
const server = https.createServer(httpsOptions, app);
|
||||
|
||||
// const wss = new WebSocket.Server({ server });
|
||||
// wss.on('connection', socket => {
|
||||
// socket.send('HELLO')
|
||||
// });
|
||||
|
||||
|
||||
const io = new Server(server, {
|
||||
pingTimeout: 60000,
|
||||
maxHttpBufferSize: 1e8, // 100 MB
|
||||
});
|
||||
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region Services/DatabaseModel
|
||||
let service = new Map();
|
||||
let databaseModel = new Map();
|
||||
|
||||
let SocketManager = require(`@services/socketManager.js`);
|
||||
let SqlManager = require(`@services/sqlManager.js`);
|
||||
let EventManager = require(`@services/eventManager.js`);
|
||||
let NotifyTrayManager = require(`@services/notifyTrayManager.js`);
|
||||
let PluginManager = require(`@services/pluginManager.js`);
|
||||
let FileSystemManager = require(`@services/fileSystemManager.js`);
|
||||
let AuthenticationManager = require(`@services/authenticationManager.js`);
|
||||
let ActiveDirectory = require(`@services/activeDirectoryManager.js`);
|
||||
|
||||
service.set('socketManager', new SocketManager(io));
|
||||
await service.get('socketManager').addAsync('/');
|
||||
await service.get('socketManager').addAsync('admin');
|
||||
|
||||
service.set('sqlManager', new SqlManager());
|
||||
service.get('sqlManager').addInstance('main', app.locals.configuration.integration.sql.connect);
|
||||
|
||||
databaseModel.set('eventlog', require(`${app.locals.path.source}/models/eventlogModel`)(service.get('sqlManager').getInstance('main')));
|
||||
databaseModel.set('eventlogView', require(`@models/eventlogView`)(service.get('sqlManager').getInstance('main')));
|
||||
service.set('eventManager', new EventManager(app, databaseModel.get('eventlog'), databaseModel.get('eventlogView'), service.get('socketManager')));
|
||||
|
||||
databaseModel.set('notifyTrayModel', require(`@models/notifyTrayModel`)(service.get('sqlManager').getInstance('main')));
|
||||
databaseModel.set('notifyTrayObjectModel', require(`@models/notifyTrayObjectsModel`)(service.get('sqlManager').getInstance('main')));
|
||||
databaseModel.set('notifyTrayView', require(`@models/notifyTrayView`)(service.get('sqlManager').getInstance('main')));
|
||||
service.set('notifyTray', new NotifyTrayManager(databaseModel.get('notifyTrayModel'), databaseModel.get('notifyTrayView'), databaseModel.get('notifyTrayObjectModel')) );
|
||||
|
||||
databaseModel.set('plugin', require(`@models/pluginModel`)(service.get('sqlManager').getInstance('main')));
|
||||
databaseModel.set('authentication', require(`@models/authenticationModel`)(service.get('sqlManager').getInstance('main')));
|
||||
|
||||
service.set('fileSystemManager', new FileSystemManager());
|
||||
service.set('authenticationManager', new AuthenticationManager(databaseModel.get('authentication'), app.locals.configuration.integration.token.secret, service.get('eventManager')));
|
||||
service.set('activeDirectoryManager', new ActiveDirectory(app.locals.configuration.integration.activedirectory))
|
||||
|
||||
// everytime last created service!
|
||||
service.set('pluginManager', new PluginManager(app, databaseModel.get('plugin'), app.locals.path.plugins, app.locals.configuration.plugin.chown, service));
|
||||
|
||||
exports.databaseModel = databaseModel;
|
||||
exports.service = service;
|
||||
exports.path = app.locals.path;
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region Service-Registration/Middleware/Utils/Helpers
|
||||
require(`${app.locals.path.root}/utils.js`);
|
||||
let helpers = service.get('fileSystemManager').loadAllFiles(`${app.locals.path.public}/helpers`, '.js');
|
||||
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
app.use(cookieParser());
|
||||
app.use(favicon(`${app.locals.path.public}/images/radix_os_icon.ico`));
|
||||
|
||||
app.use(express.static(app.locals.path.root));
|
||||
app.use(express.static(app.locals.path.public));
|
||||
app.use(express.static(app.locals.path.source));
|
||||
|
||||
|
||||
|
||||
app.use(function(request, response, next) {
|
||||
if (!request.secure) {
|
||||
return response.redirect("https://" + request.headers.host + request.url + app.locals.configuration.server.port);
|
||||
}
|
||||
next(); // Http redirection to secure protocol
|
||||
})
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region App config values
|
||||
app.set('view engine', '.hbs');
|
||||
app.set('views', [
|
||||
`${app.locals.path.public}/views`,
|
||||
`${app.locals.path.public}/views/integrated`
|
||||
]);
|
||||
app.set('trust proxy', true)
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region Error exception handling
|
||||
app.on('uncaughtException', (err) => service.get('eventManager').write(null, 8, null, err ));
|
||||
process.on('uncaughtException', (err) => service.get('eventManager').write(null, 8, null, err ));
|
||||
process.on('unhandledRejection', (reason, promise) => service.get('eventManager').write(null, 8, null, reason ));
|
||||
//#endregion
|
||||
|
||||
|
||||
app.engine('hbs', create({
|
||||
extname: 'hbs',
|
||||
helpers: helpers,
|
||||
partialsDir: `${app.locals.path.public}/views/partials`,
|
||||
layoutsDir: `${app.locals.path.public}/views/layouts`,
|
||||
defaultLayout: `${app.locals.path.public}/views/layouts/default.hbs`
|
||||
}).engine)
|
||||
|
||||
|
||||
server.listen(app.locals.configuration.server.port, () => {
|
||||
(async () => {
|
||||
const databaseTest = await service.get('sqlManager').test("main"); // Check if database connection is established
|
||||
service.get('eventManager').write(null, databaseTest.levelId, null, databaseTest.message);
|
||||
|
||||
// Loading plugins
|
||||
const plugins = await service.get('pluginManager').loadAll()
|
||||
// const pluginsLoaded = {
|
||||
// levelId: plugins.some(plugin => plugin.levelId > 0) ? 2 : 0,
|
||||
// message: plugins.map(plugin => `${plugin.pluginName} v${plugin.metadata.version} ${plugin.message}`).join('<br>')
|
||||
// }
|
||||
// service.get('eventManager').write(null, pluginsLoaded.levelId, null, pluginsLoaded.message);
|
||||
|
||||
plugins.forEach(plugin => {
|
||||
service.get('eventManager').write(null, plugin.levelId, null, `${plugin.pluginName} v${plugin.metadata.version} ${plugin.message}`);
|
||||
});
|
||||
|
||||
//#region Menu-Generator
|
||||
app.use(async (req, res, next) => {
|
||||
next();
|
||||
});
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region Implement routes
|
||||
require(`${app.locals.path.source}/routes/indexRoutes.js`).route(app, service);
|
||||
require(`${app.locals.path.source}/routes/loginRoutes.js`).route(
|
||||
app,
|
||||
service.get('authenticationManager'),
|
||||
service.get('socketManager'),
|
||||
service.get('eventManager')
|
||||
);
|
||||
require(`${app.locals.path.source}/routes/adminRoutes.js`).route(
|
||||
app,
|
||||
service.get('authenticationManager'),
|
||||
service.get('pluginManager'),
|
||||
service.get('eventManager'),
|
||||
service.get('socketManager'),
|
||||
service.get('activeDirectoryManager'),
|
||||
`${app.locals.path.source}/models/stylesheet.json`
|
||||
);
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region Implements sockets
|
||||
require(`${app.locals.path.source}/sockets/mainSocket.js`)(
|
||||
app,
|
||||
service.get('socketManager'),
|
||||
'/',
|
||||
service.get('pluginManager'),
|
||||
databaseModel.get('authentication'),
|
||||
service.get('fileSystemManager'),
|
||||
service.get('eventManager'),
|
||||
service.get('activeDirectoryManager')
|
||||
);
|
||||
require(`${app.locals.path.source}/sockets/adminSocket.js`)(
|
||||
app,
|
||||
service.get('socketManager'),
|
||||
'admin',
|
||||
service.get('eventManager')
|
||||
);
|
||||
//#endregion
|
||||
|
||||
})();
|
||||
|
||||
setTimeout(() => {
|
||||
service.get('eventManager').write(null, 1, null,
|
||||
`${app.locals.configuration.server.name} is running`,
|
||||
`fqdn: https://${os.hostname()}:${app.locals.configuration.server.port}/`,
|
||||
`process id: ${process.pid}`,
|
||||
`url: ${os.hostname()}`,
|
||||
`port: ${app.locals.configuration.server.port}`
|
||||
)
|
||||
}, 1000);
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user