-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (104 loc) · 3.5 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* https://github.com/vert-x3/vertx-service-discovery/blob/master/vertx-service-discovery/src/main/java/io/vertx/servicediscovery/rest/ServiceDiscoveryRestEndpoint.java
* ```java
* ServiceDiscoveryRestEndpoint.create(router, discovery);
* ```
*
* -> then you can call `/discovery` on each Vert.x microservice
* eg: http://localhost:8081/discovery -> get the list of the microservices
*/
const express = require("express");
const bodyParser = require("body-parser");
const fetch = require('node-fetch');
const uuidv1 = require('uuid/v1');
let port = process.env.PORT || 8888;
let app = express();
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
let service = {
record: {
name: "--=== FAKE-RAIDER ===--",
status: "UP",
type: "http-endpoint",
location: {
endpoint: "http://localhost:8888/api",
host: "localhost",
port: 8888,
root: "/api",
ssl: false
},
metadata: {
kind: "raider",
message: "ready to fight",
basestar: null,
coordinates: {
x: 100.0, y: 100.0
}
}
}
}
// get the list of the microservices
fetch(`${process.env.DISCOVERY || "http://localhost:8080/discovery"}`, {
method:'GET', headers: {"Content-Type": "application/json;charset=UTF-8"}
})
.then(response => {
return response.json();
})
.then(jsonData => {
let basestars = jsonData.filter(item => item.metadata.kind == "basestar"); // to do: make a filter
console.log("🤖 basestars: ", basestars)
// time to register
fetch(`${process.env.DISCOVERY || "http://localhost:8080/discovery"}`, {
method:'POST', headers: {"Content-Type": "application/json;charset=UTF-8"},
body: JSON.stringify(service.record)
})
.then(response => response.json())
.then(jsonData => {
let registration = jsonData.registration
console.log("😀 service registered, registration: ", registration)
app.post('/api/coordinates', (req, res) => {
let data = req.body
let selectedBaseStar = basestars[0]
service.record.registration = registration
service.record.metadata.coordinates = {
x: data.x, y: data.y, xVelocity: data.xVelocity, yVelocity: data.yVelocity
}
service.record.metadata.basestar = {
name:selectedBaseStar.name,
color:selectedBaseStar.metadata.color
}
console.log("🚀 record", service.record)
fetch(`http://localhost:8080/discovery/${registration}`, {
method:'PUT',
headers: {"Content-Type": "application/json;charset=UTF-8"},
body: JSON.stringify(service.record)
})
.then(response => res.send({messsage: "Hello 🌍, I'm the fake 🚀"}))
.catch(error => {
console.log("😡 talking to the basestar: ", error)
});
})
// start the microservice
app.listen(port)
console.log("🌍 Discovery Server is started - listening on ", port)
let selectedBaseStar = basestars[0]
fetch(`${selectedBaseStar.location.endpoint || "http://localhost:8080/api"}/raiders`, {
method:'POST',
headers: {"Content-Type": "application/json;charset=UTF-8"},
body: JSON.stringify({
registration: registration
})
})
.then(response => response.json())
.then(jsonData => console.log("😀 from 🚀: ", jsonData))
.catch(error => {
console.log("😡 talking to the basestar: ", error)
});
})
.catch(error => {
console.log("😡 registering: ", error)
});
})
.catch(error => {
console.log("😡 fetching services: ", error)
});