Skip to content

Commit

Permalink
- Reldens - v4.0.0 - Installer WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
damian-pastorini committed Nov 21, 2023
1 parent 9f15d44 commit 87b9866
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 320 deletions.
8 changes: 0 additions & 8 deletions lib/config/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,16 @@ class ConfigProcessor
this.avoidLog = false;
}

/**
* This method will receive a path that's should be always composed by 3 or 4 parameters split by a slash and will
* return a property of the same processor which should have the configurations assigned.
*/
get(path, defaultValue)
{
let defaultReturn = 'undefined' !== typeof defaultValue ? defaultValue : false;
// since the amount of parameters should be always 3 (for a config group) or 4 (for a single value) then we can
// easily split the path:
let pathArray = path.split('/');
// verify path size:
if(3 > pathArray.length){
if(!this.avoidLog){
Logger.error('Path level is too low:', path);
}
return defaultReturn;
}
// default value will be always false:
let levelCheck = (this[pathArray[0]] || {});
for(let i = 1; i < pathArray.length; i++){
if(!sc.hasOwn(levelCheck, pathArray[i])){
Expand Down
12 changes: 4 additions & 8 deletions lib/game/server/app-server-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ class AppServerFactory
key: fs.readFileSync(process.env.RELDENS_EXPRESS_HTTPS_PRIVATE_KEY, this.encoding),
cert: fs.readFileSync(process.env.RELDENS_EXPRESS_HTTPS_CERT, this.encoding)
};
if(process.env.RELDENS_EXPRESS_HTTPS_CHAIN){
credentials['ca'] = fs.readFileSync(
process.env.RELDENS_EXPRESS_HTTPS_CHAIN,
process.env.RELDENS_DEFAULT_ENCODING
);
}
if(process.env.RELDENS_EXPRESS_HTTPS_PASSPHRASE){
credentials['passphrase'] = process.env.RELDENS_EXPRESS_HTTPS_PASSPHRASE;
let httpsChain = (process.env.RELDENS_EXPRESS_HTTPS_CHAIN || '').toString();
if('' !== httpsChain){
credentials['ca'] = fs.readFileSync(httpsChain, this.encoding);
}
credentials['passphrase'] = (process.env.RELDENS_EXPRESS_HTTPS_PASSPHRASE || '').toString();
return https.createServer(credentials, app);
}

Expand Down
11 changes: 6 additions & 5 deletions lib/game/server/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class Installer
app.use(express.static(this.themeManager.installerPath));
app.use(session({secret: this.secretKey, resave: true, saveUninitialized: true}));
app.use((req, res, next) => {
if('1' === req.params.success && this.startCallback){
Logger.info('Installation success, starting server...');
return this.startCallback();
}
if(this.isInstalled()){
return next();
}
Expand Down Expand Up @@ -130,7 +126,12 @@ class Installer
let knexFileContent = TemplateEngine.render(knexDistTemplate, templateVariables);
fs.writeFileSync(path.join(this.themeManager.projectRoot, 'knexfile.js'), knexFileContent);
fs.writeFileSync(path.join(this.themeManager.projectRoot, 'install.lock'), '');
return res.redirect('/?success=1');
Logger.info('Installation success!');
if(this.startCallback){
Logger.info('Running Server Start callback...');
await this.startCallback();
}
return res.redirect('/?success=1')
});
}

Expand Down
45 changes: 33 additions & 12 deletions lib/game/server/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ class ServerManager
this.dataServerDriver = dataServerDriver;
this.events = eventsManager || EventsManagerSingleton;
this.themeManager = new ThemeManager(config);
this.installer = new Installer({themeManager: this.themeManager, startCallback: this.start});
this.installer = new Installer({
themeManager: this.themeManager,
startCallback: async () => {
return await this.start();
}
});
this.initializeConfiguration(config);
}

