Skip to content

Commit cd3dc00

Browse files
author
David Lievrouw
committed
Add ability to specify scheme of ServiceProviderConfiguration
1 parent 347ea72 commit cd3dc00

25 files changed

+86
-18
lines changed

src/Ocelot.Provider.Consul/ConsulClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public IConsulClient Get(ConsulRegistryConfiguration config)
99
{
1010
return new ConsulClient(c =>
1111
{
12-
c.Address = new Uri($"http://{config.Host}:{config.Port}");
12+
c.Address = new Uri($"{config.Scheme}://{config.Host}:{config.Port}");
1313

1414
if (!string.IsNullOrEmpty(config?.Token))
1515
{

src/Ocelot.Provider.Consul/ConsulProviderFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class ConsulProviderFactory
1212

1313
var consulFactory = provider.GetService<IConsulClientFactory>();
1414

15-
var consulRegistryConfiguration = new ConsulRegistryConfiguration(config.Host, config.Port, reRoute.ServiceName, config.Token);
15+
var consulRegistryConfiguration = new ConsulRegistryConfiguration(config.Scheme, config.Host, config.Port, reRoute.ServiceName, config.Token);
1616

1717
var consulServiceDiscoveryProvider = new Consul(consulRegistryConfiguration, factory, consulFactory);
1818

src/Ocelot.Provider.Consul/ConsulRegistryConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
{
33
public class ConsulRegistryConfiguration
44
{
5-
public ConsulRegistryConfiguration(string host, int port, string keyOfServiceInConsul, string token)
5+
public ConsulRegistryConfiguration(string scheme, string host, int port, string keyOfServiceInConsul, string token)
66
{
77
Host = string.IsNullOrEmpty(host) ? "localhost" : host;
88
Port = port > 0 ? port : 8500;
9+
Scheme = string.IsNullOrEmpty(scheme) ? "http" : scheme;
910
KeyOfServiceInConsul = keyOfServiceInConsul;
1011
Token = token;
1112
}
1213

1314
public string KeyOfServiceInConsul { get; }
15+
public string Scheme { get; }
1416
public string Host { get; }
1517
public int Port { get; }
1618
public string Token { get; }

src/Ocelot/Configuration/Builder/ServiceProviderConfigurationBuilder.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Ocelot.Configuration.Builder
22
{
33
public class ServiceProviderConfigurationBuilder
44
{
5+
private string _serviceDiscoveryProviderScheme;
56
private string _serviceDiscoveryProviderHost;
67
private int _serviceDiscoveryProviderPort;
78
private string _type;
@@ -10,6 +11,12 @@ public class ServiceProviderConfigurationBuilder
1011
private int _pollingInterval;
1112
private string _namespace;
1213

14+
public ServiceProviderConfigurationBuilder WithScheme(string serviceDiscoveryProviderScheme)
15+
{
16+
_serviceDiscoveryProviderScheme = serviceDiscoveryProviderScheme;
17+
return this;
18+
}
19+
1320
public ServiceProviderConfigurationBuilder WithHost(string serviceDiscoveryProviderHost)
1421
{
1522
_serviceDiscoveryProviderHost = serviceDiscoveryProviderHost;
@@ -54,7 +61,7 @@ public ServiceProviderConfigurationBuilder WithNamespace(string @namespace)
5461

5562
public ServiceProviderConfiguration Build()
5663
{
57-
return new ServiceProviderConfiguration(_type, _serviceDiscoveryProviderHost, _serviceDiscoveryProviderPort, _token, _configurationKey, _pollingInterval, _namespace);
64+
return new ServiceProviderConfiguration(_type, _serviceDiscoveryProviderScheme, _serviceDiscoveryProviderHost, _serviceDiscoveryProviderPort, _token, _configurationKey, _pollingInterval, _namespace);
5865
}
5966
}
6067
}

src/Ocelot/Configuration/Creator/ServiceProviderConfigurationCreator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class ServiceProviderConfigurationCreator : IServiceProviderConfiguration
88
public ServiceProviderConfiguration Create(FileGlobalConfiguration globalConfiguration)
99
{
1010
var port = globalConfiguration?.ServiceDiscoveryProvider?.Port ?? 0;
11+
var scheme = globalConfiguration?.ServiceDiscoveryProvider?.Scheme ?? "http";
1112
var host = globalConfiguration?.ServiceDiscoveryProvider?.Host ?? "localhost";
1213
var type = !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Type)
1314
? globalConfiguration?.ServiceDiscoveryProvider?.Type
@@ -16,6 +17,7 @@ public ServiceProviderConfiguration Create(FileGlobalConfiguration globalConfigu
1617
var k8snamespace = globalConfiguration?.ServiceDiscoveryProvider?.Namespace ?? string.Empty;
1718

1819
return new ServiceProviderConfigurationBuilder()
20+
.WithScheme(scheme)
1921
.WithHost(host)
2022
.WithPort(port)
2123
.WithType(type)

src/Ocelot/Configuration/File/FileServiceDiscoveryProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Ocelot.Configuration.File
22
{
33
public class FileServiceDiscoveryProvider
44
{
5+
public string Scheme { get; set; }
56
public string Host { get; set; }
67
public int Port { get; set; }
78
public string Type { get; set; }

src/Ocelot/Configuration/ServiceProviderConfiguration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
{
33
public class ServiceProviderConfiguration
44
{
5-
public ServiceProviderConfiguration(string type, string host, int port, string token, string configurationKey, int pollingInterval, string @namespace = "")
5+
public ServiceProviderConfiguration(string type, string scheme, string host, int port, string token, string configurationKey, int pollingInterval, string @namespace = "")
66
{
77
ConfigurationKey = configurationKey;
8+
Scheme = scheme;
89
Host = host;
910
Port = port;
1011
Token = token;
@@ -13,6 +14,8 @@ public ServiceProviderConfiguration(string type, string host, int port, string t
1314
Namespace = @namespace;
1415
}
1516

17+
public string Scheme { get; }
18+
1619
public string Host { get; }
1720

1821
public int Port { get; }

test/Ocelot.AcceptanceTests/CannotStartOcelotTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public void should_throw_exception_if_cannot_start_because_service_discovery_pro
2424
{
2525
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
2626
{
27+
Scheme = "https",
2728
Host = "localhost",
2829
Type = "consul",
2930
Port = 8500
@@ -66,6 +67,7 @@ public void should_throw_exception_if_cannot_start_because_service_discovery_pro
6667
{
6768
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
6869
{
70+
Scheme = "https",
6971
Host = "localhost",
7072
Type = "consul",
7173
Port = 8500

test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void should_return_response_200_with_simple_url_when_using_jsonserialized
5959
{
6060
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
6161
{
62+
Scheme = "http",
6263
Host = "localhost",
6364
Port = consulPort
6465
}

test/Ocelot.AcceptanceTests/ConsulConfigurationInConsulTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void should_return_response_200_with_simple_url()
6161
{
6262
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
6363
{
64+
Scheme = "http",
6465
Host = "localhost",
6566
Port = consulPort
6667
}
@@ -91,6 +92,7 @@ public void should_load_configuration_out_of_consul()
9192
{
9293
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
9394
{
95+
Scheme = "http",
9496
Host = "localhost",
9597
Port = consulPort
9698
}
@@ -123,6 +125,7 @@ public void should_load_configuration_out_of_consul()
123125
{
124126
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
125127
{
128+
Scheme = "http",
126129
Host = "localhost",
127130
Port = consulPort
128131
}
@@ -152,6 +155,7 @@ public void should_load_configuration_out_of_consul_if_it_is_changed()
152155
{
153156
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
154157
{
158+
Scheme = "http",
155159
Host = "localhost",
156160
Port = consulPort
157161
}
@@ -184,6 +188,7 @@ public void should_load_configuration_out_of_consul_if_it_is_changed()
184188
{
185189
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
186190
{
191+
Scheme = "http",
187192
Host = "localhost",
188193
Port = consulPort
189194
}
@@ -214,6 +219,7 @@ public void should_load_configuration_out_of_consul_if_it_is_changed()
214219
{
215220
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
216221
{
222+
Scheme = "http",
217223
Host = "localhost",
218224
Port = consulPort
219225
}
@@ -274,6 +280,7 @@ public void should_handle_request_to_consul_for_downstream_service_and_make_requ
274280
{
275281
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
276282
{
283+
Scheme = "http",
277284
Host = "localhost",
278285
Port = consulPort
279286
},
@@ -295,6 +302,7 @@ public void should_handle_request_to_consul_for_downstream_service_and_make_requ
295302
{
296303
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
297304
{
305+
Scheme = "http",
298306
Host = "localhost",
299307
Port = consulPort
300308
}

0 commit comments

Comments
 (0)