first passing proxy acceptnace test yeyyy!

This commit is contained in:
TomPallister
2016-09-07 21:47:39 +01:00
parent 71b7e7743e
commit 03ef47038a
12 changed files with 264 additions and 43 deletions

View File

@ -0,0 +1,16 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.UrlMatcher;
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
{
public class DownstreamRoute
{
public DownstreamRoute(List<TemplateVariableNameAndValue> templateVariableNameAndValues, string downstreamUrlTemplate)
{
TemplateVariableNameAndValues = templateVariableNameAndValues;
DownstreamUrlTemplate = downstreamUrlTemplate;
}
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues { get; private set; }
public string DownstreamUrlTemplate { get; private set; }
}
}

View File

@ -0,0 +1,38 @@
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
{
public class DownstreamRouteFinder : IDownstreamRouteFinder
{
private readonly IOptions<Configuration.Configuration> _configuration;
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
public DownstreamRouteFinder(IOptions<Configuration.Configuration> configuration, IUrlPathToUrlTemplateMatcher urlMatcher)
{
_configuration = configuration;
_urlMatcher = urlMatcher;
}
public Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath)
{
foreach (var template in _configuration.Value.ReRoutes)
{
var urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplate);
if (urlMatch.Match)
{
return new OkResponse<DownstreamRoute>(new DownstreamRoute(urlMatch.TemplateVariableNameAndValues, template.DownstreamTemplate));
}
}
return new ErrorResponse<DownstreamRoute>(new List<Error>
{
new UnableToFindDownstreamRouteError()
});
}
}
}

View File

@ -0,0 +1,9 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
{
public interface IDownstreamRouteFinder
{
Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath);
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
{
public class UnableToFindDownstreamRouteError : Error
{
public UnableToFindDownstreamRouteError() : base("UnableToFindDownstreamRouteError")
{
}
}
}

View File

@ -1,17 +1,17 @@
using System.Text;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
{
public class DownstreamUrlTemplateVariableReplacer : IDownstreamUrlTemplateVariableReplacer
{
public string ReplaceTemplateVariable(UrlMatch urlMatch)
public string ReplaceTemplateVariable(DownstreamRoute downstreamRoute)
{
var upstreamUrl = new StringBuilder();
upstreamUrl.Append(urlMatch.DownstreamUrlTemplate);
upstreamUrl.Append(downstreamRoute.DownstreamUrlTemplate);
foreach (var templateVarAndValue in urlMatch.TemplateVariableNameAndValues)
foreach (var templateVarAndValue in downstreamRoute.TemplateVariableNameAndValues)
{
upstreamUrl.Replace(templateVarAndValue.TemplateVariableName, templateVarAndValue.TemplateVariableValue);
}

View File

@ -1,9 +1,10 @@
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.UrlMatcher;
namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
{
public interface IDownstreamUrlTemplateVariableReplacer
{
string ReplaceTemplateVariable(UrlMatch urlMatch);
string ReplaceTemplateVariable(DownstreamRoute downstreamRoute);
}
}