-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
62 lines (61 loc) · 1.97 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
var express = require("express");
var fetch = require("node-fetch");
var bodyParser = require('body-parser');
var argv = process.argv.slice(2);
var port = 8080;
const PORT_FLAG = "--port=";
const ADAPTER_FLAG = "--adapter=";
var settings = [];
argv.forEach(arg => {
if (arg.startsWith(PORT_FLAG)) {
port = parseInt(arg.substr(PORT_FLAG.length));
return;
}
if (arg.startsWith(ADAPTER_FLAG)) {
var noflag = arg.substr(ADAPTER_FLAG.length)
var index1 = noflag.indexOf('=');
var index2 = noflag.indexOf('=', index1 + 1)
var js = require(noflag.substring(0, index1));
settings.push(
{
from: noflag.substring(index1 + 1, index2),
to: noflag.substring(index2 + 1),
template: js.template,
signUrl: js.signUrl || (url => url)
}
);
}
});
var app = express();
app.use(bodyParser.json());
settings.forEach(
function (e) {
var pattern = new RegExp(e.from);
app.use(
function (req, res, next) {
if (!pattern.test(req.url)) {
return next();
}
var to = e.signUrl(e.to);
console.debug(`${req.url}=>${to}`);
fetch(to,
{
method: "POST",
body: JSON.stringify(e.template(req.body)),
headers: {
'Content-Type': "application/json"
}
}).then(
res => res.text()
).then(
text => console.debug(text)
).catch(
error => console.warn(`${req.url}=>${to}:${error}`)
).finally(
() => res.send("ok")
);
}
)
}
)
app.listen(port);