-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
51 lines (40 loc) · 1.23 KB
/
main.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
var express = require('express')
var app = express()
var host;
var server = app.listen(8000, function () {
console.log('app listening on port 8000!')
host = server.address().address;
console.log('running at http://' + host )
console.log(server.address());
console.log(host);
})
app.get("/ping", (req, res) => {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
res.json({"message": "ping from " + host });
});
var registry = require('etcd-registry');
var services = registry('etcd-2:2379,etcd-1:2379');
var CheckRegister = setInterval(function(){
console.log('setInterval services.lookup check: ');
services.lookup('ping', function(err, service) {
if(service == undefined) {
console.log("service not registered");
console.log(service);
services.join('ping', {name: 'pingservice',port:8000,url:"http://"+host+"ping" },function(err, service) {
console.log('services.join: ');
});
}
else {
console.log("service registered");
console.log(service);
clearInterval(CheckRegister);
}
});
}, 1500)
app.get("/list", (req, res) => {
services.list('', function(err, service) {
console.log('services.list:');
console.log(service);
res.json(service);
});
});