Skip to content

Commit 9c94ce4

Browse files
committed
infer endpoint root in production mode
(cherry picked from commit 57367c8)
1 parent 5f396f4 commit 9c94ce4

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

interface/.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Change the IP address to that of your ESP device to enable local development of the UI.
22
# Remember to also enable CORS in platformio.ini before uploading the code to the device.
3-
REACT_APP_ENDPOINT_ROOT=http://192.168.0.99/rest/
3+
REACT_APP_HTTP_ROOT=http://192.168.0.99
44
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.0.99

interface/.env.production

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
REACT_APP_ENDPOINT_ROOT=/rest/
21
GENERATE_SOURCEMAP=false

interface/src/api/Env.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
export const PROJECT_NAME = process.env.REACT_APP_PROJECT_NAME!;
22
export const PROJECT_PATH = process.env.REACT_APP_PROJECT_PATH!;
3-
export const ENDPOINT_ROOT = process.env.REACT_APP_ENDPOINT_ROOT!;
43

5-
// TODO use same approach for rest endpoint?
6-
export const WEB_SOCKET_ROOT = calculateWebSocketPrefix("/ws");
4+
export const ENDPOINT_ROOT = calculateEndpointRoot("/rest/");
5+
export const WEB_SOCKET_ROOT = calculateWebSocketRoot("/ws/");
76

8-
function calculateWebSocketPrefix(webSocketPath: string) {
7+
function calculateEndpointRoot(endpointPath: string) {
8+
const httpRoot = process.env.REACT_APP_HTTP_ROOT;
9+
if (httpRoot) {
10+
return httpRoot + endpointPath;
11+
}
12+
const location = window.location;
13+
return location.protocol + "//" + location.host + endpointPath;
14+
}
15+
16+
function calculateWebSocketRoot(webSocketPath: string) {
917
const webSocketRoot = process.env.REACT_APP_WEB_SOCKET_ROOT;
10-
if (!webSocketRoot || webSocketRoot.length === 0) {
11-
var loc = window.location, webSocketURI;
12-
if (loc.protocol === "https:") {
13-
webSocketURI = "wss:";
14-
} else {
15-
webSocketURI = "ws:";
16-
}
17-
return webSocketURI + "//" + loc.host + webSocketPath;
18+
if (webSocketRoot) {
19+
return webSocketRoot + webSocketPath;
1820
}
19-
return webSocketRoot + webSocketPath;
21+
const location = window.location;
22+
const webProtocol = location.protocol === "https:" ? "wss:" : "ws:";
23+
return webProtocol + "//" + location.host + webSocketPath;
2024
}

interface/src/project/LightSettingsSocketController.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { SectionContent, BlockFormControlLabel } from '../components';
88

99
import { LightSettings } from './types';
1010

11-
export const LIGHT_SETTINGS_WEBSOCKET_URL = WEB_SOCKET_ROOT + "/lightSettings";
11+
export const LIGHT_SETTINGS_WEBSOCKET_URL = WEB_SOCKET_ROOT + "lightSettings";
1212

1313
type LightSettingsSocketControllerProps = SocketControllerProps<LightSettings>;
1414

0 commit comments

Comments
 (0)