-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (43 loc) · 1.35 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
require('signalfx-tracing').init({
service: 'rxjs-fiddle-backend',
url: `http://${process.env.SIGNALFX_AGENT_HOST}:9080/v1/trace`,
});
const express = require("express");
const cors = require("cors");
const { omit } = require('lodash');
const fetch = require('node-fetch');
const echo = require("./functions/echo");
const runFiddle = require("./functions/run-fiddle");
const functions = { echo, "run-fiddle": runFiddle };
const meta = {
env: process.env.ENVIRONMENT_NAME,
version: process.env.API_VERSION
};
const app = express();
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors());
app.use('/v2/trace', (req, res) => {
fetch('https://ingest.us0.signalfx.com/v2/trace', {
method: req.method,
body: JSON.stringify(req.body),
headers: {
...omit(req.headers, 'host', 'origin', 'referer'),
'X-SF-TOKEN': process.env.SIGNALFX_TOKEN
}
})
.then(traceResponse => traceResponse.json())
.then(traceJson => res.send(traceJson))
.catch(err => res.status(500).json({ error: err.message }))
})
Object.keys(functions).forEach(name => {
app.all(`/${name}`, (req, res) => {
const ret = functions[name]({ data: req.body });
if (ret instanceof Promise) {
ret.then(response => res.json({ response, meta }));
} else {
res.json({ response: ret, meta });
}
});
});
app.listen(port);