mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-04 10:32:49 +08:00
47 lines
2.0 KiB
C#
47 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Ocelot.Library.Configuration.Provider;
|
|
using Ocelot.Library.DownstreamRouteFinder.UrlMatcher;
|
|
using Ocelot.Library.Errors;
|
|
using Ocelot.Library.Responses;
|
|
|
|
namespace Ocelot.Library.DownstreamRouteFinder.Finder
|
|
{
|
|
public class DownstreamRouteFinder : IDownstreamRouteFinder
|
|
{
|
|
private readonly IOcelotConfigurationProvider _configProvider;
|
|
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
|
private readonly ITemplateVariableNameAndValueFinder _templateVariableNameAndValueFinder;
|
|
|
|
public DownstreamRouteFinder(IOcelotConfigurationProvider configProvider, IUrlPathToUrlTemplateMatcher urlMatcher, ITemplateVariableNameAndValueFinder templateVariableNameAndValueFinder)
|
|
{
|
|
_configProvider = configProvider;
|
|
_urlMatcher = urlMatcher;
|
|
_templateVariableNameAndValueFinder = templateVariableNameAndValueFinder;
|
|
}
|
|
|
|
public Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod)
|
|
{
|
|
var configuration = _configProvider.Get();
|
|
|
|
foreach (var template in configuration.Data.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase)))
|
|
{
|
|
var urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplatePattern);
|
|
|
|
if (urlMatch.Data.Match)
|
|
{
|
|
var templateVariableNameAndValues = _templateVariableNameAndValueFinder.Find(upstreamUrlPath,
|
|
template.UpstreamTemplate);
|
|
|
|
return new OkResponse<DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, template));
|
|
}
|
|
}
|
|
|
|
return new ErrorResponse<DownstreamRoute>(new List<Error>
|
|
{
|
|
new UnableToFindDownstreamRouteError()
|
|
});
|
|
}
|
|
}
|
|
} |