Skip to content

Commit

Permalink
Add support to DAPR_HTTP_ENDPOINT and DAPR_GRPC_ENDPOINT env. (#1124)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
  • Loading branch information
artursouza authored Jul 18, 2023
1 parent 574dc0c commit f788efa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ The `DaprClientBuilder` contains settings for:

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

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

### Configuring gRPC channel options
Expand Down
5 changes: 3 additions & 2 deletions src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ public int? RemindersStoragePartitions
/// </summary>
/// <remarks>
/// The URI endpoint to use for HTTP calls to the Dapr runtime. The default value will be
/// <c>http://127.0.0.1:DAPR_HTTP_PORT</c> where <c>DAPR_HTTP_PORT</c> represents the value of the
/// <c>DAPR_HTTP_PORT</c> environment variable.
/// <c>DAPR_HTTP_ENDPOINT</c> first, or <c>http://127.0.0.1:DAPR_HTTP_PORT</c> as fallback
/// where <c>DAPR_HTTP_ENDPOINT</c> and <c>DAPR_HTTP_PORT</c> represents the value of the
/// corresponding environment variables.
/// </remarks>
/// <value></value>
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();
Expand Down
5 changes: 3 additions & 2 deletions src/Dapr.Client/DaprClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public DaprClientBuilder()
/// </summary>
/// <param name="httpEndpoint">
/// The URI endpoint to use for HTTP calls to the Dapr runtime. The default value will be
/// <c>http://127.0.0.1:DAPR_HTTP_PORT</c> where <c>DAPR_HTTP_PORT</c> represents the value of the
/// <c>DAPR_HTTP_PORT</c> environment variable.
/// <c>DAPR_HTTP_ENDPOINT</c> first, or <c>http://127.0.0.1:DAPR_HTTP_PORT</c> as fallback
/// where <c>DAPR_HTTP_ENDPOINT</c> and <c>DAPR_HTTP_PORT</c> represents the value of the
/// corresponding environment variables.
/// </param>
/// <returns>The <see cref="DaprClientBuilder" /> instance.</returns>
public DaprClientBuilder UseHttpEndpoint(string httpEndpoint)
Expand Down
6 changes: 6 additions & 0 deletions src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ static bool TryGetGrpcAddress(out string address)
// 1. DaprDefaults.cs uses 127.0.0.1 instead of localhost, which prevents testing with Dapr on WSL2 and the app on Windows
// 2. DaprDefaults.cs doesn't compile when the project has C# nullable reference types enabled.
// If the above issues are fixed (ensuring we don't regress anything) we should switch to using the logic in DaprDefaults.cs.
string? daprEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT");
if (!String.IsNullOrEmpty(daprEndpoint)) {
address = daprEndpoint;
return true;
}

string? daprPortStr = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT");
if (int.TryParse(daprPortStr, out int daprGrpcPort))
{
Expand Down
20 changes: 16 additions & 4 deletions src/Shared/DaprDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ public static string GetDefaultAppApiToken()
}

/// <summary>
/// Get the value of environment variable DAPR_HTTP_PORT
/// Get the value of HTTP endpoint based off environment variables
/// </summary>
/// <returns>The value of environment variable DAPR_HTTP_PORT</returns>
/// <returns>The value of HTTP endpoint based off environment variables</returns>
public static string GetDefaultHttpEndpoint()
{
if (httpEndpoint == null)
{
var endpoint = Environment.GetEnvironmentVariable("DAPR_HTTP_ENDPOINT");
if (!string.IsNullOrEmpty(endpoint)) {
httpEndpoint = endpoint;
return httpEndpoint;
}

var port = Environment.GetEnvironmentVariable("DAPR_HTTP_PORT");
port = string.IsNullOrEmpty(port) ? "3500" : port;
httpEndpoint = $"http://127.0.0.1:{port}";
Expand All @@ -73,13 +79,19 @@ public static string GetDefaultHttpEndpoint()
}

/// <summary>
/// Get the value of environment variable DAPR_GRPC_PORT
/// Get the value of gRPC endpoint based off environment variables
/// </summary>
/// <returns>The value of environment variable DAPR_GRPC_PORT</returns>
/// <returns>The value of gRPC endpoint based off environment variables</returns>
public static string GetDefaultGrpcEndpoint()
{
if (grpcEndpoint == null)
{
var endpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT");
if (!string.IsNullOrEmpty(endpoint)) {
grpcEndpoint = endpoint;
return grpcEndpoint;
}

var port = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT");
port = string.IsNullOrEmpty(port) ? "50001" : port;
grpcEndpoint = $"http://127.0.0.1:{port}";
Expand Down

0 comments on commit f788efa

Please sign in to comment.