-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#340 renamed stuff to provider rather than finder, as its not longer …
…finding anything
- Loading branch information
Tom Gardham-Pallister
committed
May 10, 2018
1 parent
81f920f
commit ed695e3
Showing
9 changed files
with
879 additions
and
882 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 62 additions & 62 deletions
124
...uteFinder/Finder/DownstreamRouteFinder.cs → ...eFinder/Finder/DownstreamRouteProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteFinder.cs
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.