Skip to content

Commit

Permalink
Updated to put endpoint resolution mechanism in DaprDefaults within D…
Browse files Browse the repository at this point in the history
…apr.Common - updating projects and unit tests

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Oct 16, 2024
1 parent 55f375f commit 48e4de8
Show file tree
Hide file tree
Showing 15 changed files with 723 additions and 913 deletions.
7 changes: 7 additions & 0 deletions all.sln
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Protos", "src\Dapr.Pro
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Common\Dapr.Common.csproj", "{B445B19C-A925-4873-8CB7-8317898B6970}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -302,6 +304,10 @@ Global
{B445B19C-A925-4873-8CB7-8317898B6970}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.Build.0 = Release|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -357,6 +363,7 @@ Global
{C74FBA78-13E8-407F-A173-4555AEE41FF3} = {A7F41094-8648-446B-AECD-DCC2CC871F73}
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40}
Expand Down
110 changes: 13 additions & 97 deletions src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ public static void AddActors(this IServiceCollection? services, Action<ActorRunt
{
var options = s.GetRequiredService<IOptions<ActorRuntimeOptions>>().Value;
//Replace the HttpEndpoint with an endpoint prioritizing IConfiguration
var configuration = s.GetService<IConfiguration>();
options.HttpEndpoint = options.HttpEndpoint != "http://127.0.0.1:3500"
? GetHttpEndpoint(configuration)
: options.HttpEndpoint;
options.DaprApiToken = string.IsNullOrWhiteSpace(options.DaprApiToken)
? GetApiToken(configuration)
: options.DaprApiToken;
options.DaprApiToken = !string.IsNullOrWhiteSpace(options.DaprApiToken)
? options.DaprApiToken
: DaprDefaults.GetDefaultDaprApiToken(configuration);
options.HttpEndpoint = !string.IsNullOrWhiteSpace(options.HttpEndpoint)
? options.HttpEndpoint
: DaprDefaults.GetDefaultHttpEndpoint();
var loggerFactory = s.GetRequiredService<ILoggerFactory>();
var activatorFactory = s.GetRequiredService<ActorActivatorFactory>();
var proxyFactory = s.GetRequiredService<IActorProxyFactory>();
Expand All @@ -68,12 +67,12 @@ public static void AddActors(this IServiceCollection? services, Action<ActorRunt
//Replace the HttpEndpoint with an endpoint prioritizing IConfiguration
var configuration = s.GetService<IConfiguration>();
options.HttpEndpoint = options.HttpEndpoint != "http://127.0.0.1:3500"
? GetHttpEndpoint(configuration)
: options.HttpEndpoint;
options.DaprApiToken = string.IsNullOrWhiteSpace(options.DaprApiToken)
? GetApiToken(configuration)
: options.DaprApiToken;
options.DaprApiToken = !string.IsNullOrWhiteSpace(options.DaprApiToken)
? options.DaprApiToken
: DaprDefaults.GetDefaultDaprApiToken(configuration);
options.HttpEndpoint = !string.IsNullOrWhiteSpace(options.HttpEndpoint)
? options.HttpEndpoint
: DaprDefaults.GetDefaultHttpEndpoint();
var factory = new ActorProxyFactory()
{
Expand All @@ -93,88 +92,5 @@ public static void AddActors(this IServiceCollection? services, Action<ActorRunt
services.Configure<ActorRuntimeOptions>(configure);
}
}

/// <summary>
/// Retrieves the Dapr API token using a failover approach starting with an optional <see cref="IConfiguration"/>
/// instance, then trying to pull from the well-known environment variable name and then opting for an empty string
/// as a default value.
/// </summary>
/// <returns>The Dapr API token.</returns>
private static string GetApiToken(IConfiguration? configuration) => GetResourceValue(configuration, DaprDefaults.DaprApiTokenName);

/// <summary>
/// Builds the Dapr gRPC endpoint using the value from the IConfiguration, if available, then falling back
/// to the value in the environment variable(s) and finally otherwise using the default value (an empty string).
/// </summary>
/// <remarks>
/// Marked as internal for testing purposes.
/// </remarks>
/// <param name="configuration">An injected instance of the <see cref="IConfiguration"/>.</param>
/// <returns>The built gRPC endpoint.</returns>
private static string GetHttpEndpoint(IConfiguration? configuration)
{
//Prioritize pulling from IConfiguration with a fallback from pulling from the environment variable directly
var httpEndpoint = GetResourceValue(configuration, DaprDefaults.DaprHttpEndpointName);
var httpPort = GetResourceValue(configuration, DaprDefaults.DaprHttpPortName);
int? parsedGrpcPort = string.IsNullOrWhiteSpace(httpPort) ? null : int.Parse(httpPort);

var endpoint = BuildEndpoint(httpEndpoint, parsedGrpcPort);
return string.IsNullOrWhiteSpace(endpoint) ? $"http://localhost:{DaprDefaults.DefaultHttpPort}/" : endpoint;
}

/// <summary>
/// Retrieves the specified value prioritizing pulling it from <see cref="IConfiguration"/>, falling back
/// to an environment variable, and using an empty string as a default.
/// </summary>
/// <param name="configuration">An instance of an <see cref="IConfiguration"/>.</param>
/// <param name="name">The name of the value to retrieve.</param>
/// <returns>The value of the resource.</returns>
private static string GetResourceValue(IConfiguration? configuration, string name)
{
//Attempt to retrieve first from the configuration
var configurationValue = configuration?.GetValue<string?>(name);
if (configurationValue is not null)
return configurationValue;

//Fall back to the environment variable with the same name or default to an empty string
var envVar = Environment.GetEnvironmentVariable(name);
return envVar ?? string.Empty;
}

/// <summary>
/// Builds the endpoint provided an optional endpoint and optional port.
/// </summary>
/// <remarks>
/// Marked as internal for testing purposes.
/// </remarks>
/// <param name="endpoint">The endpoint.</param>
/// <param name="endpointPort">The port</param>
/// <returns>A constructed endpoint value.</returns>
internal static string BuildEndpoint(string? endpoint, int? endpointPort)
{
if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is null)
return string.Empty;

var endpointBuilder = new UriBuilder();
if (!string.IsNullOrWhiteSpace(endpoint))
{
//Extract the scheme, host and port from the endpoint
var uri = new Uri(endpoint);
endpointBuilder.Scheme = uri.Scheme;
endpointBuilder.Host = uri.Host;
endpointBuilder.Port = uri.Port;

//Update the port if provided separately
if (endpointPort is not null)
endpointBuilder.Port = (int)endpointPort;
}
else if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is not null)
{
endpointBuilder.Host = "localhost";
endpointBuilder.Port = (int)endpointPort;
}

return endpointBuilder.ToString();
}
}
}
1 change: 1 addition & 0 deletions src/Dapr.Actors.AspNetCore/Dapr.Actors.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@

