Skip to content

Commit f788efa

Browse files
authored
Add support to DAPR_HTTP_ENDPOINT and DAPR_GRPC_ENDPOINT env. (#1124)
Signed-off-by: Artur Souza <[email protected]>
1 parent 574dc0c commit f788efa

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

daprdocs/content/en/dotnet-sdk-docs/dotnet-client/dotnet-daprclient-usage.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ The `DaprClientBuilder` contains settings for:
3434

3535
The SDK will read the following environment variables to configure the default values:
3636

37-
- `DAPR_HTTP_PORT`: used to find the HTTP endpoint of the Dapr sidecar
38-
- `DAPR_GRPC_PORT`: used to find the gRPC endpoint of the Dapr sidecar
37+
- `DAPR_HTTP_ENDPOINT`: used to find the HTTP endpoint of the Dapr sidecar, example: `https://dapr-api.mycompany.com`
38+
- `DAPR_GRPC_ENDPOINT`: used to find the gRPC endpoint of the Dapr sidecar, example: `https://dapr-grpc-api.mycompany.com`
39+
- `DAPR_HTTP_PORT`: if `DAPR_HTTP_ENDPOINT` is not set, this is used to find the HTTP local endpoint of the Dapr sidecar
40+
- `DAPR_GRPC_PORT`: if `DAPR_GRPC_ENDPOINT` is not set, this is used to find the gRPC local endpoint of the Dapr sidecar
3941
- `DAPR_API_TOKEN`: used to set the API Token
4042

4143
### Configuring gRPC channel options

src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ public int? RemindersStoragePartitions
220220
/// </summary>
221221
/// <remarks>
222222
/// The URI endpoint to use for HTTP calls to the Dapr runtime. The default value will be
223-
/// <c>http://127.0.0.1:DAPR_HTTP_PORT</c> where <c>DAPR_HTTP_PORT</c> represents the value of the
224-
/// <c>DAPR_HTTP_PORT</c> environment variable.
223+
/// <c>DAPR_HTTP_ENDPOINT</c> first, or <c>http://127.0.0.1:DAPR_HTTP_PORT</c> as fallback
224+
/// where <c>DAPR_HTTP_ENDPOINT</c> and <c>DAPR_HTTP_PORT</c> represents the value of the
225+
/// corresponding environment variables.
225226
/// </remarks>
226227
/// <value></value>
227228
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();

src/Dapr.Client/DaprClientBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public DaprClientBuilder()
6363
/// </summary>
6464
/// <param name="httpEndpoint">
6565
/// The URI endpoint to use for HTTP calls to the Dapr runtime. The default value will be
66-
/// <c>http://127.0.0.1:DAPR_HTTP_PORT</c> where <c>DAPR_HTTP_PORT</c> represents the value of the
67-
/// <c>DAPR_HTTP_PORT</c> environment variable.
66+
/// <c>DAPR_HTTP_ENDPOINT</c> first, or <c>http://127.0.0.1:DAPR_HTTP_PORT</c> as fallback
67+
/// where <c>DAPR_HTTP_ENDPOINT</c> and <c>DAPR_HTTP_PORT</c> represents the value of the
68+
/// corresponding environment variables.
6869
/// </param>
6970
/// <returns>The <see cref="DaprClientBuilder" /> instance.</returns>
7071
public DaprClientBuilder UseHttpEndpoint(string httpEndpoint)

src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ static bool TryGetGrpcAddress(out string address)
104104
// 1. DaprDefaults.cs uses 127.0.0.1 instead of localhost, which prevents testing with Dapr on WSL2 and the app on Windows
105105
// 2. DaprDefaults.cs doesn't compile when the project has C# nullable reference types enabled.
106106
// If the above issues are fixed (ensuring we don't regress anything) we should switch to using the logic in DaprDefaults.cs.
107+
string? daprEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT");
108+
if (!String.IsNullOrEmpty(daprEndpoint)) {
109+
address = daprEndpoint;
110+
return true;
111+
}
112+
107113
string? daprPortStr = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT");
108114
if (int.TryParse(daprPortStr, out int daprGrpcPort))
109115
{

src/Shared/DaprDefaults.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,19 @@ public static string GetDefaultAppApiToken()
5757
}
5858

5959
/// <summary>
60-
/// Get the value of environment variable DAPR_HTTP_PORT
60+
/// Get the value of HTTP endpoint based off environment variables
6161
/// </summary>
62-
/// <returns>The value of environment variable DAPR_HTTP_PORT</returns>
62+
/// <returns>The value of HTTP endpoint based off environment variables</returns>
6363
public static string GetDefaultHttpEndpoint()
6464
{
6565
if (httpEndpoint == null)
6666
{
67+
var endpoint = Environment.GetEnvironmentVariable("DAPR_HTTP_ENDPOINT");
68+
if (!string.IsNullOrEmpty(endpoint)) {
69+
httpEndpoint = endpoint;
70+
return httpEndpoint;
71+
}
72+
6773
var port = Environment.GetEnvironmentVariable("DAPR_HTTP_PORT");
6874
port = string.IsNullOrEmpty(port) ? "3500" : port;
6975
httpEndpoint = $"http://127.0.0.1:{port}";
@@ -73,13 +79,19 @@ public static string GetDefaultHttpEndpoint()
7379
}
7480

7581
/// <summary>
76-
/// Get the value of environment variable DAPR_GRPC_PORT
82+
/// Get the value of gRPC endpoint based off environment variables
7783
/// </summary>
78-
/// <returns>The value of environment variable DAPR_GRPC_PORT</returns>
84+
/// <returns>The value of gRPC endpoint based off environment variables</returns>
7985
public static string GetDefaultGrpcEndpoint()
8086
{
8187
if (grpcEndpoint == null)
8288
{
89+
var endpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT");
90+
if (!string.IsNullOrEmpty(endpoint)) {
91+
grpcEndpoint = endpoint;
92+
return grpcEndpoint;
93+
}
94+
8395
var port = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT");
8496
port = string.IsNullOrEmpty(port) ? "50001" : port;
8597
grpcEndpoint = $"http://127.0.0.1:{port}";

0 commit comments

Comments
 (0)