forked from hyperledger-labs/blockchain-explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
101 lines (88 loc) · 2.87 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
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
/*
SPDX-License-Identifier: Apache-2.0
*/
/**
*
* Created by shouhewu on 6/8/17.
*
*/
var http = require("http");
var url = require("url");
var WebSocket = require("ws");
var Explorer = require("./app/explorer/Explorer.js")
var appconfig = require("./appconfig.json");
var helper = require('./app/helper.js')
var logger = helper.getLogger("main");
var express = require("express");
var path = require("path");
var pgservice = require('./app/persistance/postgreSQL/db/pgservice.js');
var host = process.env.HOST || appconfig.host;
var port = process.env.PORT || appconfig.port;
class Broadcaster extends WebSocket.Server {
constructor(server) {
super({ server });
this.on("connection", function connection(ws, req) {
const location = url.parse(req.url, true);
this.on("message", function incoming(message) {
console.log("received: %s", message);
});
});
}
broadcast(data) {
this.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
}
}
var server;
async function startExplorer() {
var explorer = new Explorer();
//============ web socket ==============//
server = http.createServer(explorer.getApp());
var broadcaster = new Broadcaster(server);
await explorer.initialize(broadcaster);
explorer.getApp().use(express.static(path.join(__dirname, "client/build")));
logger.info(
"Please set logger.setLevel to DEBUG in ./app/helper.js to log the debugging."
);
// ============= start server =======================
server.listen(port, function () {
console.log('\n')
console.log(`Please open web browser to access :http://${host}:${port}/`);
console.log('\n')
console.log('pid is ' + process.pid)
console.log('\n')
});
}
startExplorer();
let connections = [];
server.on('connection', connection => {
connections.push(connection);
connection.on('close', () => connections = connections.filter(curr => curr !== connection));
});
// this function is called when you want the server to die gracefully
// i.e. wait for existing connections
var shutDown = function () {
console.log('Received kill signal, shutting down gracefully');
server.close(() => {
console.log('Closed out remaining connections');
pgservice.closeconnection();
process.exit(0);
});
setTimeout(() => {
console.error('Could not close connections in time, forcefully shutting down');
pgservice.closeconnection();
process.exit(1);
}, 10000);
connections.forEach(curr => curr.end());
setTimeout(() => connections.forEach(curr => curr.destroy()), 5000);
}
// listen for TERM signal .e.g. kill
process.on('SIGTERM', shutDown);
// listen for INT signal e.g. Ctrl-C
process.on('SIGINT', shutDown);
/*setInterval(() => server.getConnections(
(err, connections) => console.log(`${connections} connections currently open`)
), 1000);*/