diff --git a/.gitignore b/.gitignore index 08a5bd6..4d646e4 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,6 @@ backend/video/segments downloads *.exe -backend/lisa \ No newline at end of file +backend/lisa + +env/ \ No newline at end of file diff --git a/backend/LiSA.py b/backend/LiSA.py index de7cfe3..77d1eb8 100644 --- a/backend/LiSA.py +++ b/backend/LiSA.py @@ -1,7 +1,7 @@ import logging from video.downloader.msg_system import MsgSystem from threading import Thread -from sys import stdout +from sys import stdout, argv import asyncio from utils import DB from config import ServerConfig, parse_config_json, update_environ, FileConfig @@ -14,7 +14,9 @@ def run_api_server(port: int = 8000): ServerConfig.API_SERVER_ADDRESS = f"http://localhost:{port}" - print(f"server started on port: {port} \n You can access API SERVER on {ServerConfig.API_SERVER_ADDRESS}") + print( + f"server started on port: {port} \n You can access API SERVER on {ServerConfig.API_SERVER_ADDRESS}" + ) start_api_server(port=port) @@ -34,7 +36,10 @@ def run_api_server(port: int = 8000): parse_config_json(FileConfig.CONFIG_JSON_PATH) update_environ() - t1 = Thread(target=run_api_server, args=(6969, )) + # For your development cosmic, you don't need to change your testing port number + PORT = int(argv[1]) if (len(argv) >= 2 and argv[1]) else 6969 + + t1 = Thread(target=run_api_server, args=(PORT,)) t1.daemon = True t1.start() diff --git a/main.js b/main.js index 74ede9c..228db08 100644 --- a/main.js +++ b/main.js @@ -70,7 +70,8 @@ const createMainWindow = () => { // executeOnWindow(isPageLoaded, handleLoad); if (isDevMode) { - mainWindow.loadURL("http://localhost:3967"); + const REACT_PORT = process.argv[2]; + mainWindow.loadURL(`http://localhost:${REACT_PORT}`); mainWindow.hide(); @@ -174,7 +175,8 @@ app.whenReady().then(async () => { // shell: true, // stdio: "inherit", // }); - var devProc = spawn("python backend/LiSA.py", { + const PYTHON_SERVER = process.argv[3]; + var devProc = spawn(`python backend/LiSA.py ${PYTHON_SERVER}`, { detached: true, shell: true, }); @@ -259,22 +261,22 @@ app.whenReady().then(async () => { // // spawn(`powershell.exe -Command kill ${devProc.pid}`); - // devProc.kill("SIGHUP"); + // devProc.kill(); // psTree(devProc.pid, function (err, children) { - // console.log(`asdasdasdasdasd`) - // console.log(err) - // console.log(children) - // devProc.spawn( - // "kill", - // ["-9"].concat( - // children.map(function (p) { - // console.log(`inside map child`) - // console.log(children) - // return p.PID; - // }) - // ) - // ); + // console.log(`asdasdasdasdasd`) + // console.log(err) + // console.log(children) + // devProc.spawn( + // "kill", + // ["-9"].concat( + // children.map(function (p) { + // console.log(`inside map child`) + // console.log(children) + // return p.PID; + // }) + // ) + // ); // }); spawn("taskkill /IM LiSA.exe /F", { diff --git a/scripts/start.js b/scripts/start.js index 4220825..e9ab85e 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -1,6 +1,8 @@ const { spawn, spawnSync } = require("child_process"); const { get } = require("axios"); +const { getRandomPort } = require('../utilities/getRandomPort') + /** * @namespace Starter * @description - Scripts to start Electron, React, and Python. @@ -30,9 +32,13 @@ class Starter { // spawnSync('npx kill-port 3000', spawnOptions.hideLogs); // Start & identify React & Electron processes - spawn("cross-env BROWSER=none react-scripts start", spawnOptions.showLogs); - spawn("electron .", spawnOptions.showLogs); + const { port: REACT_PORT = 3967 } = await getRandomPort(); + const { port: PYTHON_SERVER = 6969 } = await getRandomPort(); + // console.log(REACT_PORT, PYTHON_SERVER); + + spawn(`cross-env BROWSER=none PORT=${REACT_PORT} REACT_APP_SERVER_URL=http://localhost:${PYTHON_SERVER} react-scripts start `, spawnOptions.showLogs); + spawn(`npx electron main ${REACT_PORT} ${PYTHON_SERVER}`, spawnOptions.showLogs); // Kill processes on exit const exitOnEvent = (event) => { process.once(event, () => { @@ -41,7 +47,7 @@ class Starter { const expectedErrors = ["ECONNRESET", "ECONNREFUSED"]; // Send command to Flask server to quit and close - get(`http://localhost:3967/quit`).catch( + get(`http://localhost:${REACT_PORT}/quit`).catch( (error) => !expectedErrors.includes(error.code) && console.log(error) ); diff --git a/src/actions/animeActions.js b/src/actions/animeActions.js index d98483d..303cd4a 100644 --- a/src/actions/animeActions.js +++ b/src/actions/animeActions.js @@ -40,7 +40,7 @@ export const searchAnimeList = (query) => async (dispatch) => { dispatch({ type: ANIME_SEARCH_REQUEST, payload: {} }); - const { data } = await server.get(`/search?anime=${query}`); + const { data } = await server.get(`/search?type=anime&query=${query}`); dispatch({ type: ANIME_SEARCH_SUCCESS, payload: data }); } catch (error) { dispatch({ type: ANIME_SEARCH_FAIL, payload: error.response.data }); diff --git a/utilities/getRandomPort.js b/utilities/getRandomPort.js new file mode 100644 index 0000000..db12065 --- /dev/null +++ b/utilities/getRandomPort.js @@ -0,0 +1,31 @@ +const net = require('net'); + +module.exports.getRandomPort = async (callback) => { + return new Promise((resolve, reject) => { + const server = net.createServer(); + + server.on('error', (err) => { + callback({ err }); + resolve({ err }) + }); + + server.on('listening', () => { + const port = server.address().port; + server.close(() => { + // console.log(port); + // callback({ port }); + resolve({ port }) + }); + }); + + server.listen(); + }) +} + +// getRandomPort((err, port) => { +// if (err) { +// console.error('Error finding available port:', err); +// } else { +// console.log('Available port:', port); +// } +// }); \ No newline at end of file