Skip to content

Commit

Permalink
Merge pull request #12 from AndresMpa/client
Browse files Browse the repository at this point in the history
Base url fix
  • Loading branch information
xcerock authored May 31, 2024
2 parents 77d7dd8 + aa2e49e commit 807ae36
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 81 deletions.
18 changes: 18 additions & 0 deletions client/OS/baseLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const axios = require("axios");

const config = require("../utils/config");

const baseUrlLoader = async () => {
let lyraBaseUrl;
if (config.nodeEnv === "production") {
const baseUrl = `${config.host}:${config.apiPort}`;
lyraBaseUrl = baseUrl;
} else {
const response = await axios.get(config.lyraUrl);
lyraBaseUrl = response.data;
}

return lyraBaseUrl;
};

module.exports = { baseUrlLoader };
3 changes: 2 additions & 1 deletion client/OS/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const diskData = await window.electron.getDiskUsage();
const ramData = await window.electron.getRAMUsage();
const cpuData = await window.electron.getCPUUsage();
const baseUrl = await window.electron.getBaseUrl();

export { cpuData, ramData, diskData };
export { cpuData, ramData, diskData, baseUrl };
50 changes: 0 additions & 50 deletions client/OS/plots.js

This file was deleted.

4 changes: 0 additions & 4 deletions client/config/default.json

This file was deleted.

7 changes: 3 additions & 4 deletions client/eventHandlers/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
saveArrayToStorage,
getItemFromStorage,
} from "../utils/storageHandler.js";
import { baseUrl } from "../OS/main.js";

const promptChat = document.querySelector("#promptChat");

Expand Down Expand Up @@ -38,16 +39,14 @@ const createMessage = (content, messageType) => {
};

const askLyra = async (entry) => {
console.log(entry);

const rawData = await fetch(`${window.lyraBaseUrl}/chat`, {
const rawData = await fetch(`${baseUrl}/chat`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ user_input: entry }),
})
.then((response) => response.json())
.then((data) => data.response)
.catch((error) => console.error("Error en la solicitud fetch:", error));
.catch((error) => console.error("Error on fetch request: ", error));

return rawData;
};
Expand Down
24 changes: 13 additions & 11 deletions client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const config = require("./utils/config");
const { getRAMUsage } = require("./OS/RAM");
const { getCPUUsage } = require("./OS/CPU");
const { getDiskUsage } = require("./OS/Disk");
const { baseUrlLoader } = require("./OS/baseLoader");

function createWindow() {
const { height } = screen.getPrimaryDisplay().workAreaSize;
Expand All @@ -35,6 +36,17 @@ function createWindow() {

if (config.nodeEnv === "production") {
Menu.setApplicationMenu(null);

const pythonServer = spawn("python", [
"-u",
path.join(__dirname, "..", "backend", "app.py"),
]);

pythonServer.stdout.on("data", (data) => console.log(`stdout: ${data}`));
pythonServer.stderr.on("data", (data) => console.error(`stderr: ${data}`));
pythonServer.on("close", (code) =>
console.log(`Python server exited with code ${code}`),
);
}

if (config.nodeEnv === "development") {
Expand All @@ -54,17 +66,6 @@ function createWindow() {
});
}

const pythonServer = spawn("python", [
"-u",
path.join(__dirname, "..", "backend", "app.py"),
]);

pythonServer.stdout.on("data", (data) => console.log(`stdout: ${data}`));
pythonServer.stderr.on("data", (data) => console.error(`stderr: ${data}`));
pythonServer.on("close", (code) =>
console.log(`Python server exited with code ${code}`),
);

app.whenReady().then(() => {
createWindow();

Expand All @@ -86,3 +87,4 @@ app.on("window-all-closed", () => {
ipcMain.handle("get-ram-usage", async () => await getRAMUsage());
ipcMain.handle("get-cpu-usage", async () => await getCPUUsage());
ipcMain.handle("get-disk-usage", async () => await getDiskUsage());
ipcMain.handle("get-base-url", async () => await baseUrlLoader());
12 changes: 3 additions & 9 deletions client/preloader/preload.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const { contextBridge, ipcRenderer } = require("electron");
const axios = require("axios");

const config = require("../utils/config");

contextBridge.exposeInMainWorld("electron", {
getBaseUrl: () => ipcRenderer.invoke("get-base-url"),
getCPUUsage: () => ipcRenderer.invoke("get-cpu-usage"),
getDiskUsage: () => ipcRenderer.invoke("get-disk-usage"),
getRAMUsage: () => ipcRenderer.invoke("get-ram-usage"),
getDiskUsage: () => ipcRenderer.invoke("get-disk-usage"),
});

const attachContextData = () => {
Expand All @@ -20,9 +19,4 @@ const attachContextData = () => {
}
};

window.addEventListener("DOMContentLoaded", async () => {
const response = await axios.get(config.lyraUrl);
window.lyraBaseUrl = response.data;

attachContextData();
});
window.addEventListener("DOMContentLoaded", () => attachContextData());
2 changes: 0 additions & 2 deletions client/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ const fs = require("fs");
const os = require("os");
const path = require("path");

const defaultConfigPath = path.join(__dirname, "config", "default.json");
const customConfigPaths = [
path.join(os.homedir(), ".config", "lyra", "config.json"),
path.join(os.homedir(), ".lyra", "config.json"),
];

const loadConfig = () => {
let configPath = defaultConfigPath;
let customPath;

for (customPath of customConfigPaths) {
Expand Down

0 comments on commit 807ae36

Please sign in to comment.