Skip to content

Commit

Permalink
refactor: config reader
Browse files Browse the repository at this point in the history
  • Loading branch information
adamant-al committed Oct 30, 2022
1 parent d1dc669 commit 7f51ffa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions config.default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
/** To override min_confirmations for specific cryptocurrency. **/
"min_confirmations_ADM": 2,

/** Bot's name for notifications **/
"bot_name": "Lovely Bet Bot",

/** How to reply user in-chat, if first unknown command received. **/
"welcome_string": "Hi! 😊 I'm anonymous and Blockchain-proved bet bot. I accept bets on currency rates and pay rewards to winners. ℹ️ Learn more on ADAMANT’s blog or type **/help** to start betting.",

Expand Down
24 changes: 18 additions & 6 deletions modules/configReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const fields = {
default: 'log',
},
};

try {
let configFile;
if (isDev || process.env.JEST_WORKER_ID) {
Expand All @@ -110,16 +111,25 @@ try {
}
config = JSON.parse(jsonminify(fs.readFileSync(configFile, 'utf-8')));

let keysPair;
if (!config.node_ADM) {
exit(`Bot's config is wrong. ADM nodes are not set. Cannot start the Bot.`);
}
if (!config.passPhrase || config.passPhrase.length < 35) {
exit(`Bot's config is wrong. Set an ADAMANT passPhrase to manage the Bot.`);
}

let keyPair;
try {
keysPair = keys.createKeypairFromPassPhrase(config.passPhrase);
keyPair = keys.createKeypairFromPassPhrase(config.passPhrase);
} catch (e) {
exit('Passphrase is not valid! Error:' + e);
exit(`Bot's config is wrong. Invalid passPhrase. Error: ${e}. Cannot start the Bot.`);
}
const address = keys.createAddressFromPublicKey(keysPair.publicKey);
config.publicKey = keysPair.publicKey;
const address = keys.createAddressFromPublicKey(keyPair.publicKey);
config.keyPair = keyPair;
config.publicKey = keyPair.publicKey.toString('hex');
config.address = address;

config.notifyName = `${config.bot_name} (${config.address})`;
config.version = require('../package.json').version;

['min_confirmations'].forEach((param) => {
config.known_crypto.forEach((coin) => {
Expand All @@ -141,6 +151,8 @@ try {
exit(`Bet Bot ${address} config is wrong. Field type _${f}_ is not valid, expected type is _${fields[f].type.name}_. Cannot start Bot.`);
}
});

console.info(`The bot ${address} successfully read the config-file '${configFile}'${isDev ? ' (dev)' : ''}.`);
} catch (e) {
console.error('Error reading config: ' + e);
}
Expand Down
3 changes: 2 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const config = require('./modules/configReader');
const log = require('./helpers/log');
const port = config.api;
const db = require('./modules/DB');

Expand Down Expand Up @@ -38,5 +39,5 @@ if (port) {
});
});

app.listen(port, () => console.info('Server listening on port ' + port + ' http://localhost:' + port + '/db?tb=systemDb'));
app.listen(port, () => log.info(`${config.notifyName} debug server is listening on http://localhost:${port}. F. e., http://localhost:${port}/db?tb=systemDb.`));
}

0 comments on commit 7f51ffa

Please sign in to comment.