Skip to content

Commit a22d925

Browse files
Mantas Audickasmantasaudickas
authored andcommitted
add downstream service name finder (#1188)
1 parent 99a15d8 commit a22d925

File tree

7 files changed

+72
-16
lines changed

7 files changed

+72
-16
lines changed

src/Ocelot/DependencyInjection/OcelotBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
108108
Services.TryAddSingleton<IDownstreamPathPlaceholderReplacer, DownstreamTemplatePathPlaceholderReplacer>();
109109
Services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteFinder>();
110110
Services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteCreator>();
111+
Services.TryAddSingleton<IDownstreamServiceFinder, DownstreamServiceFinder>();
111112
Services.TryAddSingleton<IDownstreamRouteProviderFactory, DownstreamRouteProviderFactory>();
112113
Services.TryAddSingleton<IHttpRequester, HttpClientHttpRequester>();
113114
Services.TryAddSingleton<IHttpResponder, HttpContextResponder>();

src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
public class DownstreamRouteCreator : IDownstreamRouteProvider
1515
{
1616
private readonly IQoSOptionsCreator _qoSOptionsCreator;
17+
private readonly IDownstreamServiceFinder _serviceFinder;
1718
private readonly ConcurrentDictionary<string, OkResponse<DownstreamRoute>> _cache;
1819

19-
public DownstreamRouteCreator(IQoSOptionsCreator qoSOptionsCreator)
20+
public DownstreamRouteCreator(IQoSOptionsCreator qoSOptionsCreator, IDownstreamServiceFinder serviceFinder)
2021
{
2122
_qoSOptionsCreator = qoSOptionsCreator;
23+
_serviceFinder = serviceFinder;
2224
_cache = new ConcurrentDictionary<string, OkResponse<DownstreamRoute>>();
2325
}
2426

2527
public Response<DownstreamRoute> Get(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod, IInternalConfiguration configuration, string upstreamHost)
2628
{
27-
var serviceName = GetServiceName(upstreamUrlPath);
29+
var serviceName = _serviceFinder.GetServiceName(upstreamUrlPath, upstreamQueryString, upstreamHttpMethod, upstreamHost, configuration);
2830

2931
var downstreamPath = GetDownstreamPath(upstreamUrlPath);
3032

@@ -108,19 +110,6 @@ private static string GetDownstreamPath(string upstreamUrlPath)
108110
.Substring(upstreamUrlPath.IndexOf('/', 1));
109111
}
110112

111-
private static string GetServiceName(string upstreamUrlPath)
112-
{
113-
if (upstreamUrlPath.IndexOf('/', 1) == -1)
114-
{
115-
return upstreamUrlPath
116-
.Substring(1);
117-
}
118-
119-
return upstreamUrlPath
120-
.Substring(1, upstreamUrlPath.IndexOf('/', 1))
121-
.TrimEnd('/');
122-
}
123-
124113
private string CreateLoadBalancerKey(string downstreamTemplatePath, string httpMethod, LoadBalancerOptions loadBalancerOptions)
125114
{
126115
if (!string.IsNullOrEmpty(loadBalancerOptions.Type) && !string.IsNullOrEmpty(loadBalancerOptions.Key) && loadBalancerOptions.Type == nameof(CookieStickySessions))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Ocelot.Configuration;
2+
3+
namespace Ocelot.DownstreamRouteFinder.Finder
4+
{
5+
public class DownstreamServiceFinder: IDownstreamServiceFinder
6+
{
7+
public string GetServiceName(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod, string upstreamHost, IInternalConfiguration configuration)
8+
{
9+
if (upstreamUrlPath.IndexOf('/', 1) == -1)
10+
{
11+
return upstreamUrlPath
12+
.Substring(1);
13+
}
14+
15+
return upstreamUrlPath
16+
.Substring(1, upstreamUrlPath.IndexOf('/', 1))
17+
.TrimEnd('/');
18+
}
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Ocelot.Configuration;
2+
3+
namespace Ocelot.DownstreamRouteFinder.Finder
4+
{
5+
public interface IDownstreamServiceFinder
6+
{
7+
string GetServiceName(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod, string upstreamHost, IInternalConfiguration configuration);
8+
}
9+
}

test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public DownstreamRouteCreatorTests()
3939
_qosOptionsCreator
4040
.Setup(x => x.Create(It.IsAny<QoSOptions>(), It.IsAny<string>(), It.IsAny<List<string>>()))
4141
.Returns(_qoSOptions);
42-
_creator = new DownstreamRouteCreator(_qosOptionsCreator.Object);
42+
_creator = new DownstreamRouteCreator(_qosOptionsCreator.Object, new DownstreamServiceFinder());
4343
}
4444

4545
[Fact]

test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public DownstreamRouteProviderFactoryTests()
3030
services.AddSingleton<IQoSOptionsCreator, QoSOptionsCreator>();
3131
services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteFinder>();
3232
services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteCreator>();
33+
services.AddSingleton<IDownstreamServiceFinder, DownstreamServiceFinder>();
3334
var provider = services.BuildServiceProvider();
3435
_logger = new Mock<IOcelotLogger>();
3536
_loggerFactory = new Mock<IOcelotLoggerFactory>();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Ocelot.DownstreamRouteFinder.Finder;
2+
using Xunit;
3+
4+
namespace Ocelot.UnitTests.DownstreamRouteFinder
5+
{
6+
public class DownstreamServiceFinderTests
7+
{
8+
private readonly DownstreamServiceFinder _serviceFinder;
9+
10+
public DownstreamServiceFinderTests()
11+
{
12+
_serviceFinder = new DownstreamServiceFinder();
13+
}
14+
15+
[Fact]
16+
public void should_return_empty_when_without_path()
17+
{
18+
var serviceName = _serviceFinder.GetServiceName("/");
19+
Assert.Equal("", serviceName);
20+
}
21+
22+
[Fact]
23+
public void should_return_node_name_when_single_node()
24+
{
25+
var serviceName = _serviceFinder.GetServiceName("/service-name");
26+
Assert.Equal("service-name", serviceName);
27+
}
28+
29+
[Fact]
30+
public void should_return_first_node_name_when_multiple_nodes()
31+
{
32+
var serviceName = _serviceFinder.GetServiceName("/service-name/some/longer/path");
33+
Assert.Equal("service-name", serviceName);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)