From 8c38c7e05908c7c92497fc78e77eb6025e5ffd43 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Sat, 16 Oct 2021 00:19:26 +0530 Subject: [PATCH] Updating setup files --- setup/configure-backup.js | 100 ++++++++++++++++++++++++++++++++++ setup/configure-settings.js | 69 ++++++++++++++++++++--- setup/create-backup-schema.js | 2 + setup/create-schema.js | 6 +- setup/help.js | 31 ++++++++--- setup/post-install.js | 4 +- 6 files changed, 193 insertions(+), 19 deletions(-) create mode 100644 setup/configure-backup.js create mode 100644 setup/create-backup-schema.js diff --git a/setup/configure-backup.js b/setup/configure-backup.js new file mode 100644 index 0000000..04f4d20 --- /dev/null +++ b/setup/configure-backup.js @@ -0,0 +1,100 @@ +const fs = require('fs'); +const getInput = require('./getInput'); + +var config, flag_new; +try { + config = require('../args/backup-config.json'); + flag_new = false; +} catch (error) { + config = { + "sql_user": null, + "sql_pwd": null, + "sql_db": "exchange", + "sql_host": "localhost", + + "main_server_url": null, + "private_key": null + }; + flag_new = true; +} + +function flaggedYesOrNo(text) { + return new Promise((resolve) => { + if (flag_new) + resolve(true); + else + getInput.YesOrNo(text) + .then(result => resolve(result)) + .catch(error => reject(error)) + }) +} + +function configureMainServerURL() { + return new Promise(resolve => { + getInput.Text('Enter URL of main server', config["main_server_url"]).then(url => { + config["main_server_url"] = url; + resolve(true); + }) + }) +} + +function configureSQL() { + return new Promise(resolve => { + flaggedYesOrNo('Do you want to re-configure mySQL connection').then(value => { + if (value) { + console.log('Enter mySQL connection values: ') + getInput.Text('Host', config['sql_host']).then(host => { + config['sql_host'] = host; + getInput.Text('Database name', config['sql_db']).then(dbname => { + config['sql_db'] = dbname; + getInput.Text('MySQL username', config['sql_user']).then(sql_user => { + config['sql_user'] = sql_user; + getInput.Text('Mysql password', config['sql_pwd']).then(sql_pwd => { + config['sql_pwd'] = sql_pwd; + resolve(true); + }) + }) + }) + }) + } else + resolve(false); + }) + }) +} + +function configure() { + return new Promise((resolve, reject) => { + configureMainServerURL().then(port_result => { + configureSQL().then(sql_result => { + fs.writeFile(__dirname + '/../args/backup-config.json', JSON.stringify(config), 'utf8', (err) => { + if (err) { + console.error(err); + return reject(false); + } + console.log('Configuration successful!'); + if (sql_result) { + getInput.YesOrNo('Do you want to create schema in the database').then(value => { + if (value) { + const createSchema = require('./create-schema'); + createSchema(false).then(result => resolve(result)) + .catch(error => { + console.log('Retry using: \n' + 'npm run create-backup-schema'); + reject(error); + }); + } else { + console.log('To create schema, use: \n' + 'npm run create-backup-schema'); + resolve(true); + } + }); + } else + resolve(true); + }) + }) + }); + }) +} + +if (!module.parent) + configure().then(_ => null).catch(_ => null); +else + module.exports = configure; \ No newline at end of file diff --git a/setup/configure-settings.js b/setup/configure-settings.js index fa0fb7d..2347f3f 100644 --- a/setup/configure-settings.js +++ b/setup/configure-settings.js @@ -3,7 +3,7 @@ const getInput = require('./getInput'); var config, flag_new; try { - config = require('./args/config.json'); + config = require('../args/app-config.json'); flag_new = false; } catch (error) { config = { @@ -13,7 +13,10 @@ try { "sql_user": null, "sql_pwd": null, "sql_db": "exchange", - "sql_host": "localhost" + "sql_host": "localhost", + + "backup-port": "8081", + "backup-floIDs": [] }; flag_new = true; } @@ -29,15 +32,67 @@ function flaggedYesOrNo(text) { }) } +function getBackupIDs(ids) { + return new Promise((resolve, reject) => { + getInput("", "continue").then(id => { + if (id === "continue") + resolve(Array.from(new Set(ids))); + else { + ids.push(id); + getBackupIDs(ids) + .then(result => resolve(result)) + .catch(error => reject(error)); + } + }) + }) +} + +function configureBackup() { + return new Promise(resolve => { + getInput.Text('Enter backup port (N = No backup)', config["backup-port"]).then(backup_port => { + config["backup-port"] = backup_port === N ? null : backup_port; + if (!config["backup-port"]) + return resolve(true); + getInput.YesOrNo('Do you want to add/remove backup floIDs?').then(value => { + if (value) { + console("Enter floIDs to add as backup: "); + getBackupIDs(config["backup-floIDs"]).then(ids => { + //delete backup IDs + let tmp_obj = {}; + for (let i in ids) { + console.log(i + 1, ":", ids[i]); + tmp_obj[i + 1] = ids[i]; + } + getInput.Text("Enter numbers to delete (seperated by comma)", "continue").then(ri => { + if (ri === "continue") + config["backup-floIDs"] = ids; + else { + for (let i of ri.split(",")) + delete tmp_obj[parseInt(i)]; + let tmp_array = []; + for (let id of tmp_obj) + tmp_array.push(id); + config["backup-floIDs"] = tmp_array; + } + resolve(true); + }) + }) + } else + resolve(true); + }) + }) + }) +} function configurePort() { return new Promise(resolve => { getInput.Text('Enter port', config["port"]).then(port => { config["port"] = port; - resolve(true); + configureBackup() + .then(result => resolve(true)) }) }) -}; +} function configureSQL() { return new Promise(resolve => { @@ -61,7 +116,7 @@ function configureSQL() { resolve(false); }) }) -}; +} function randomizeSessionSecret() { return new Promise((resolve) => { @@ -72,7 +127,7 @@ function randomizeSessionSecret() { var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for (var i = 0; i < N; i++) secret += characters.charAt(Math.floor(Math.random() * characters.length)); - config['secret'] = secret + config['secret'] = secret; resolve(true); } else resolve(false); @@ -85,7 +140,7 @@ function configure() { configurePort().then(port_result => { randomizeSessionSecret().then(secret_result => { configureSQL().then(sql_result => { - fs.writeFile(__dirname + '/../args/config.json', JSON.stringify(config), 'utf8', (err) => { + fs.writeFile(__dirname + '/../args/app-config.json', JSON.stringify(config), 'utf8', (err) => { if (err) { console.error(err); return reject(false); diff --git a/setup/create-backup-schema.js b/setup/create-backup-schema.js new file mode 100644 index 0000000..ba67ef6 --- /dev/null +++ b/setup/create-backup-schema.js @@ -0,0 +1,2 @@ +const createSchema = require('./create-schema'); +createSchema(false); \ No newline at end of file diff --git a/setup/create-schema.js b/setup/create-schema.js index 36a2259..b8758e3 100644 --- a/setup/create-schema.js +++ b/setup/create-schema.js @@ -1,8 +1,8 @@ const fs = require('fs'); -const config = require('./args/config.json'); -let Database = require('./src/database'); +let Database = require('../src/database'); -function createSchema() { +function createSchema(app = true) { + const config = require('../args/' + (app ? 'app' : 'backup') + "-config.json"); return new Promise((resolve, reject) => { fs.readFile(__dirname + '/../args/schema.sql', 'utf8', (err, data) => { if (err) { diff --git a/setup/help.js b/setup/help.js index 9f2d8b1..9092343 100644 --- a/setup/help.js +++ b/setup/help.js @@ -1,11 +1,26 @@ let message = ` Exchange market +--------------- -npm install - Install the app and node modules. -npm run setup - Finish the setup (configure and reset password). -npm run configure - Configure the app. -npm run reset-password - Reset the password (for private-key). -npm run create-schema - Create the schema in MySQL database. -npm run help - List all commands. -npm start - Start the application. -`; \ No newline at end of file +npm install - Install the app and node modules. +npm run help - List all commands. +npm run setup - Finish the setup (configure and reset password). +npm run configure - Configure the app. +npm run reset-password - Reset the password (for private-key). +npm run create-schema - Create schema in MySQL database. +npm run configure-backup - Configure the backup. +npm run create-backup-schema - Create backup-schema in MySQL database. +npm run backup - Run the backup-node. + +npm start - Start the application (main). + +NOTE: env variable 'PASSWORD' required for 'npm start'. + +WINDOWS: +$env:PASSWORD=""; npm start + +LINUX: +PASSWORD=" npm start +`; + +console.log(message); \ No newline at end of file diff --git a/setup/post-install.js b/setup/post-install.js index e36ffef..e56f37a 100644 --- a/setup/post-install.js +++ b/setup/post-install.js @@ -28,6 +28,8 @@ getInput.YesOrNo('Do you want to finish the setup now').then(value => { console.log('Reset the password later using:\n' + 'npm run reset-password'); }) }) - } else + } else { console.log('Finish the setup later using:\n' + 'npm run setup'); + console.log('To configure for backup use:\n' + 'npm run configure-backup'); + } }) \ No newline at end of file