Skip to content

Commit ea0cc53

Browse files
Merge pull request #192 from shubham1172/shubham1172/use-env-vars-in-client
feat: Use environment variables to initialize client and server
2 parents 0b93304 + 3a46b2c commit ea0cc53

File tree

6 files changed

+112
-22
lines changed

6 files changed

+112
-22
lines changed

src/implementation/Client/DaprClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import HTTPClient from './HTTPClient/HTTPClient';
3333

3434
import CommunicationProtocolEnum from '../../enum/CommunicationProtocol.enum';
3535
import { DaprClientOptions } from '../../types/DaprClientOptions';
36+
import { Settings } from '../../utils/Settings.util';
3637

3738
export default class DaprClient {
3839
readonly daprHost: string;
@@ -52,15 +53,15 @@ export default class DaprClient {
5253
readonly actor: IClientActorBuilder;
5354

5455
constructor(
55-
daprHost: string
56-
, daprPort: string
56+
daprHost?: string
57+
, daprPort?: string
5758
, communicationProtocol: CommunicationProtocolEnum = CommunicationProtocolEnum.HTTP
5859
, options: DaprClientOptions = {
5960
isKeepAlive: true
6061
}
6162
) {
62-
this.daprHost = daprHost;
63-
this.daprPort = daprPort;
63+
this.daprHost = daprHost ?? Settings.getDefaultHost();
64+
this.daprPort = daprPort ?? Settings.getDefaultPort(communicationProtocol);
6465
this.communicationProtocol = communicationProtocol;
6566
this.options = options;
6667

src/implementation/Client/GRPCClient/GRPCClient.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ import { DaprClient } from "../../../proto/dapr/proto/runtime/v1/dapr_grpc_pb"
44
import IClient from "../../../interfaces/Client/IClient";
55
import CommunicationProtocolEnum from "../../../enum/CommunicationProtocol.enum";
66
import { DaprClientOptions } from "../../../types/DaprClientOptions";
7+
import { Settings } from '../../../utils/Settings.util';
78

89
export default class GRPCClient implements IClient {
9-
private isInitialized: boolean;
1010
private readonly client: DaprClient;
1111
private readonly clientCredentials: grpc.ChannelCredentials;
1212
private readonly clientHost: string;
1313
private readonly clientPort: string;
1414
private readonly options: DaprClientOptions;
1515

16-
constructor(host = "127.0.0.1", port = "50050", options: DaprClientOptions = {
17-
isKeepAlive: true
18-
}) {
19-
this.isInitialized = true;
16+
constructor(
17+
host = Settings.getDefaultHost()
18+
, port = Settings.getDefaultGrpcPort()
19+
, options: DaprClientOptions = {
20+
isKeepAlive: true
21+
}
22+
) {
2023
this.clientHost = host;
2124
this.clientPort = port;
2225
this.clientCredentials = ChannelCredentials.createInsecure();

src/implementation/Client/HTTPClient/HTTPClient.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import IClient from "../../../interfaces/Client/IClient";
44
import http from "http";
55
import https from "https";
66
import { DaprClientOptions } from "../../../types/DaprClientOptions";
7+
import { Settings } from '../../../utils/Settings.util';
78

89
export default class HTTPClient implements IClient {
9-
private readonly isInitialized: boolean;
1010
private client: typeof fetch;
1111
private readonly clientHost: string;
1212
private readonly clientPort: string;
@@ -16,10 +16,13 @@ export default class HTTPClient implements IClient {
1616
private readonly httpAgent;
1717
private readonly httpsAgent;
1818

19-
constructor(host = "127.0.0.1", port = "50050", options: DaprClientOptions = {
20-
isKeepAlive: true
21-
}) {
22-
this.isInitialized = true;
19+
constructor(
20+
host = Settings.getDefaultHost()
21+
, port = Settings.getDefaultHttpPort()
22+
, options: DaprClientOptions = {
23+
isKeepAlive: true
24+
}
25+
) {
2326
this.clientHost = host;
2427
this.clientPort = port;
2528
this.options = options;

src/implementation/Server/DaprServer.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import HTTPServerInvoker from './HTTPServer/invoker';
1818
import HTTPServerActor from './HTTPServer/actor';
1919
import { DaprClientOptions } from '../../types/DaprClientOptions';
2020
import { DaprClient } from '../..';
21+
import { Settings } from '../../utils/Settings.util';
2122

2223
export default class DaprServer {
2324
// App details
@@ -35,19 +36,19 @@ export default class DaprServer {
3536
readonly client: DaprClient;
3637

3738
constructor(
38-
serverHost = "127.0.0.1"
39-
, serverPort: string = process.env.DAPR_SERVER_PORT || "50050"
40-
, daprHost = "127.0.0.1"
41-
, daprPort = "50051"
39+
serverHost?: string
40+
, serverPort?: string
41+
, daprHost?: string
42+
, daprPort?: string
4243
, communicationProtocol: CommunicationProtocolEnum = CommunicationProtocolEnum.HTTP
4344
, clientOptions: DaprClientOptions = {
4445
isKeepAlive: true
4546
}
4647
) {
47-
this.serverHost = serverHost;
48-
this.serverPort = serverPort;
49-
this.daprHost = daprHost;
50-
this.daprPort = daprPort;
48+
this.serverHost = serverHost ?? Settings.getDefaultHost();
49+
this.serverPort = serverPort ?? Settings.getDefaultAppPort(communicationProtocol);
50+
this.daprHost = daprHost ?? Settings.getDefaultHost();
51+
this.daprPort = daprPort ?? Settings.getDefaultPort(communicationProtocol);
5152

5253
// Create a client to interface with the sidecar from the server side
5354
this.client = new DaprClient(daprHost, daprPort, communicationProtocol, clientOptions);

src/utils/Settings.util.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import CommunicationProtocolEnum from "../enum/CommunicationProtocol.enum";
2+
3+
export class Settings {
4+
private static readonly defaultHost: string = "127.0.0.1";
5+
private static readonly defaultHttpAppPort: string = "3000";
6+
private static readonly defaultHttpPort: string = "3500";
7+
private static readonly defaultGrpcAppPort: string = "50000";
8+
private static readonly defaultGrpcPort: string = "50001";
9+
10+
static getDefaultHost(): string {
11+
return Settings.defaultHost;
12+
}
13+
14+
static getDefaultHttpPort(): string {
15+
return process.env.DAPR_HTTP_PORT ?? Settings.defaultHttpPort;
16+
}
17+
18+
static getDefaultGrpcPort(): string {
19+
return process.env.DAPR_GRPC_PORT ?? Settings.defaultGrpcPort;
20+
}
21+
22+
/**
23+
* Gets the default port that the Dapr sidecar is listening to.
24+
* @param communicationProtocolEnum communication protocol
25+
* @returns port number
26+
*/
27+
static getDefaultPort(communicationProtocolEnum: CommunicationProtocolEnum): string {
28+
switch (communicationProtocolEnum) {
29+
case CommunicationProtocolEnum.GRPC:
30+
return this.getDefaultGrpcPort();
31+
default:
32+
return this.getDefaultHttpPort();
33+
}
34+
}
35+
36+
static getDefaultHttpAppPort(): string {
37+
return process.env.APP_PORT ?? Settings.defaultHttpAppPort;
38+
}
39+
40+
static getDefaultGrpcAppPort(): string {
41+
return process.env.APP_PORT ?? Settings.defaultGrpcAppPort;
42+
}
43+
44+
/**
45+
* Gets the default port that the application is listening on.
46+
* @param communicationProtocolEnum communication protocol
47+
* @returns port number
48+
*/
49+
static getDefaultAppPort(communicationProtocolEnum: CommunicationProtocolEnum): string {
50+
switch (communicationProtocolEnum) {
51+
case CommunicationProtocolEnum.GRPC:
52+
return this.getDefaultGrpcAppPort();
53+
default:
54+
return this.getDefaultHttpAppPort();
55+
}
56+
}
57+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { CommunicationProtocolEnum } from "../../../src";
2+
import { Settings } from "../../../src/utils/Settings.util";
3+
4+
describe('Settings', () => {
5+
describe('getDefaultPort returns the correct default when', () => {
6+
it('communication protocol is GRPC', () => {
7+
expect(Settings.getDefaultPort(CommunicationProtocolEnum.GRPC))
8+
.toEqual(Settings.getDefaultGrpcPort())
9+
});
10+
it('communication protocol is HTTP', () => {
11+
expect(Settings.getDefaultPort(CommunicationProtocolEnum.HTTP))
12+
.toEqual(Settings.getDefaultHttpPort())
13+
});
14+
});
15+
describe('getDefaultAppPort returns the correct default when', () => {
16+
it('communication protocol is GRPC', () => {
17+
expect(Settings.getDefaultAppPort(CommunicationProtocolEnum.GRPC))
18+
.toEqual(Settings.getDefaultGrpcAppPort())
19+
});
20+
it('communication protocol is HTTP', () => {
21+
expect(Settings.getDefaultAppPort(CommunicationProtocolEnum.HTTP))
22+
.toEqual(Settings.getDefaultHttpAppPort())
23+
});
24+
});
25+
})

0 commit comments

Comments
 (0)