-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (63 loc) · 1.81 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
const bodyParser = require('body-parser');
const express = require('express')
const request = require('request')
const inventory = require('./lib/inventory')
const inventoryURL = 'http://apis.docker:8080/DSProductInventory/api/productInventory/v2/hub'
const callbackURL = 'http://bae.iota.docker:3000/inventory'
const port = 3000;
// Initialize express routes
const app = express()
app.use(bodyParser.json());
app.post('/inventory', inventory.notificationHandler)
const init = () => {
app.listen(port, () => {
console.log(`Server running at ${port}/`);
});
}
const makeSubcription = () => {
let data = {
callback: callbackURL
};
let headers = {
'content-type': 'application/json'
};
let req = {
method: 'POST',
url: inventoryURL,
headers: headers,
body: JSON.stringify(data)
};
request(req, (err, response) => {
if (!err && (response.statusCode == 201 || response.statusCode == 409)) {
init()
}
});
}
const createInventorySubscription = () => {
request.get(inventoryURL, (err, response, body) => {
if (response.statusCode === 200) {
let hubs = JSON.parse(body);
if (hubs.filter((x) => x.callback === callbackURL).length > 0) {
init()
} else {
makeSubcription()
}
} else {
console.log('Error accessing to inventory data')
}
})
}
createInventorySubscription();
/*const keyGen = length => {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9'
let key = ''
while (key.length < length) {
let byte = crypto.randomBytes(1)
if (byte[0] < 243) {
key += charset.charAt(byte[0] % 27)
}
}
return key
}
seed = keyGen(81);
console.log('SEED: ' + seed)*/