Skip to content

Commit 36b22a6

Browse files
authored
Merge pull request #4121 from headlamp-k8s/improve-headlamp-port-clashes
Improve headlamp port clashes
2 parents 01f52ab + 7352c01 commit 36b22a6

File tree

22 files changed

+463
-91
lines changed

22 files changed

+463
-91
lines changed

app/electron/main.ts

Lines changed: 263 additions & 73 deletions
Large diffs are not rendered by default.

app/electron/preload.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ contextBridge.exposeInMainWorld('desktopApi', {
3030
'plugin-manager',
3131
'request-backend-token',
3232
'request-plugin-permission-secrets',
33+
'request-backend-port',
3334
];
3435
if (validChannels.includes(channel)) {
3536
ipcRenderer.send(channel, data);
@@ -48,6 +49,7 @@ contextBridge.exposeInMainWorld('desktopApi', {
4849
'backend-token',
4950
'plugin-permission-secrets',
5051
'open-about-dialog',
52+
'backend-port',
5153
];
5254
if (validChannels.includes(channel)) {
5355
// Deliberately strip event as it includes `sender`

app/scripts/start.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ if (process.platform !== 'win32') {
170170
const frontendProcess = spawn(frontendCmd, [], {
171171
stdio: 'inherit',
172172
shell: true,
173+
env: {
174+
...process.env,
175+
},
173176
});
174177

175178
let appProcess = null;

frontend/src/helpers/getAppUrl.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ import { isDevMode } from './isDevMode';
1919
import { isDockerDesktop } from './isDockerDesktop';
2020
import { isElectron } from './isElectron';
2121

22+
declare global {
23+
interface Window {
24+
/**
25+
* headlampBackendPort is set by Electron to specify the backend server port.
26+
* It allows the frontend to connect to the backend on a configurable port.
27+
*/
28+
headlampBackendPort?: number;
29+
}
30+
}
31+
2232
/**
2333
* @returns URL depending on dev-mode/electron/docker desktop, base-url, and window.location.origin.
2434
*
@@ -31,9 +41,30 @@ import { isElectron } from './isElectron';
3141
*
3242
*/
3343
export function getAppUrl(): string {
34-
let url = isDevMode() || isElectron() ? 'http://localhost:4466' : window.location.origin;
44+
let url = '';
45+
let backendPort = 4466;
46+
let useLocalhost = false;
47+
48+
if (isElectron()) {
49+
if (window?.headlampBackendPort) {
50+
backendPort = window.headlampBackendPort;
51+
}
52+
useLocalhost = true;
53+
}
54+
55+
if (isDevMode()) {
56+
useLocalhost = true;
57+
}
58+
3559
if (isDockerDesktop()) {
36-
url = 'http://localhost:64446';
60+
backendPort = 64446;
61+
useLocalhost = true;
62+
}
63+
64+
if (useLocalhost) {
65+
url = `http://localhost:${backendPort}`;
66+
} else {
67+
url = window.location.origin;
3768
}
3869

3970
const baseUrl = getBaseUrl();

frontend/src/i18n/locales/de/app.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"Continue": "Weiter",
4747
"Failed to quit the other running process": "Konnte den anderen laufenden Prozess nicht beenden",
4848
"Could not quit the other running process, PIDs: {{ process_list }}. Please stop that process and relaunch the app.": "Konnte den anderen laufenden Prozess nicht beenden, PIDs: {{ process_list }}. Bitte stoppen Sie diesen Prozess und starten Sie die App neu.",
49+
"No available ports": "",
50+
"Could not find an available port. There are processes running on ports {{startPort}}-{{endPort}}. Terminate these processes and retry?": "",
51+
"Terminate and Retry": "",
52+
"Failed to start": "",
53+
"Could not start the server even after terminating existing processes.": "",
54+
"Could not find an available port in the range {{startPort}}-{{endPort}}. Please free up a port and try again.": "",
4955
"Invalid URL": "Ungültige URL",
5056
"Application opened with an invalid URL: {{ url }}": "Anwendung mit einer ungültigen URL geöffnet: {{ url }}"
5157
}

frontend/src/i18n/locales/en/app.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"Continue": "Continue",
4747
"Failed to quit the other running process": "Failed to quit the other running process",
4848
"Could not quit the other running process, PIDs: {{ process_list }}. Please stop that process and relaunch the app.": "Could not quit the other running process, PIDs: {{ process_list }}. Please stop that process and relaunch the app.",
49+
"No available ports": "",
50+
"Could not find an available port. There are processes running on ports {{startPort}}-{{endPort}}. Terminate these processes and retry?": "",
51+
"Terminate and Retry": "",
52+
"Failed to start": "",
53+
"Could not start the server even after terminating existing processes.": "",
54+
"Could not find an available port in the range {{startPort}}-{{endPort}}. Please free up a port and try again.": "",
4955
"Invalid URL": "Invalid URL",
5056
"Application opened with an invalid URL: {{ url }}": "Application opened with an invalid URL: {{ url }}"
5157
}

frontend/src/i18n/locales/es/app.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"Continue": "Continuar",
4747
"Failed to quit the other running process": "Fallo al quitar el otro proceso en ejecución",
4848
"Could not quit the other running process, PIDs: {{ process_list }}. Please stop that process and relaunch the app.": "No se ha podido terminar el otro proceso, PIDs: {{ process_list }}. Por favor, termine el otro proceso y vuelva a lanzar la aplicación.",
49+
"No available ports": "",
50+
"Could not find an available port. There are processes running on ports {{startPort}}-{{endPort}}. Terminate these processes and retry?": "",
51+
"Terminate and Retry": "",
52+
"Failed to start": "",
53+
"Could not start the server even after terminating existing processes.": "",
54+
"Could not find an available port in the range {{startPort}}-{{endPort}}. Please free up a port and try again.": "",
4955
"Invalid URL": "URL inválida",
5056
"Application opened with an invalid URL: {{ url }}": "Aplicación abierta con una URL inválida: {{ url }}"
5157
}

frontend/src/i18n/locales/fr/app.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"Continue": "Continuer",
4747
"Failed to quit the other running process": "Échec de la fermeture de l'autre processus en cours",
4848
"Could not quit the other running process, PIDs: {{ process_list }}. Please stop that process and relaunch the app.": "Impossible de quitter l'autre processus en cours, PIDs : {{ process_list }}. Veuillez arrêter ce processus et relancer l'application.",
49+
"No available ports": "",
50+
"Could not find an available port. There are processes running on ports {{startPort}}-{{endPort}}. Terminate these processes and retry?": "",
51+
"Terminate and Retry": "",
52+
"Failed to start": "",
53+
"Could not start the server even after terminating existing processes.": "",
54+
"Could not find an available port in the range {{startPort}}-{{endPort}}. Please free up a port and try again.": "",
4955
"Invalid URL": "URL invalide",
5056
"Application opened with an invalid URL: {{ url }}": "Application ouverte avec une URL invalide : {{ url }}"
5157
}

frontend/src/i18n/locales/hi/app.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"Continue": "जारी रखें",
4747
"Failed to quit the other running process": "अन्य चल रही प्रक्रिया से बाहर निकलने में विफल",
4848
"Could not quit the other running process, PIDs: {{ process_list }}. Please stop that process and relaunch the app.": "अन्य चल रही प्रक्रिया से बाहर नहीं निकल सका, PIDs: {{ process_list }}। कृपया उस प्रक्रिया को रोकें और ऐप को फिर से लॉन्च करें।",
49+
"No available ports": "",
50+
"Could not find an available port. There are processes running on ports {{startPort}}-{{endPort}}. Terminate these processes and retry?": "",
51+
"Terminate and Retry": "",
52+
"Failed to start": "",
53+
"Could not start the server even after terminating existing processes.": "",
54+
"Could not find an available port in the range {{startPort}}-{{endPort}}. Please free up a port and try again.": "",
4955
"Invalid URL": "अमान्य URL",
5056
"Application opened with an invalid URL: {{ url }}": "अनुप्रयोग एक अमान्य URL के साथ खोला गया: {{ url }}"
5157
}

frontend/src/i18n/locales/it/app.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"Continue": "Continua",
4747
"Failed to quit the other running process": "Impossibile terminare l'altro processo in esecuzione",
4848
"Could not quit the other running process, PIDs: {{ process_list }}. Please stop that process and relaunch the app.": "Impossibile terminare l'altro processo in esecuzione, PID: {{ process_list }}. Ferma quel processo e riavvia l'app.",
49+
"No available ports": "",
50+
"Could not find an available port. There are processes running on ports {{startPort}}-{{endPort}}. Terminate these processes and retry?": "",
51+
"Terminate and Retry": "",
52+
"Failed to start": "",
53+
"Could not start the server even after terminating existing processes.": "",
54+
"Could not find an available port in the range {{startPort}}-{{endPort}}. Please free up a port and try again.": "",
4955
"Invalid URL": "URL Non Valido",
5056
"Application opened with an invalid URL: {{ url }}": "Applicazione avviata con un URL non valido: {{ url }}"
5157
}

0 commit comments

Comments
 (0)