From d3709f85b5e3ffd4f2c90cd165d13fe6e09a84d7 Mon Sep 17 00:00:00 2001 From: Jeffrey Morgan Date: Sun, 25 Jun 2023 00:30:02 -0400 Subject: [PATCH] build server into desktop app --- .gitignore | 4 ---- client/forge.config.ts | 1 + client/permissions-plugin.ts | 24 ------------------------ client/src/app.tsx | 2 +- client/src/index.ts | 34 +++++++++++++++++++++------------- client/webpack.plugins.ts | 9 --------- models/.gitignore | 1 + server/.gitignore | 5 +++++ server/build.py | 4 +--- server/server.py | 2 +- 10 files changed, 31 insertions(+), 55 deletions(-) delete mode 100644 client/permissions-plugin.ts create mode 100644 models/.gitignore create mode 100644 server/.gitignore diff --git a/.gitignore b/.gitignore index e6ff4765..94f1119e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,2 @@ .DS_Store .vscode -*.spec -*/build -*/dist -client/resources/server \ No newline at end of file diff --git a/client/forge.config.ts b/client/forge.config.ts index ace7d900..49e3ae00 100644 --- a/client/forge.config.ts +++ b/client/forge.config.ts @@ -13,6 +13,7 @@ const config: ForgeConfig = { packagerConfig: { asar: true, icon: './images/icon', + extraResource: ['../server/dist/server'], }, rebuildConfig: {}, makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})], diff --git a/client/permissions-plugin.ts b/client/permissions-plugin.ts deleted file mode 100644 index e65365a5..00000000 --- a/client/permissions-plugin.ts +++ /dev/null @@ -1,24 +0,0 @@ -import chmodr from 'chmodr' -import * as path from 'path' - -interface PluginOptions { - resourcePath: string -} - -class PermissionsPlugin { - options: PluginOptions - - constructor(options: PluginOptions) { - this.options = options - } - - apply(compiler: any) { - compiler.hooks.afterEmit.tap('PermissionsPlugin', () => { - chmodr(path.join(this.options.resourcePath), 0o755, err => { - // this fails on the first call to suppress the error - }) - }) - } -} - -export default PermissionsPlugin diff --git a/client/src/app.tsx b/client/src/app.tsx index bf675ddf..0c39c86a 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -1,6 +1,6 @@ import { useState } from 'react' -const API_URL = 'http://127.0.0.1:5000' +const API_URL = 'http://127.0.0.1:5001' type Message = { sender: string diff --git a/client/src/index.ts b/client/src/index.ts index 0a1ca3d2..4feb0928 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -21,26 +21,34 @@ const createWindow = (): void => { minWidth: 400, minHeight: 300, titleBarStyle: 'hiddenInset', - // trafficLightPosition: { x: 20, y: 18 }, - // vibrancy: 'titlebar', transparent: true, }) - // Start the executable - let pyExecutable = path.join(__dirname, '../renderer/resources/server') - console.log(`Starting ${pyExecutable}`) - let pyProcess = spawn(pyExecutable) - pyProcess.stdout.on('data', data => { - console.log(`server: ${data}`) - }) - pyProcess.stderr.on('data', data => { - console.error(`server: ${data}`) - }) - // and load the index.html of the app. mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY) } +// if the app is packaged then run the server +if (app.isPackaged) { + const resources = process.resourcesPath + console.log(resources) + + // Start the executable + const exec = path.join(resources, 'server') + console.log(`Starting ${exec}`) + const proc = spawn(exec) + proc.stdout.on('data', data => { + console.log(`server: ${data}`) + }) + proc.stderr.on('data', data => { + console.error(`server: ${data}`) + }) + + process.on('exit', () => { + proc.kill() + }) +} + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. diff --git a/client/webpack.plugins.ts b/client/webpack.plugins.ts index 0e7187f5..ca78adc1 100644 --- a/client/webpack.plugins.ts +++ b/client/webpack.plugins.ts @@ -1,19 +1,10 @@ import type IForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin' -import * as path from 'path' -import PermissionsPlugin from './permissions-plugin' // eslint-disable-next-line @typescript-eslint/no-var-requires const ForkTsCheckerWebpackPlugin: typeof IForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin') -const CopyWebpackPlugin = require('copy-webpack-plugin') export const plugins = [ new ForkTsCheckerWebpackPlugin({ logger: 'webpack-infrastructure', }), - new CopyWebpackPlugin({ - patterns: [{ from: 'resources', to: 'resources' }], - }), - new PermissionsPlugin({ - resourcePath: '.webpack/renderer/resources/server', - }), ] diff --git a/models/.gitignore b/models/.gitignore new file mode 100644 index 00000000..a8a0dcec --- /dev/null +++ b/models/.gitignore @@ -0,0 +1 @@ +*.bin diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 00000000..1b1d3c68 --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,5 @@ +.env +.venv +*.spec +build +dist diff --git a/server/build.py b/server/build.py index 38f79652..f1fecafd 100644 --- a/server/build.py +++ b/server/build.py @@ -1,6 +1,5 @@ import site import os -import shutil from PyInstaller.__main__ import run as pyi_run # the llama_cpp directory is not included if not explicitly added @@ -13,10 +12,9 @@ args = [ site_packages_dir, "--add-data", f"{llama_cpp_dir}{os.pathsep}llama_cpp", - "--onefile", + "--onefile" ] # generate the .spec file and run PyInstaller pyi_run(args) -shutil.copy2("dist/server", "../client/resources/server") diff --git a/server/server.py b/server/server.py index 9fe3fd76..46103a26 100644 --- a/server/server.py +++ b/server/server.py @@ -76,4 +76,4 @@ def generate(): if __name__ == "__main__": - app.run(debug=True, threaded=True, port=5000) + app.run(debug=True, threaded=True, port=5001)