fix system log in standalone

This commit is contained in:
Proddy
2023-02-26 16:43:56 +01:00
parent 471d18f7fa
commit 2ffa5fbe36
3 changed files with 14 additions and 39 deletions

View File

@@ -9,7 +9,6 @@ export default defineConfig(({ command, mode }) => {
if (mode === 'hosted') { if (mode === 'hosted') {
return { return {
// hosted, ignore all errors, output to dist // hosted, ignore all errors, output to dist
// plugins: [react({ plugins: [['@swc/plugin-styled-components', {}]] }), viteTsconfigPaths(), svgrPlugin()]
plugins: [react(), viteTsconfigPaths(), svgrPlugin(), visualizer({ gzipSize: true }) as PluginOption] plugins: [react(), viteTsconfigPaths(), svgrPlugin(), visualizer({ gzipSize: true }) as PluginOption]
}; };
} else { } else {
@@ -33,8 +32,12 @@ export default defineConfig(({ command, mode }) => {
'/api': { '/api': {
target: 'http://localhost:3080', target: 'http://localhost:3080',
changeOrigin: true, changeOrigin: true,
secure: false, secure: false
ws: true },
'/es': {
target: 'http://localhost:3080',
changeOrigin: true,
secure: false
} }
} }
} }

View File

@@ -2,20 +2,17 @@
"name": "api", "name": "api",
"version": "1.0.0", "version": "1.0.0",
"description": "mock api for EMS-ESP", "description": "mock api for EMS-ESP",
"author": "proddy",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"dev": "nodemon ./server.js localhost 3080", "dev": "nodemon ./server.js localhost 3080",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "proddy",
"license": "ISC",
"dependencies": { "dependencies": {
"@msgpack/msgpack": "^2.8.0", "@msgpack/msgpack": "^2.8.0",
"compression": "^1.7.4", "compression": "^1.7.4",
"express": "^4.18.2", "express": "^4.18.2",
"express-sse": "^0.5.3", "nodemon": "^2.0.20"
"nodemon": "^2.0.20",
"ws": "^8.12.1"
}, },
"packageManager": "yarn@3.4.1" "packageManager": "yarn@3.4.1"
} }

View File

@@ -1,20 +1,20 @@
const express = require('express'); const express = require('express');
const compression = require('compression'); const compression = require('compression');
const SSE = require('express-sse');
const path = require('path'); const path = require('path');
const msgpack = require('@msgpack/msgpack'); const msgpack = require('@msgpack/msgpack');
const WebSocket = require('ws');
// REST API // REST API
const rest_server = express(); const rest_server = express();
const port = 3080; const port = 3080;
const REST_ENDPOINT_ROOT = '/rest/';
rest_server.use(compression()); rest_server.use(compression());
rest_server.use(express.static(path.join(__dirname, '../interface/build'))); rest_server.use(express.static(path.join(__dirname, '../interface/build')));
rest_server.use(express.json()); rest_server.use(express.json());
// API endpoint // endpoints
const API_ENDPOINT_ROOT = '/api/'; const API_ENDPOINT_ROOT = '/api/';
const REST_ENDPOINT_ROOT = '/rest/';
const EVENTSOURCE_ENDPOINT_ROOT = '/es/';
// LOG // LOG
const LOG_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'logSettings'; const LOG_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'logSettings';
@@ -1371,32 +1371,10 @@ rest_server.get(GET_SCHEDULE_ENDPOINT, (req, res) => {
// start server // start server
const expressServer = rest_server.listen(port, () => const expressServer = rest_server.listen(port, () =>
console.log(`EMS-ESP REST API server running on http://localhost:${port}/api`) console.log(`EMS-ESP REST API server running on http://localhost:${port}/`)
); );
// start websocket server // event source
const websocketServer = new WebSocket.Server({
noServer: true,
path: '/ws'
});
console.log('WebSocket server is listening to /ws');
expressServer.on('upgrade', (request, socket, head) => {
websocketServer.handleUpgrade(request, socket, head, (websocket) => {
websocketServer.emit('connection', websocket, request);
});
});
websocketServer.on('connection', function connection(websocketConnection, connectionRequest) {
const [_path, params] = connectionRequest?.url?.split('?');
console.log(params);
websocketConnection.on('message', (message) => {
const parsedMessage = JSON.parse(message);
console.log(parsedMessage);
});
});
var count = 8; var count = 8;
var log_index = 0; var log_index = 0;
const ES_ENDPOINT_ROOT = '/es/'; const ES_ENDPOINT_ROOT = '/es/';
@@ -1406,8 +1384,6 @@ rest_server.get(ES_LOG_ENDPOINT, function (req, res) {
res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Connection', 'keep-alive'); res.setHeader('Connection', 'keep-alive');
// res.setHeader('Content-Encoding', 'deflate')
// res.setHeader('X-Accel-Buffering', 'no')
res.flushHeaders(); res.flushHeaders();
var timer = setInterval(function () { var timer = setInterval(function () {
@@ -1421,7 +1397,6 @@ rest_server.get(ES_LOG_ENDPOINT, function (req, res) {
m: 'incoming message #' + count + '/' + log_index m: 'incoming message #' + count + '/' + log_index
}; };
const sseFormattedResponse = `data: ${JSON.stringify(data)}\n\n`; const sseFormattedResponse = `data: ${JSON.stringify(data)}\n\n`;
// console.log('sending log #' + count)
res.write(sseFormattedResponse); res.write(sseFormattedResponse);
res.flush(); // this is important res.flush(); // this is important