<ItemGroup>
<ProjectReference Include="..\Dapr.Actors\Dapr.Actors.csproj" />
<ProjectReference Include="..\Dapr.Common\Dapr.Common.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Dapr.Actors/Client/ActorProxyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public JsonSerializerOptions JsonSerializerOptions
/// <summary>
/// The Dapr Api Token that is added to the header for all requests.
/// </summary>
public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken();
public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken(null);

/// <summary>
/// Gets or sets the HTTP endpoint URI used to communicate with the Dapr sidecar.
Expand Down
22 changes: 7 additions & 15 deletions src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// limitations under the License.
// ------------------------------------------------------------------------

#nullable enable

using System;
using System.Text.Json;

Expand All @@ -34,7 +36,7 @@ public sealed class ActorRuntimeOptions
};
private bool useJsonSerialization = false;
private JsonSerializerOptions jsonSerializerOptions = JsonSerializerDefaults.Web;
private string daprApiToken = DaprDefaults.GetDefaultDaprApiToken();
private string daprApiToken = string.Empty;
private int? remindersStoragePartitions = null;

/// <summary>
Expand Down Expand Up @@ -180,19 +182,14 @@ public JsonSerializerOptions JsonSerializerOptions

set
{
if (value is null)
{
throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null");
}

this.jsonSerializerOptions = value;
this.jsonSerializerOptions = value ?? throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null");
}
}

/// <summary>
/// The <see cref="DaprApiToken"/> to add to the headers in requests to Dapr runtime
/// </summary>
public string DaprApiToken
public string? DaprApiToken
{
get
{
Expand All @@ -201,12 +198,7 @@ public string DaprApiToken

set
{
if (value is null)
{
throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null");
}

this.daprApiToken = value;
this.daprApiToken = value ?? throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null");
}
}

Expand Down Expand Up @@ -241,6 +233,6 @@ public int? RemindersStoragePartitions
/// corresponding environment variables.
/// </remarks>
/// <value></value>
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();
public string? HttpEndpoint { get; set; }
}
}
3 changes: 1 addition & 2 deletions src/Dapr.AspNetCore/DaprAuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using Microsoft.AspNetCore.Authentication;

namespace Dapr.AspNetCore
Expand All @@ -29,6 +28,6 @@ public class DaprAuthenticationOptions : AuthenticationSchemeOptions
/// Gets or sets the App API token.
/// By default, the token will be read from the APP_API_TOKEN environment variable.
/// </summary>
public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken();
public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken(null);
}
}
Loading

0 comments on commit 48e4de8

Please sign in to comment.