From d965ac8bb00e443c6c9ce72674b72edcaa3e9ecc Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 12 Jul 2018 15:23:25 +0300 Subject: Implement secrets --- index.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'index.js') diff --git a/index.js b/index.js index 2c7a373..558d7d0 100644 --- a/index.js +++ b/index.js @@ -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.`)); -- cgit v1.3