-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxy-server.js
110 lines (93 loc) · 2.9 KB
/
proxy-server.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require('express');
const bodyParser = require('body-parser');
const gremlin = require('gremlin');
const cors = require('cors');
const path = require('path');
const app = express();
const port = 3001;
app.use(cors({
credentials: true,
}));
// parse application/json
app.use(bodyParser.json());
function mapToObj(inputMap) {
let obj = {};
inputMap.forEach((value, key) => {
obj[key] = value
});
return obj;
}
function edgesToJson(edgeList) {
return edgeList.map(
edge => ({
id: typeof edge.get('id') !== "string" ? JSON.stringify(edge.get('id')) : edge.get('id'),
from: edge.get('from'),
to: edge.get('to'),
label: edge.get('label'),
properties: mapToObj(edge.get('properties')),
})
);
}
function nodesToJson(nodeList) {
return nodeList.map(
node => ({
id: node.get('id'),
label: node.get('label'),
properties: mapToObj(node.get('properties')),
edges: edgesToJson(node.get('edges'))
})
);
}
function makeQuery(query, nodeLimit) {
const nodeLimitQuery = !isNaN(nodeLimit) && Number(nodeLimit) > 0 ? `.limit(${nodeLimit})`: '';
return `${query}${nodeLimitQuery}
.dedup()
.as('node')
.project('id', 'label', 'properties', 'edges')
.by(__.id())
.by(__.label())
.by(__.valueMap())
.by(__.outE()
.project('id', 'from', 'to', 'label', 'properties')
.by(__.id())
.by(__.select('node').id())
.by(__.inV().id())
.by(__.label())
.by(__.valueMap())
.fold()
)`;
}
app.post('/query', (req, res, next) => {
const gremlinHost = req.body.host;
const gremlinPort = req.body.port;
const nodeLimit = req.body.nodeLimit;
const query = req.body.query;
const traversalSource = req.body.traversalSource;
const client = new gremlin.driver.Client(`ws://${gremlinHost}:${gremlinPort}/gremlin`, { traversalSource: traversalSource, mimeType: 'application/json' });
client.submit(makeQuery(query, nodeLimit), {})
.then((result) => res.send(nodesToJson(result._items)))
.catch((err) => next(err));
});
app.get('/settings', (_, res) => {
return res.json({
GREMLIN_HOST: firstNotNull(process.env.GREMLIN_HOST, 'localhost'),
GREMLIN_PORT: firstNotNull(process.env.GREMLIN_PORT, '8182'),
GREMLIN_TRAVERSAL_SOURCE: firstNotNull(process.env.GREMLIN_TRAVERSAL_SOURCE, 'g'),
GREMLIN_DEFAULT_QUERY: firstNotNull(process.env.GREMLIN_DEFAULT_QUERY, 'g.V()'),
});
});
// Hosting react app in express
// https://create-react-app.dev/docs/deployment#other-solutions
app.use(express.static(path.join(__dirname, 'frontend')));
app.get('/', function (_, res) {
res.sendFile(path.join(__dirname, 'frontend', 'index.html'));
});
app.listen(port, () => console.log(`Simple gremlin-proxy server listening on port ${port}!`));
function firstNotNull() {
for (let i = 0; i < arguments.length; i++) {
const arg = arguments[i];
if (arg) {
return arg;
}
}
}