feat: Implemented server mode

This commit is contained in:
Davide Passoni
2024-12-16 17:24:02 +01:00
parent 032b74b57b
commit c2d5d4ea17
20 changed files with 389 additions and 222 deletions

View File

@@ -7,7 +7,7 @@
{
"type": "node",
"request": "launch",
"name": "Launch Server (DCS)",
"name": "Launch server (core only)",
"skipFiles": [
"<node_internals>/**"
],
@@ -26,75 +26,25 @@
"--vite"
],
"restart": true
},
},
{
"type": "node",
"request": "launch",
"name": "Launch Server (No DCS)",
"name": "Launch server (Electron)",
"skipFiles": [
"<node_internals>/**"
],
"runtimeExecutable": "nodemon",
"runtimeExecutable": "npm",
"runtimeArgs": [
"--watch",
"src/**/*.ts",
"--exec",
"node",
"--inspect",
"-r",
"ts-node/register",
"src/www.ts",
"run",
"server",
"--",
"-s",
"-c",
"${input:enterDir}/Config/olympus.json",
"--vite"
],
"restart": true,
"preLaunchTask": "demo-server"
},
{
"type": "node",
"request": "launch",
"name": "Launch Server static Vite (DCS)",
"skipFiles": [
"<node_internals>/**"
],
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--watch",
"src/**/*.ts",
"--exec",
"node",
"--inspect",
"-r",
"ts-node/register",
"src/www.ts",
"-c",
"${input:enterDir}/Config/olympus.json"
],
"restart": true
},
{
"type": "node",
"request": "launch",
"name": "Launch Server static Vite (No DCS)",
"skipFiles": [
"<node_internals>/**"
],
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--watch",
"src/**/*.ts",
"--exec",
"node",
"--inspect",
"-r",
"ts-node/register",
"src/www.ts",
"-c",
"${input:enterDir}/Config/olympus.json"
],
"restart": true,
"preLaunchTask": "demo-server"
}
],
"inputs": [

View File

@@ -1,57 +1,79 @@
const { app, BrowserWindow } = require('electron/main')
const path = require('path')
const fs = require('fs')
const { spawn } = require('child_process');
const yargs = require('yargs');
const { app, BrowserWindow } = require("electron/main");
const path = require("path");
const fs = require("fs");
const { spawn } = require("child_process");
const yargs = require("yargs");
yargs.alias('c', 'config').describe('c', 'olympus.json config location').string('rp');
yargs
.alias("c", "config")
.describe("c", "olympus.json config location")
.string("rp");
yargs.alias("s", "server").describe("s", "run in server mode").string("rp");
args = yargs.argv;
console.log(`Config location: ${args["config"]}`)
console.log(`Config location: ${args["config"]}`);
var frontendPort = 3000;
var serverMode = args["server"] ?? false;
if (fs.existsSync(args["config"])) {
var json = JSON.parse(fs.readFileSync(args["config"], 'utf-8'));
frontendPort = json["frontend"]["port"];
var json = JSON.parse(fs.readFileSync(args["config"], "utf-8"));
frontendPort = json["frontend"]["port"];
} else {
console.log("Failed to read config, trying default port");
console.log("Failed to read config, trying default port");
}
function createWindow() {
const win = new BrowserWindow({
icon: "./../img/olympus.ico"
})
const win = new BrowserWindow({
icon: "./../img/olympus.ico",
});
win.loadURL(`http://localhost:${frontendPort}`);
win.setMenuBarVisibility(false);
win.maximize();
win.loadURL(
`http://localhost:${frontendPort}${serverMode ? "/?server" : ""}`
);
win.setMenuBarVisibility(false);
if (serverMode) {
} else {
win.maximize();
}
}
app.whenReady().then(() => {
const server = spawn('node', [path.join('.', 'build', 'www.js'), "--config", args["config"]]);
app
.whenReady()
.then(() => {
const server = spawn("node", [
path.join(".", "build", "www.js"),
"--config",
args["config"],
"--vite",
]);
server.stdout.on('data', (data) => {
console.log(`${data}`);
});
server.stdout.on("data", (data) => {
console.log(`${data}`);
});
server.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
server.stderr.on("data", (data) => {
console.error(`stderr: ${data}`);
});
server.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
});
server.on("close", (code) => {
console.log(`Child process exited with code ${code}`);
});
createWindow()
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
})
.catch((err) => {
console.error(err);
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});

View File

@@ -4,7 +4,7 @@
"version": "{{OLYMPUS_VERSION_NUMBER}}",
"scripts": {
"build-release": "call ./scripts/build-release.bat",
"server": "node ./build/www.js",
"server": "electron .",
"client": "electron .",
"tsc": "tsc"
},

View File

@@ -11,7 +11,7 @@ const generateClient = new textToSpeech.TextToSpeechClient();
module.exports = function () {
router.put("/generate", (req, res, next) => {
const request = {
input: {text: req.body.text},
input: {ssml: `<speak>${req.body.text}</speak>`},
voice: {languageCode: 'en-US', ssmlGender: 'MALE'},
audioConfig: {audioEncoding: 'MP3'},
};

View File

@@ -23,13 +23,20 @@ module.exports = function (configLocation) {
}
if (fs.existsSync(configLocation)) {
let rawdata = fs.readFileSync(configLocation, "utf-8");
const local = ["127.0.0.1", "::ffff:127.0.0.1", "::1"].includes(req.connection.remoteAddress);
const config = JSON.parse(rawdata);
let resConfig = {
frontend: { ...config.frontend },
audio: { ...(config.audio ?? {}) },
controllers: { ...(config.controllers ?? {}) },
profiles: { ...(profiles ?? {}) },
local: local,
};
if (local) {
resConfig["authentication"] = config["authentication"]
}
res.send(
JSON.stringify({
frontend: { ...config.frontend },
audio: { ...(config.audio ?? {}) },
profiles: { ...(profiles ?? {}) },
})
JSON.stringify(resConfig)
);
res.end();
} else {