app.locals repalced
This commit is contained in:
87
server.js
87
server.js
@@ -15,45 +15,24 @@ 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';
|
||||
const { localPath, cache, runtimeFile } = require('@root/globalize.js');
|
||||
|
||||
//#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
|
||||
process.env.TZ = 'Europe/Berlin';
|
||||
|
||||
|
||||
//#region Nesessary json files
|
||||
const jsonFiles = ['configuration.json', 'stylesheet.json'];
|
||||
for (var i = 0; i < jsonFiles.length; i++) {
|
||||
if(!fs.existsSync(`${app.locals.path.source}/models/${jsonFiles[i]}`)) {
|
||||
fs.copyFileSync(`${app.locals.path.source}/backups/${jsonFiles[i]}.temp`, `${app.locals.path.source}/models/${jsonFiles[i]}`);
|
||||
if(!fs.existsSync(`${localPath.source}/models/${jsonFiles[i]}`)) {
|
||||
fs.copyFileSync(`${localPath.source}/backups/${jsonFiles[i]}.temp`, `${localPath.source}/models/${jsonFiles[i]}`);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
const securePath = `${localPath.source}/secure`;
|
||||
const certConfig = runtimeFile.configuration.live.certificate;
|
||||
|
||||
let httpsOptions = {};
|
||||
|
||||
@@ -105,9 +84,9 @@ const server = https.createServer(httpsOptions, app);
|
||||
await service.get('socketManager').addAsync('admin');
|
||||
|
||||
service.set('sqlManager', new SqlManager());
|
||||
service.get('sqlManager').addInstance('main', app.locals.configuration.integration.sql.connect);
|
||||
service.get('sqlManager').addInstance('main', runtimeFile.configuration.live.integration.sql.connect);
|
||||
|
||||
databaseModel.set('eventlog', require(`${app.locals.path.source}/models/eventlogModel`)(service.get('sqlManager').getInstance('main')));
|
||||
databaseModel.set('eventlog', require(`${localPath.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')));
|
||||
|
||||
@@ -136,22 +115,22 @@ const server = https.createServer(httpsOptions, app);
|
||||
databaseModel.set('rolePermissionsModel', require(`@models/rolePermissionsModel`)(service.get('sqlManager').getInstance('main')));
|
||||
|
||||
service.set('rbacManager', new RBACManager(databaseModel));
|
||||
service.set('authenticationManager', new AuthenticationManager(databaseModel.get('authentication'), app.locals.configuration.integration.token.secret));
|
||||
service.set('authenticationManager', new AuthenticationManager(databaseModel.get('authentication'), runtimeFile.configuration.live.integration.token.secret));
|
||||
|
||||
service.set('activeDirectoryManager', new ActiveDirectory(app.locals.configuration.integration.activedirectory))
|
||||
service.set('activeDirectoryManager', new ActiveDirectory(runtimeFile.configuration.live.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));
|
||||
service.set('pluginManager', new PluginManager(app, databaseModel.get('plugin'), localPath.plugins, runtimeFile.configuration.live.plugin.chown, service));
|
||||
|
||||
exports.databaseModel = databaseModel;
|
||||
exports.service = service;
|
||||
exports.path = app.locals.path;
|
||||
exports.path = localPath;
|
||||
//#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');
|
||||
require(`${localPath.root}/utils.js`);
|
||||
let helpers = service.get('fileSystemManager').loadAllFiles(`${localPath.public}/helpers`, '.js');
|
||||
exports.helpers = helpers;
|
||||
|
||||
// app.use(service.get('vaultifyManager').createMiddleware());
|
||||
@@ -160,17 +139,17 @@ const server = https.createServer(httpsOptions, app);
|
||||
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(favicon(`${localPath.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(express.static(localPath.root));
|
||||
app.use(express.static(localPath.public));
|
||||
app.use(express.static(localPath.source));
|
||||
|
||||
|
||||
|
||||
app.use(function(request, response, next) {
|
||||
if (!request.secure) {
|
||||
return response.redirect("https://" + request.headers.host + request.url + app.locals.configuration.server.port);
|
||||
return response.redirect("https://" + request.headers.host + request.url + runtimeFile.configuration.live.server.port);
|
||||
}
|
||||
next(); // Http redirection to secure protocol
|
||||
})
|
||||
@@ -180,8 +159,8 @@ const server = https.createServer(httpsOptions, app);
|
||||
//#region App config values
|
||||
app.set('view engine', '.hbs');
|
||||
app.set('views', [
|
||||
`${app.locals.path.public}/views`,
|
||||
`${app.locals.path.public}/views/integrated`
|
||||
`${localPath.public}/views`,
|
||||
`${localPath.public}/views/integrated`
|
||||
]);
|
||||
app.set('trust proxy', true)
|
||||
//#endregion
|
||||
@@ -196,13 +175,13 @@ const server = https.createServer(httpsOptions, app);
|
||||
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`
|
||||
partialsDir: `${localPath.public}/views/partials`,
|
||||
layoutsDir: `${localPath.public}/views/layouts`,
|
||||
defaultLayout: `${localPath.public}/views/layouts/default.hbs`
|
||||
}).engine)
|
||||
|
||||
|
||||
server.listen(app.locals.configuration.server.port, () => {
|
||||
server.listen(runtimeFile.configuration.live.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);
|
||||
@@ -223,15 +202,15 @@ const server = https.createServer(httpsOptions, app);
|
||||
|
||||
|
||||
//#region Implement routes
|
||||
require(`${app.locals.path.source}/routes/loginRoutes.js`).route(app, service); // #1 - no token security! important: first!!!
|
||||
require(`${app.locals.path.source}/routes/indexRoutes.js`).route(app, service); // #2 - token security enabled at this point
|
||||
require(`${app.locals.path.source}/routes/adminRoutes.js`).route(app, service); // #3 - token security always enabled
|
||||
require(`${localPath.source}/routes/loginRoutes.js`).route(app, service); // #1 - no token security! important: first!!!
|
||||
require(`${localPath.source}/routes/indexRoutes.js`).route(app, service); // #2 - token security enabled at this point
|
||||
require(`${localPath.source}/routes/adminRoutes.js`).route(app, service); // #3 - token security always enabled
|
||||
//#endregion
|
||||
|
||||
app.use(service.get('rbacManager').authenticate());
|
||||
|
||||
//#region Implements sockets
|
||||
require(`${app.locals.path.source}/sockets/mainSocket.js`)(
|
||||
require(`${localPath.source}/sockets/mainSocket.js`)(
|
||||
app,
|
||||
service.get('socketManager'),
|
||||
'/',
|
||||
@@ -241,7 +220,7 @@ const server = https.createServer(httpsOptions, app);
|
||||
service.get('eventManager'),
|
||||
service.get('activeDirectoryManager')
|
||||
);
|
||||
require(`${app.locals.path.source}/sockets/adminSocket.js`)(
|
||||
require(`${localPath.source}/sockets/adminSocket.js`)(
|
||||
app,
|
||||
service.get('socketManager'),
|
||||
'admin',
|
||||
@@ -253,11 +232,11 @@ const server = https.createServer(httpsOptions, app);
|
||||
|
||||
setTimeout(() => {
|
||||
service.get('eventManager').write(null, 1, null,
|
||||
`${app.locals.configuration.server.name} is running`,
|
||||
`fqdn: https://${os.hostname()}:${app.locals.configuration.server.port}/`,
|
||||
`${runtimeFile.configuration.live.server.name} is running`,
|
||||
`fqdn: https://${os.hostname()}:${runtimeFile.configuration.live.server.port}/`,
|
||||
`process id: ${process.pid}`,
|
||||
`url: ${os.hostname()}`,
|
||||
`port: ${app.locals.configuration.server.port}`
|
||||
`port: ${runtimeFile.configuration.live.server.port}`
|
||||
)
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user