Skip to content

Commit

Permalink
#340 renamed stuff to provider rather than finder, as its not longer …
Browse files Browse the repository at this point in the history
…finding anything
  • Loading branch information
Tom Gardham-Pallister committed May 10, 2018
1 parent 81f920f commit ed695e3
Show file tree
Hide file tree
Showing 9 changed files with 879 additions and 882 deletions.
6 changes: 3 additions & 3 deletions src/Ocelot/DependencyInjection/OcelotBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
_services.TryAddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
_services.TryAddSingleton<IPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
_services.TryAddSingleton<IDownstreamPathPlaceholderReplacer, DownstreamTemplatePathPlaceholderReplacer>();
_services.TryAddSingleton<IDownstreamRouteFinder, DownstreamRouteFinder>();
_services.TryAddSingleton<IDownstreamRouteFinder, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
_services.TryAddSingleton<IDownstreamRouteFinderFactory, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinderFactory>();
_services.TryAddSingleton<IDownstreamRouteProvider, DownstreamRouteProvider>();
_services.TryAddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
_services.TryAddSingleton<IDownstreamRouteProviderFactory, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProviderFactory>();
_services.TryAddSingleton<IHttpRequester, HttpClientHttpRequester>();
_services.TryAddSingleton<IHttpResponder, HttpContextResponder>();
_services.TryAddSingleton<IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
using System.Collections.Generic;
using System.Linq;
using Ocelot.Configuration;
using Ocelot.DownstreamRouteFinder.UrlMatcher;
using Ocelot.Errors;
using Ocelot.Responses;

namespace Ocelot.DownstreamRouteFinder.Finder
{
public class DownstreamRouteFinder : IDownstreamRouteFinder
{
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
private readonly IPlaceholderNameAndValueFinder _placeholderNameAndValueFinder;

public DownstreamRouteFinder(IUrlPathToUrlTemplateMatcher urlMatcher, IPlaceholderNameAndValueFinder urlPathPlaceholderNameAndValueFinder)
{
_urlMatcher = urlMatcher;
_placeholderNameAndValueFinder = urlPathPlaceholderNameAndValueFinder;
}

public Response<DownstreamRoute> FindDownstreamRoute(string path, string httpMethod, IInternalConfiguration configuration, string upstreamHost)
{
var downstreamRoutes = new List<DownstreamRoute>();

var applicableReRoutes = configuration.ReRoutes
.Where(r => RouteIsApplicableToThisRequest(r, httpMethod, upstreamHost))
.OrderByDescending(x => x.UpstreamTemplatePattern.Priority);

foreach (var reRoute in applicableReRoutes)
{
var urlMatch = _urlMatcher.Match(path, reRoute.UpstreamTemplatePattern.Template);

if (urlMatch.Data.Match)
{
downstreamRoutes.Add(GetPlaceholderNamesAndValues(path, reRoute));
}
}

if (downstreamRoutes.Any())
{
var notNullOption = downstreamRoutes.FirstOrDefault(x => !string.IsNullOrEmpty(x.ReRoute.UpstreamHost));
var nullOption = downstreamRoutes.FirstOrDefault(x => string.IsNullOrEmpty(x.ReRoute.UpstreamHost));

return notNullOption != null ? new OkResponse<DownstreamRoute>(notNullOption) : new OkResponse<DownstreamRoute>(nullOption);
}

return new ErrorResponse<DownstreamRoute>(new UnableToFindDownstreamRouteError(path, httpMethod));
}

private bool RouteIsApplicableToThisRequest(ReRoute reRoute, string httpMethod, string upstreamHost)
{
return reRoute.UpstreamHttpMethod.Count == 0 || reRoute.UpstreamHttpMethod.Select(x => x.Method.ToLower()).Contains(httpMethod.ToLower()) && !(!string.IsNullOrEmpty(reRoute.UpstreamHost) && reRoute.UpstreamHost != upstreamHost);
}

private DownstreamRoute GetPlaceholderNamesAndValues(string path, ReRoute reRoute)
{
var templatePlaceholderNameAndValues = _placeholderNameAndValueFinder.Find(path, reRoute.UpstreamPathTemplate.Value);

return new DownstreamRoute(templatePlaceholderNameAndValues.Data, reRoute);
}
}
}
using System.Collections.Generic;
using System.Linq;
using Ocelot.Configuration;
using Ocelot.DownstreamRouteFinder.UrlMatcher;
using Ocelot.Errors;
using Ocelot.Responses;

