diff options
| -rwxr-xr-x | cli.js | 53 | ||||
| -rw-r--r-- | index.js | 17 | ||||
| -rw-r--r-- | models.js | 6 |
3 files changed, 72 insertions, 4 deletions
@@ -226,6 +226,59 @@ vorpal }); vorpal + .command('secret set <key> <value>', 'Set a secret key value pair.') + .action(async function (args, callback) { + try { + await checkConnection(mongoURL, this); + } catch (err) { + showMongoNotSetError(vorpal); + return callback(); + } + + if (!args.key || !args.value) { + this.log('Please provide both "key" and "value" arguments.'); + return callback(); + } + + try { + await models.secrets.update( + {key: args.key}, + {key: args.key, value: args.value}, + {upsert: true}); + this.log(chalk.green(`Secret "${args.key}" set successfully.`)); + } catch (err) { + this.log(chalk.red(`Failed to set secret ${args.key}!`)); + this.log(chalk.red(String(err))); + } + callback(); + }); + +vorpal + .command('secret remove <key>', 'Remove a secret key value pair.') + .action(async function (args, callback) { + try { + await checkConnection(mongoURL, this); + } catch (err) { + showMongoNotSetError(vorpal); + return callback(); + } + + if (!args.key) { + this.log('Please provide the "key" argument.'); + return callback(); + } + + try { + await models.secrets.remove({key: args.key}); + this.log(chalk.green(`Secret "${args.key}" removed successfully.`)); + } catch (err) { + this.log(chalk.red(`Failed to remove secret ${args.key}!`)); + this.log(chalk.red(String(err))); + } + callback(); + }); + +vorpal .command('info', 'Show information about the session') .action(async function (args, callback) { this.log('Mongo URL: ' + chalk.yellow(mongoURL)); @@ -21,20 +21,31 @@ app.all('/:path*', async (req, res) => { const path = req.params.path.split('/')[0]; if (!path || path.length === 0) { res.status(400); - res.send('Empty endpoint.'); + return res.send('Empty endpoint.'); } const endpoint = await models.endpoints.findOne({name: path}); if (!endpoint) { res.status(400); - res.send('No such endpoint.'); + return res.send('No such endpoint.'); } + const secretsList = await models.endpoints.find(); + if (!secretsList) { + res.status(500); + return res.send('Failed to fetch secrets from the database.'); + } + + const secrets = secretsList.reduce((prev, cur) => ({ + ...prev, + [cur.key]: cur.value + }), {}); + console.info(`${chalk.green(req.path)}, running endpoint "${chalk.yellow(endpoint.name)}".`); const {code} = endpoint; try { const func = requireFromString(code); - func(req, res); + func(req, res, secrets); } catch (err) { res.status(500); console.error(chalk.red(`Error in endpoint ${endpoint.name}! Details below.`)); @@ -2,7 +2,11 @@ module.exports = db => { const endpoints = db.get('endpoints'); endpoints.createIndex({name: 1}, {unique: true}); + const secrets = db.get('secrets'); + secrets.createIndex({key: 1}, {unique: true}); + return { - endpoints + endpoints, + secrets }; }; |
