Skip to content

Commit

Permalink
Merge pull request #19 from mlx-chat/MLC-37
Browse files Browse the repository at this point in the history
[MLC-37] Make app production ready with builds
  • Loading branch information
ParkerSm1th authored Mar 3, 2024
2 parents db13f00 + a9fff2b commit f100356
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 79 deletions.
12 changes: 12 additions & 0 deletions app/mac/entitlements.mac.inherit.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
<true/>
<key>com.apple.security.cs.disable-library-validation</key>
<true/>
</dict>
</plist>
40 changes: 29 additions & 11 deletions app/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Main File for Electron

import {
execFile,
} from 'child_process';
import {
app,
BrowserWindow,
Expand Down Expand Up @@ -45,13 +48,15 @@ class ServerManager {
});
}

private runPythonServer(model: string, port: number): any {
const args = [`--model ${model}`, '--host 127.0.0.1', `--port ${port}`];
private runPythonServer(port: number): any {
const args = ['--host 127.0.0.1', `--port ${port}`];
const modifiedArgs = args.flatMap(arg => arg.split(/\s+/));

const pythonProcess = spawn('python', ['-m', 'server.server', ...modifiedArgs], {
cwd: '../',
});
const pythonProcess = isProd
? execFile(path.join(process.resourcesPath, 'server', 'runner'), modifiedArgs)
: spawn('python', ['-m', 'server.server', ...modifiedArgs], {
cwd: '../',
});
pythonProcess.stdout.on(
'data',
(data: Buffer) => console.log('Server output:', data.toString('utf8')),
Expand All @@ -69,16 +74,29 @@ class ServerManager {
this.stop();

this.findOpenPort(8080).then((port) => {
console.log(`Starting server for model: ${model} on port: ${port}`);
this.serverProcess = this.runPythonServer(model, port);
console.log(`APP: Starting server for model: ${model} on port: ${port}`);
this.serverProcess = this.runPythonServer(port);

this.serverProcess.stdout.on('data', (data: Buffer) => {
this.serverProcess.stdout.on('data', async (data: Buffer) => {
const output = data.toString('utf8');
console.log('Server output:', output);

await new Promise((resolve) => setTimeout(resolve, 1000));

// Check if the server is ready
if (output.includes('starting server on')) {
resolve(); // Resolve the promise when the server is ready
if (output.includes('Starting httpd')) {
fetch(`http://127.0.0.1:${port}/api/init`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ model }),
}).then(() => {
resolve(); // Resolve the promise when the server is ready
}).catch((err) => {
console.error('Error initializing the server:', err);
reject(err);
});
}
});

Expand Down Expand Up @@ -280,7 +298,7 @@ const createWindow = () => {
});

if (isProd) {
settingsModal.loadURL('app://./home.html');
settingsModal.loadURL('app://./settings.html');
} else {
// const port = process.argv[2];
settingsModal.loadURL('http://localhost:3000/settings');
Expand Down
18 changes: 18 additions & 0 deletions app/notarize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require('dotenv').config();
const { notarize } = require('electron-notarize');

exports.default = async function notarizing(context) {
const { electronPlatformName, appOutDir } = context;
if (electronPlatformName !== 'darwin') {
return;
}

const appName = context.packager.appInfo.productFilename;

return await notarize({
appBundleId: 'com.parkersmith.mlx-chat',
appPath: `${appOutDir}/${appName}.app`,
appleId: process.env.APPLEID,
appleIdPassword: process.env.APPLEIDPASS,
});
};
Loading

0 comments on commit f100356

Please sign in to comment.