Skip to content

Commit 8d4b8c8

Browse files
mnothmanpullpushcommit
authored andcommitted
Fix: Add support for DAPR_RUNTIME_HOST in Setrtings.getDefaultHost()
Problem: getDefaultHttpPort() already respected DAPR_HTTP_PORT however, settings.getDefaultHost() did not use DAPR_RUNTIME_HOST Solution: (closes Custom sidecar port Fixes #646) -Added DAPR_RUNTIME_HOST env variable support to Settings.getDefaultHost() Now users can set both DAPR_RUNTIME_HOST and DAPR_HTTP_PORT to fully control where client connects Tests: Created new dedicated test file for env mutations, so as to not mix up jest.isolateModules and top level Settings import Signed-off-by: mnothman <[email protected]>
1 parent 854fb0a commit 8d4b8c8

File tree

5 files changed

+94
-13
lines changed

5 files changed

+94
-13
lines changed

src/implementation/Client/GRPCClient/GRPCClient.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,19 @@ export default class GRPCClient implements IClient {
7979
}
8080

8181
private generateEndpoint(options: Partial<DaprClientOptions>): GrpcEndpoint {
82-
const host = options?.daprHost ?? Settings.getDefaultHost();
83-
const port = options?.daprPort ?? Settings.getDefaultGrpcPort();
84-
let uri = `${host}:${port}`;
85-
82+
// Check for explicit endpoint first (highest priority)
8683
if (!(options?.daprHost || options?.daprPort)) {
87-
// If neither host nor port are specified, check the endpoint environment variable.
8884
const endpoint = Settings.getDefaultGrpcEndpoint();
8985
if (endpoint != "") {
90-
uri = endpoint;
86+
return new GrpcEndpoint(endpoint);
9187
}
9288
}
9389

90+
// Use provided options or environment variable defaults
91+
const host = options?.daprHost ?? Settings.getDefaultHost();
92+
const port = options?.daprPort ?? Settings.getDefaultGrpcPort();
93+
const uri = `${host}:${port}`;
94+
9495
return new GrpcEndpoint(uri);
9596
}
9697

src/implementation/Client/HTTPClient/HTTPClient.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,19 @@ export default class HTTPClient implements IClient {
7575
}
7676

7777
private generateEndpoint(options: Partial<DaprClientOptions>): HttpEndpoint {
78-
const host = options?.daprHost ?? Settings.getDefaultHost();
79-
const port = options?.daprPort ?? Settings.getDefaultHttpPort();
80-
let uri = `${host}:${port}`;
81-
78+
// Check for explicit endpoint first (highest priority)
8279
if (!(options?.daprHost || options?.daprPort)) {
83-
// If neither host nor port are specified, check the endpoint environment variable.
8480
const endpoint = Settings.getDefaultHttpEndpoint();
8581
if (endpoint != "") {
86-
uri = endpoint;
82+
return new HttpEndpoint(endpoint);
8783
}
8884
}
8985

86+
// Use provided options or environment variable defaults
87+
const host = options?.daprHost ?? Settings.getDefaultHost();
88+
const port = options?.daprPort ?? Settings.getDefaultHttpPort();
89+
const uri = `${host}:${port}`;
90+
9091
return new HttpEndpoint(uri);
9192
}
9293

src/utils/Settings.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Settings {
5757
}
5858

5959
static getDefaultHost(): string {
60-
return Settings.defaultHost;
60+
return process.env.DAPR_RUNTIME_HOST ?? Settings.defaultHost;
6161
}
6262

6363
static getDefaultHttpPort(): string {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2022 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
// No top-level imports - we use jest.isolateModules() to import fresh modules
15+
16+
describe("Settings - Environment Variable Support", () => {
17+
// Store original env vars to restore later
18+
const originalEnv = { ...process.env };
19+
20+
afterEach(() => {
21+
// Restore original env after each test
22+
process.env = { ...originalEnv };
23+
});
24+
25+
describe("getDefaultHost", () => {
26+
it("respects DAPR_RUNTIME_HOST environment variable", () => {
27+
jest.isolateModules(() => {
28+
process.env.DAPR_RUNTIME_HOST = "192.168.1.100";
29+
const { Settings } = require("../../../src/utils/Settings.util");
30+
expect(Settings.getDefaultHost()).toEqual("192.168.1.100");
31+
});
32+
});
33+
34+
it("returns default when DAPR_RUNTIME_HOST is not set", () => {
35+
jest.isolateModules(() => {
36+
delete process.env.DAPR_RUNTIME_HOST;
37+
const { Settings } = require("../../../src/utils/Settings.util");
38+
expect(Settings.getDefaultHost()).toEqual("127.0.0.1");
39+
});
40+
});
41+
});
42+
43+
describe("getDefaultHttpPort", () => {
44+
it("respects DAPR_HTTP_PORT environment variable", () => {
45+
jest.isolateModules(() => {
46+
process.env.DAPR_HTTP_PORT = "3510";
47+
const { Settings } = require("../../../src/utils/Settings.util");
48+
expect(Settings.getDefaultHttpPort()).toEqual("3510");
49+
});
50+
});
51+
52+
it("returns default when DAPR_HTTP_PORT is not set", () => {
53+
jest.isolateModules(() => {
54+
delete process.env.DAPR_HTTP_PORT;
55+
const { Settings } = require("../../../src/utils/Settings.util");
56+
expect(Settings.getDefaultHttpPort()).toEqual("3500");
57+
});
58+
});
59+
});
60+
61+
describe("getDefaultGrpcPort", () => {
62+
it("respects DAPR_GRPC_PORT environment variable", () => {
63+
jest.isolateModules(() => {
64+
process.env.DAPR_GRPC_PORT = "50010";
65+
const { Settings } = require("../../../src/utils/Settings.util");
66+
expect(Settings.getDefaultGrpcPort()).toEqual("50010");
67+
});
68+
});
69+
70+
it("returns default when DAPR_GRPC_PORT is not set", () => {
71+
jest.isolateModules(() => {
72+
delete process.env.DAPR_GRPC_PORT;
73+
const { Settings } = require("../../../src/utils/Settings.util");
74+
expect(Settings.getDefaultGrpcPort()).toEqual("50001");
75+
});
76+
});
77+
});
78+
});

test/unit/utils/Settings.util.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe("Settings", () => {
2323
expect(Settings.getDefaultPort(CommunicationProtocolEnum.HTTP)).toEqual(Settings.getDefaultHttpPort());
2424
});
2525
});
26+
2627
describe("getDefaultAppPort returns the correct default when", () => {
2728
it("communication protocol is GRPC", () => {
2829
expect(Settings.getDefaultAppPort(CommunicationProtocolEnum.GRPC)).toEqual(Settings.getDefaultGrpcAppPort());

0 commit comments

Comments
 (0)