Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make port/host configurable #178

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The host to use for the ComfyUI server.
COMFY_HOST=127.0.0.1

# The port to use for the ComfyUI server.
COMFY_PORT=8188

# Whether to use an external server instead of starting one locally.
USE_EXTERNAL_SERVER=false
10 changes: 10 additions & 0 deletions .env_test_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Use this file to test with local ComfyUI_frontend dev server.

# The host to use for the ComfyUI server.
COMFY_HOST=localhost

# The port to use for the ComfyUI server.
COMFY_PORT=5173

# Whether to use an external server instead of starting one locally.
USE_EXTERNAL_SERVER=true
69 changes: 46 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ import { PythonEnvironment } from './pythonEnvironment';
import { DownloadManager } from './models/DownloadManager';
import { getModelsDirectory } from './utils';
import { ComfySettings } from './config/comfySettings';
import dotenv from 'dotenv';

dotenv.config();

let comfyServerProcess: ChildProcess | null = null;
let isRestarting: boolean = false; // Prevents double restarts TODO(robinhuang): Remove this once we have a better way to handle restarts. https://github.com/Comfy-Org/electron/issues/149
const host = '127.0.0.1';
let port = 8188;

/** The host to use for the ComfyUI server. */
const host = process.env.COMFY_HOST || '127.0.0.1';
/** The port to use for the ComfyUI server. */
let port = parseInt(process.env.COMFY_PORT || '-1');
/**
* Whether to use an external server instead of starting one locally.
* Only effective if COMFY_PORT is set.
* Note: currently used for testing only.
*/
const useExternalServer = process.env.USE_EXTERNAL_SERVER === 'true';

let mainWindow: BrowserWindow | null = null;
let store: Store<StoreType> | null = null;
const messageQueue: Array<any> = []; // Stores mesaages before renderer is ready.
Expand Down Expand Up @@ -188,27 +201,37 @@ if (!gotTheLock) {
downloadManager = DownloadManager.getInstance(mainWindow!, getModelsDirectory(basePath));
downloadManager.registerIpcHandlers();

port = await findAvailablePort(8000, 9999).catch((err) => {
log.error(`ERROR: Failed to find available port: ${err}`);
throw err;
});

sendProgressUpdate('Setting up Python Environment...');
const pythonEnvironment = new PythonEnvironment(pythonInstallPath, appResourcesPath, spawnPythonAsync);
await pythonEnvironment.setup();
SetupTray(
mainWindow,
basePath,
modelConfigPath,
() => {
log.info('Resetting install location');
fs.rmSync(modelConfigPath);
restartApp();
},
pythonEnvironment
);
sendProgressUpdate('Starting Comfy Server...');
await launchPythonServer(pythonEnvironment.pythonInterpreterPath, appResourcesPath, modelConfigPath, basePath);
port =
port !== -1
? port
: await findAvailablePort(8000, 9999).catch((err) => {
log.error(`ERROR: Failed to find available port: ${err}`);
throw err;
});

if (!useExternalServer) {
sendProgressUpdate('Setting up Python Environment...');
const pythonEnvironment = new PythonEnvironment(pythonInstallPath, appResourcesPath, spawnPythonAsync);
await pythonEnvironment.setup();

// TODO: Make tray setup more flexible here as not all actions depend on the python environment.
SetupTray(
mainWindow,
basePath,
modelConfigPath,
() => {
log.info('Resetting install location');
fs.rmSync(modelConfigPath);
restartApp();
},
pythonEnvironment
);
sendProgressUpdate('Starting Comfy Server...');
await launchPythonServer(pythonEnvironment.pythonInterpreterPath, appResourcesPath, modelConfigPath, basePath);
} else {
sendProgressUpdate('Using external server at ' + host + ':' + port);
loadComfyIntoMainWindow();
}
} catch (error) {
log.error(error);
sendProgressUpdate(COMFY_ERROR_MESSAGE);
Expand Down
Loading