-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
96 lines (78 loc) · 2.16 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
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
const http = require('http')
const morgan = require('morgan')
const express = require('express')
const bodyParser = require('body-parser')
const superagent = require('superagent')
const port = (process.env.PORT || 3000)
const listenHost = (process.env.HOST || '0.0.0.0')
const registryHost = (process.env.REGISTRY || 'registry:4873')
const keyword = "node-red"
const url = "https://" + registryHost + "/-/all"
const catalogue = {
"name":"Ben's custom catalogue",
"updated_at": new Date().toISOString(),
"modules": [
// {
// "id": "@ben/ben-node-random",
// "version": "1.0.0",
// "description": "A node-red node that generates random numbers",
// "keywords": [
// "node-red",
// "random"
// ],
// "updated_at": "2020-09-21T18:37:50.673Z",
// "url": "https://flows.hardill.me.uk/node/ben-red-random"
// }
]
}
function update() {
//reset list
catalogue.modules = [];
superagent.get(url)
.end((err, res) => {
if (!err) {
const nodes = res.body;
var nodeNames = Object.keys(nodes);
const index = nodeNames.indexOf("_updated");
if (index > -1) {
nodeNames.splice(index, 1);
}
for (const node in nodeNames) {
var n = nodes[nodeNames[node]];
if (n.keywords.indexOf(keyword) != -1) {
var entry = {
id: n.name,
version: n["dist-tags"].latest,
description: n.description,
keywords: n.keywords,
updated_at: n.time.modified,
url: "https://" + registryHost + "/-/web/details/" + n.name
}
catalogue.modules.push(entry)
}
}
console.log(JSON.stringify(catalogue, null, 2));
} else {
console.log(err);
}
});
}
const app = express()
app.use(morgan("combined"))
app.use(bodyParser.json())
app.post('/update', (req, res, next) => {
const updateRequest = req.body
console.log(JSON.stringify(updateRequest,null, 2))
update()
res.status(200).send();
})
app.get('/catalogue.json', (req, res, next) => {
res.send(catalogue)
})
// app.head('/catalogue.json', (req,res,next) => {
// })
update()
const server = http.Server(app);
server.listen(port, listenHost, function(){
console.log('App listening on %s:%d!', listenHost, port);
});