Skip to content

Commit e0140cc

Browse files
committed
Make DaprServer configurable with envvars
Signed-off-by: Shubham Sharma <[email protected]>
1 parent 1003044 commit e0140cc

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/implementation/Server/DaprServer.ts

Lines changed: 7 additions & 6 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 = Settings.getDefaultHost()
40+
, serverPort?: string
41+
, daprHost = Settings.getDefaultHost()
42+
, daprPort?: string
4243
, communicationProtocol: CommunicationProtocolEnum = CommunicationProtocolEnum.HTTP
4344
, clientOptions: DaprClientOptions = {
4445
isKeepAlive: true
4546
}
4647
) {
4748
this.serverHost = serverHost;
48-
this.serverPort = serverPort;
49+
this.serverPort = serverPort ?? Settings.getDefaultAppPort(communicationProtocol);
4950
this.daprHost = daprHost;
50-
this.daprPort = daprPort;
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import CommunicationProtocolEnum from "../enum/CommunicationProtocol.enum";
22

33
export class Settings {
44
private static readonly defaultHost: string = "127.0.0.1";
5+
private static readonly defaultHttpAppPort: string = "3000";
56
private static readonly defaultHttpPort: string = "3500";
7+
private static readonly defaultGrpcAppPort: string = "50000";
68
private static readonly defaultGrpcPort: string = "50001";
79

810
static getDefaultHost(): string {
@@ -17,6 +19,11 @@ export class Settings {
1719
return process.env.DAPR_GRPC_PORT ?? Settings.defaultGrpcPort;
1820
}
1921

22+
/**
23+
* Gets the default port that the Dapr sidecar is listening to.
24+
* @param communicationProtocolEnum communication protocol
25+
* @returns port number
26+
*/
2027
static getDefaultPort(communicationProtocolEnum: CommunicationProtocolEnum): string {
2128
switch (communicationProtocolEnum) {
2229
case CommunicationProtocolEnum.GRPC:
@@ -25,4 +32,26 @@ export class Settings {
2532
return this.getDefaultHttpPort();
2633
}
2734
}
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+
}
2857
}

0 commit comments

Comments
 (0)