namespace Ocelot.DownstreamRouteFinder.Finder
{
public class DownstreamRouteProvider : IDownstreamRouteProvider
{
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
private readonly IPlaceholderNameAndValueFinder _placeholderNameAndValueFinder;

public DownstreamRouteProvider(IUrlPathToUrlTemplateMatcher urlMatcher, IPlaceholderNameAndValueFinder urlPathPlaceholderNameAndValueFinder)
{
_urlMatcher = urlMatcher;
_placeholderNameAndValueFinder = urlPathPlaceholderNameAndValueFinder;
}

public Response<DownstreamRoute> Get(string path, string httpMethod, IInternalConfiguration configuration, string upstreamHost)
{
var downstreamRoutes = new List<DownstreamRoute>();

var applicableReRoutes = configuration.ReRoutes
.Where(r => RouteIsApplicableToThisRequest(r, httpMethod, upstreamHost))
.OrderByDescending(x => x.UpstreamTemplatePattern.Priority);

foreach (var reRoute in applicableReRoutes)
{
var urlMatch = _urlMatcher.Match(path, reRoute.UpstreamTemplatePattern.Template);

if (urlMatch.Data.Match)
{
downstreamRoutes.Add(GetPlaceholderNamesAndValues(path, reRoute));
}
}

if (downstreamRoutes.Any())
{
var notNullOption = downstreamRoutes.FirstOrDefault(x => !string.IsNullOrEmpty(x.ReRoute.UpstreamHost));
var nullOption = downstreamRoutes.FirstOrDefault(x => string.IsNullOrEmpty(x.ReRoute.UpstreamHost));

return notNullOption != null ? new OkResponse<DownstreamRoute>(notNullOption) : new OkResponse<DownstreamRoute>(nullOption);
}

return new ErrorResponse<DownstreamRoute>(new UnableToFindDownstreamRouteError(path, httpMethod));
}

private bool RouteIsApplicableToThisRequest(ReRoute reRoute, string httpMethod, string upstreamHost)
{
return reRoute.UpstreamHttpMethod.Count == 0 || reRoute.UpstreamHttpMethod.Select(x => x.Method.ToLower()).Contains(httpMethod.ToLower()) && !(!string.IsNullOrEmpty(reRoute.UpstreamHost) && reRoute.UpstreamHost != upstreamHost);
}

private DownstreamRoute GetPlaceholderNamesAndValues(string path, ReRoute reRoute)
{
var templatePlaceholderNameAndValues = _placeholderNameAndValueFinder.Find(path, reRoute.UpstreamPathTemplate.Value);

return new DownstreamRoute(templatePlaceholderNameAndValues.Data, reRoute);
}
}
}
61 changes: 0 additions & 61 deletions src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteFinder.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Threading.Tasks;
using Ocelot.Configuration;
using Ocelot.Responses;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;

