代码拉取完成,页面将自动刷新
/*
* SPDX-License-Identifier: Apache-2.0
*/
/**
*
* Created by shouhewu on 6/8/17.
*
*/
const express = require('express');
const helmet = require('helmet');
const path = require('path');
const http = require('http');
const https = require('https');
const fs = require('fs');
const url = require('url');
const WebSocket = require('ws');
const appconfig = require('./appconfig.json');
const helper = require('./app/common/helper');
const logger = helper.getLogger('main');
const Explorer = require('./app/Explorer');
const ExplorerError = require('./app/common/ExplorerError');
const sslEnabled = process.env.SSL_ENABLED || appconfig.sslEnabled;
const sslCertsPath = process.env.SSL_CERTS_PATH || appconfig.sslCertsPath;
const host = process.env.HOST || appconfig.host;
const port = process.env.PORT || appconfig.port;
const protocol = sslEnabled ? 'https' : 'http';
/**
*
*
* @class Broadcaster
* @extends {WebSocket.Server}
*/
class Broadcaster extends WebSocket.Server {
/**
* Creates an instance of Broadcaster.
* @param {*} server
* @memberof Broadcaster
*/
constructor(server) {
super({
server
});
this.on('connection', function connection(ws, req) {
const location = url.parse(req.url, true);
this.on('message', message => {
logger.info('received: %s, %s', location, message);
});
});
}
/**
*
*
* @param {*} data
* @memberof Broadcaster
*/
broadcast(data) {
this.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
logger.debug('Broadcast >> %j', data);
client.send(JSON.stringify(data));
}
});
}
}
let server;
let explorer;
async function startExplorer() {
explorer = new Explorer();
// Application headers
explorer.getApp().use(helmet());
explorer.getApp().use(helmet.xssFilter());
explorer.getApp().use(helmet.hidePoweredBy());
explorer.getApp().use(helmet.referrerPolicy());
explorer.getApp().use(helmet.noSniff());
/* eslint-disable */
explorer.getApp().use(helmet.frameguard({ action: 'SAMEORIGIN' }));
explorer.getApp().use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
objectSrc: ["'self'"],
frameSrc: ["'self'"],
fontSrc: ["'self'"],
imgSrc: ["'self' data: https:; "]
}
})
);
/* eslint-enable */
// Application headers
// = =========== web socket ==============//
const sslPath = path.join(__dirname, sslCertsPath);
logger.debug(sslEnabled, sslCertsPath, sslPath);
if (sslEnabled) {
const options = {
key: fs.readFileSync(sslPath + '/privatekey.pem').toString(),
cert: fs.readFileSync(sslPath + '/certificate.pem').toString()
};
server = https.createServer(options, explorer.getApp());
} else {
server = http.createServer(explorer.getApp());
}
const broadcaster = new Broadcaster(server);
await explorer.initialize(broadcaster);
explorer.getApp().use(express.static(path.join(__dirname, 'client/build')));
// ============= start server =======================
console.log("----- 3");
server.listen(port, () => {
logger.info(
`Please open web browser to access :${protocol}://${host}:${port}/`
);
logger.info(`pid is ${process.pid}`);
});
console.log("----- 4");
}
startExplorer();
/* eslint-disable */
let connections = [];
server.on('connection', connection => {
connections.push(connection);
connection.on(
'close',
() => (connections = connections.filter(curr => curr !== connection))
);
});
/* eslint-enable */
/*
* This function is called when you want the server to die gracefully
* i.e. wait for existing connections
*/
const shutDown = function(exitCode) {
console.log("shut down ----------- ");
logger.info('Received kill signal, shutting down gracefully');
server.close(() => {
explorer.close();
logger.info('Closed out connections');
process.exit(exitCode);
});
setTimeout(() => {
logger.error('Could not close connections in time, forcefully shutting down');
explorer.close();
process.exit(1);
}, 10000);
connections.forEach(curr => curr.end());
setTimeout(() => connections.forEach(curr => curr.destroy()), 5000);
};
process.on('unhandledRejection', up => {
console.log("--- 1");
console.log(up);
logger.error(
'<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>'
);
if (up instanceof ExplorerError) {
logger.error('Error : ', up.message);
} else {
logger.error(up);
}
setTimeout(() => {
shutDown(1);
}, 2000);
});
process.on('uncaughtException', up => {
console.log("---- 2");
console.log(up);
logger.error(
'<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>'
);
if (up instanceof ExplorerError) {
logger.error('Error : ', up.message);
} else {
logger.error(up);
}
setTimeout(() => {
shutDown(1);
}, 2000);
});
// Listen for TERM signal .e.g. kill
process.on('SIGTERM', shutDown);
// Listen for INT signal e.g. Ctrl-C
process.on('SIGINT', shutDown);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。