Updating setup files

This commit is contained in:
sairajzero 2021-10-16 00:19:26 +05:30
parent 57eb249ed6
commit 8c38c7e059
6 changed files with 193 additions and 19 deletions

100
setup/configure-backup.js Normal file
View File

@ -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;

View File

@ -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);

View File

@ -0,0 +1,2 @@
const createSchema = require('./create-schema');
createSchema(false);

View File

@ -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) {

View File

@ -1,11 +1,26 @@
let message = `
Exchange market
---------------
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 the schema in MySQL database.
npm run help - List all commands.
npm start - Start the application.
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="<password>"; npm start
LINUX:
PASSWORD="<password"> npm start
`;
console.log(message);

View File

@ -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');
}
})