namespace Ocelot.DownstreamRouteFinder.Finder
{
public interface IDownstreamRouteProvider
{
Response<DownstreamRoute> Get(string upstreamUrlPath, string upstreamHttpMethod, IInternalConfiguration configuration, string upstreamHost);
}

public class DownstreamRouteCreator : IDownstreamRouteProvider
{
public Response<DownstreamRoute> Get(string upstreamUrlPath, string upstreamHttpMethod, IInternalConfiguration configuration, string upstreamHost)
{
throw new NotImplementedException();
}
}

public interface IDownstreamRouteProviderFactory
{
IDownstreamRouteProvider Get(IInternalConfiguration config);
}

public class DownstreamRouteProviderFactory : IDownstreamRouteProviderFactory
{
private readonly Dictionary<string, IDownstreamRouteProvider> _providers;

public DownstreamRouteProviderFactory(IServiceProvider provider)
{
_providers = provider.GetServices<IDownstreamRouteProvider>().ToDictionary(x => x.GetType().Name);
}

public IDownstreamRouteProvider Get(IInternalConfiguration config)
{
if(!config.ReRoutes.Any() && IsServiceDiscovery(config.ServiceProviderConfiguration))
{
return _providers[nameof(DownstreamRouteCreator)];
}

return _providers[nameof(DownstreamRouteProvider)];
}

private bool IsServiceDiscovery(ServiceProviderConfiguration config)
{
if(!string.IsNullOrEmpty(config?.Host) || config?.Port > 0)
{
return true;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
public class DownstreamRouteFinderMiddleware : OcelotMiddleware
{
private readonly OcelotRequestDelegate _next;
private readonly IDownstreamRouteFinderFactory _factory;
private readonly IDownstreamRouteProviderFactory _factory;
private readonly IInternalConfigurationRepository _repo;
private readonly IMultiplexer _multiplexer;

public DownstreamRouteFinderMiddleware(OcelotRequestDelegate next,
IOcelotLoggerFactory loggerFactory,
IDownstreamRouteFinderFactory downstreamRouteFinder,
IDownstreamRouteProviderFactory downstreamRouteFinder,
IInternalConfigurationRepository repo,
IMultiplexer multiplexer)
:base(loggerFactory.CreateLogger<DownstreamRouteFinderMiddleware>())
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task Invoke(DownstreamContext context)

var provider = _factory.Get(configuration.Data);

var downstreamRoute = provider.FindDownstreamRoute(upstreamUrlPath, context.HttpContext.Request.Method, configuration.Data, upstreamHost);
var downstreamRoute = provider.Get(upstreamUrlPath, context.HttpContext.Request.Method, configuration.Data, upstreamHost);

if (downstreamRoute.IsError)
{
Expand Down
2 changes: 1 addition & 1 deletion test/Ocelot.IntegrationTests/RaftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ bool CommandCalledOnAllStateMachines()
catch(Exception e)
{
//_output.WriteLine($"{e.Message}, {e.StackTrace}");
//Console.WriteLine(e);
Console.WriteLine(e);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

public class DownstreamRouteFinderMiddlewareTests
{
private readonly Mock<IDownstreamRouteFinder> _finder;
private readonly Mock<IDownstreamRouteFinderFactory> _factory;
private readonly Mock<IDownstreamRouteProvider> _finder;
private readonly Mock<IDownstreamRouteProviderFactory> _factory;
private readonly Mock<IInternalConfigurationRepository> _repo;
private Response<DownstreamRoute> _downstreamRoute;
private IInternalConfiguration _config;
Expand All @@ -36,8 +36,8 @@ public class DownstreamRouteFinderMiddlewareTests
public DownstreamRouteFinderMiddlewareTests()
{
_repo = new Mock<IInternalConfigurationRepository>();
_finder = new Mock<IDownstreamRouteFinder>();
_factory = new Mock<IDownstreamRouteFinderFactory>();
_finder = new Mock<IDownstreamRouteProvider>();
_factory = new Mock<IDownstreamRouteProviderFactory>();
_factory.Setup(x => x.Get(It.IsAny<IInternalConfiguration>())).Returns(_finder.Object);
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
_loggerFactory = new Mock<IOcelotLoggerFactory>();
Expand Down Expand Up @@ -88,7 +88,7 @@ private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRout
{
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
_finder
.Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IInternalConfiguration>(), It.IsAny<string>()))
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IInternalConfiguration>(), It.IsAny<string>()))
.Returns(_downstreamRoute);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

namespace Ocelot.UnitTests.DownstreamRouteFinder
{
public class DownstreamRouteFinderFactoryTests
public class DownstreamRouteProviderFactoryTests
{
private DownstreamRouteFinderFactory _factory;
private DownstreamRouteProviderFactory _factory;

public DownstreamRouteFinderFactoryTests()
public DownstreamRouteProviderFactoryTests()
{
var services = new ServiceCollection();
services.AddSingleton<IPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
services.AddSingleton<IDownstreamRouteFinder, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
services.AddSingleton<IDownstreamRouteFinder, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider>();
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
var provider = services.BuildServiceProvider();
_factory = new DownstreamRouteFinderFactory(provider);
_factory = new DownstreamRouteProviderFactory(provider);
}

//todo - bddfy
Expand All @@ -40,7 +40,7 @@ public void should_return_downstream_route_finder()
};
IInternalConfiguration config = new InternalConfiguration(reRoutes, "", null, "");
var result = _factory.Get(config);
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider>();
}

[Fact]
Expand All @@ -52,7 +52,7 @@ public void should_return_downstream_route_finder_as_no_service_discovery()
};
IInternalConfiguration config = new InternalConfiguration(reRoutes, "", spConfig, "");
var result = _factory.Get(config);
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider>();
}

[Fact]
Expand Down
Loading

0 comments on commit ed695e3

Please sign in to comment.