Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public sealed class ConsulDiscoveryOptions

internal bool IsHeartbeatEnabled => Heartbeat is { Enabled: true };
internal bool IsRetryEnabled => Retry is { Enabled: true };
internal string EffectiveScheme => Scheme ?? "http";

/// <summary>
/// Gets or sets a value indicating whether to enable the Consul client. Default value: true.
Expand Down Expand Up @@ -58,9 +59,9 @@ public sealed class ConsulDiscoveryOptions
public bool QueryPassing { get; set; } = true;

/// <summary>
/// Gets or sets the scheme to register the running app with ("http" or "https"). Default value: http.
/// Gets or sets the scheme to register the running app with ("http" or "https").
/// </summary>
public string? Scheme { get; set; } = "http";
public string? Scheme { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to enable periodic health checking for the running app. Default value: true.
Expand Down
2 changes: 1 addition & 1 deletion src/Discovery/src/Consul/ConfigurationSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
},
"Scheme": {
"type": "string",
"description": "Gets or sets the scheme to register the running app with (\"http\" or \"https\"). Default value: http."
"description": "Gets or sets the scheme to register the running app with (\"http\" or \"https\")."
},
"ServiceName": {
"type": "string",
Expand Down
18 changes: 11 additions & 7 deletions src/Discovery/src/Consul/PostConfigureConsulDiscoveryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void PostConfigure(string? name, ConsulDiscoveryOptions options)
if (options.Port == 0)
{
ICollection<string> addresses = _configuration.GetListenAddresses();
SetPortsFromListenAddresses(options, addresses);
SetSchemeWithPortFromListenAddresses(options, addresses);
}

options.InstanceId = GetInstanceId(options);
Expand All @@ -77,11 +77,11 @@ private string GetServiceName(ConsulDiscoveryOptions options)
return NormalizeForConsul(serviceName, nameof(ConsulDiscoveryOptions.ServiceName));
}

private void SetPortsFromListenAddresses(ConsulDiscoveryOptions options, IEnumerable<string> listenOnAddresses)
private void SetSchemeWithPortFromListenAddresses(ConsulDiscoveryOptions options, IEnumerable<string> listenOnAddresses)
{
// Try to pull some values out of server configuration to override defaults, but only if not using NetUtils.
// If NetUtils are configured, the user probably wants to define their own behavior.
if (options is { UseAspNetCoreUrls: true, Port: 0 })
if (options is { UseAspNetCoreUrls: true, Port: 0, Scheme: null })
{
int? listenHttpPort = null;
int? listenHttpsPort = null;
Expand All @@ -100,11 +100,15 @@ private void SetPortsFromListenAddresses(ConsulDiscoveryOptions options, IEnumer
}
}

int? listenPort = listenHttpsPort ?? listenHttpPort;

if (listenPort != null)
if (listenHttpsPort != null)
{
options.Port = listenHttpsPort.Value;
options.Scheme = "https";
}
else if (listenHttpPort != null)
{
options.Port = listenPort.Value;
options.Port = listenHttpPort.Value;
options.Scheme = "http";
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Discovery/src/Consul/Registry/ConsulRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ internal sealed class ConsulRegistration : IServiceInstance
public int Port { get; }

/// <inheritdoc />
public bool IsSecure => _optionsMonitor.CurrentValue.Scheme == "https";
public bool IsSecure => _optionsMonitor.CurrentValue.EffectiveScheme == "https";

/// <inheritdoc />
public Uri Uri => new($"{_optionsMonitor.CurrentValue.Scheme}://{Host}:{Port}");
public Uri Uri => new($"{_optionsMonitor.CurrentValue.EffectiveScheme}://{Host}:{Port}");

public IReadOnlyList<string> Tags { get; }

Expand Down Expand Up @@ -114,7 +114,7 @@ private static Dictionary<string, string> CreateMetadata(ConsulDiscoveryOptions
}

// store the secure flag in the metadata so that clients will be able to figure out whether to use http or https automatically
metadata.TryAdd("secure", options.Scheme == "https" ? "true" : "false");
metadata.TryAdd("secure", options.EffectiveScheme == "https" ? "true" : "false");

return metadata;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public static AgentServiceCheck CreateCheck(int port, ConsulDiscoveryOptions opt
}
else
{
var uri = new Uri($"{options.Scheme}://{options.HostName}:{port}{options.HealthCheckPath}");
var uri = new Uri($"{options.EffectiveScheme}://{options.HostName}:{port}{options.HealthCheckPath}");
check.HTTP = uri.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void Constructor_InitializesDefaults()
options.InstanceZone.Should().BeNull();
options.PreferIPAddress.Should().BeFalse();
options.QueryPassing.Should().BeTrue();
options.Scheme.Should().Be("http");
options.Scheme.Should().BeNull();
options.EffectiveScheme.Should().Be("http");
options.ServiceName.Should().BeNull();
options.Tags.Should().BeEmpty();
options.Metadata.Should().BeEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void CreateCheck_ReturnsExpected()
options.Heartbeat = null;
const int port = 1234;
result = ConsulRegistration.CreateCheck(port, options);
var uri = new Uri($"{options.Scheme}://{options.HostName}:{port}{options.HealthCheckPath}");
var uri = new Uri($"{options.EffectiveScheme}://{options.HostName}:{port}{options.HealthCheckPath}");

result.HTTP.Should().Be(uri.ToString());
result.Interval.Should().Be(DateTimeConversions.ToTimeSpan(options.HealthCheckInterval!));
Expand Down
Loading