setupCustomServerPlugin(config)
Expand All @@ -71,19 +77,31 @@ class ServerManager
this.configManager = new ConfigManager({events: this.events, customClasses: (config.customClasses || {})});
this.projectRoot = sc.get(config, 'projectRoot', './');
Logger.info('Project root: '+this.projectRoot, 'Module root: '+__dirname);
let envPath = path.join(this.projectRoot, '.env');
dotenv.config({debug: process.env.DEBUG, path: envPath});
this.configServer = {
host: process.env.RELDENS_APP_HOST || 'http://localhost',
port: Number(process.env.PORT) || Number(process.env.RELDENS_APP_PORT) || 8080,
dotenv.config({debug: process.env.DEBUG, path: this.themeManager.envFilePath});
this.configServer = this.fetchConfigServerFromEnvironmentVariables();
this.isHotPlugEnabled = process.env.RELDENS_HOT_PLUG || false;
}

fetchConfigServerFromEnvironmentVariables()
{
let host = (process.env.RELDENS_APP_HOST || '').toString();
let port = Number(process.env.PORT || 0);
if (0 === port){
port = Number(process.env.RELDENS_APP_PORT || 0);
if (0 === port){
port = 8080;
}
}
return {
host: '' !== host ? host : 'http://localhost',
port: port,
monitor: {
enabled: 1 === Number(process.env.RELDENS_MONITOR || 0) || false,
auth: process.env.RELDENS_MONITOR_AUTH || false,
user: process.env.RELDENS_MONITOR_USER,
pass: process.env.RELDENS_MONITOR_PASS,
auth: Boolean(process.env.RELDENS_MONITOR_AUTH || false),
user: process.env.RELDENS_MONITOR_USER || '',
pass: process.env.RELDENS_MONITOR_PASS || '',
}
};
this.isHotPlugEnabled = process.env.RELDENS_HOT_PLUG || false;
}

async initializeStorage(config, dataServerDriver)
Expand Down Expand Up @@ -113,7 +131,10 @@ class ServerManager
return false;
}
Logger.info('Starting Server!');
await this.initializeConfiguration(this.rawConfig);
if(this.appServer?.listening){
await this.appServer.close();
}
this.setupCustomServerPlugin(this.rawConfig);
await this.themeManager.validateOrCreateTheme();
await this.initializeStorage(this.rawConfig, this.dataServerDriver);
this.configManager.dataServer = this.dataServer;
Expand Down Expand Up @@ -158,10 +179,10 @@ class ServerManager
{
await this.createAppServer();
if(!this.installer.isInstalled()){
Logger.info('Reldens not installed, preparing setup procedure.');
return await this.installer.prepareSetup(this.app);
}
this.enableServeStaticsAndHomePage();
// await this.createGameServer();
return true;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/game/server/theme-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ThemeManager

projectRoot = '';
projectRootPackageJson = '';
envFilePath = '';
installationLockPath = '';
reldensModulePath = '';
reldensModuleLibPath = '';
Expand Down Expand Up @@ -49,6 +50,7 @@ class ThemeManager
{
this.projectRoot = sc.get(props, 'projectRoot', '');
this.projectRootPackageJson = path.join(this.projectRoot, 'package.json');
this.envFilePath = path.join(this.projectRoot, '.env');
this.installationLockPath = path.join(this.projectRoot, 'install.lock');
this.projectThemeName = sc.get(props, 'projectThemeName', GameConst.STRUCTURE.DEFAULT);
this.reldensModulePath = path.join(this.projectRoot, 'node_modules', 'reldens');
Expand Down Expand Up @@ -192,12 +194,11 @@ class ThemeManager

copyEnvFile()
{
let envFile = path.join(this.projectRoot, '.env');
if(fs.existsSync(envFile)){
if(fs.existsSync(this.envFilePath)){
Logger.info('File already exists: .env');
return false;
}
fs.copyFileSync(path.join(this.reldensModulePathInstallTemplatesFolder, '.env.dist'), envFile);
fs.copyFileSync(path.join(this.reldensModulePathInstallTemplatesFolder, '.env.dist'), this.envFilePath);
Logger.info('Reminder: edit the .env file!');
}

Expand Down
Loading

0 comments on commit 87b9866

Please sign in to comment.