-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (58 loc) · 2.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require('dotenv').config();
const path = require('path');
const chalk = require('chalk');
if (!process.env.MONGO_URL) {
throw new Error('No MONGO_URL set in .env!');
}
const db = require('monk')(process.env.MONGO_URL);
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
const requireFromString = require('require-from-string');
const models = require('./models')(db);
const {installPackagesFromDatabase} = require('./npm')(models);
app.get('/', async (req, res) => {
const endpointObjects = await models.endpoints.find({});
const endpoints = endpointObjects.map(e => e.name);
res.render('frontpage', {
endpoints
});
});
app.all('/:path*', async (req, res) => {
const path = req.params.path.split('/')[0];
if (!path || path.length === 0) {
res.status(400);
return res.send('Empty endpoint.');
}
const endpoint = await models.endpoints.findOne({name: path});
if (!endpoint) {
res.status(400);
return res.send('No such endpoint.');
}
const secretsList = await models.secrets.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, secrets);
} catch (err) {
res.status(500);
console.error(chalk.red(`Error in endpoint ${endpoint.name}! Details below.`));
console.error(err);
res.send(err);
}
});
const packagePollInterval = process.env.PACKAGE_POLL_INTERVAL || 60000; // 1 minute
installPackagesFromDatabase(true);
setInterval(installPackagesFromDatabase, packagePollInterval);
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`${chalk.blue('faas')} listening on port ${port}! URL: ${chalk.yellow(`http://localhost:${port}`)}`));