more simplification is that a word

This commit is contained in:
TomPallister
2016-09-07 20:40:56 +01:00
parent 15d7cafa1c
commit 71b7e7743e
18 changed files with 56 additions and 262 deletions

View File

@ -6,9 +6,9 @@ namespace Ocelot.Library.Infrastructure.Configuration
{
public Configuration()
{
Routes = new List<Route>();
ReRoutes = new List<ReRoute>();
}
public List<Route> Routes { get; set; }
public List<ReRoute> ReRoutes { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace Ocelot.Library.Infrastructure.Configuration
{
public class ReRoute
{
public string DownstreamTemplate { get; set; }
public string UpstreamTemplate { get; set; }
}
}

View File

@ -1,8 +0,0 @@
namespace Ocelot.Library.Infrastructure.Configuration
{
public class Route
{
public string Downstream { get; set; }
public string Upstream { get; set; }
}
}

View File

@ -2,6 +2,6 @@ namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public interface IUrlPathToUrlTemplateMatcher
{
UrlMatch Match(string downstreamUrlPath, string downstreamUrlTemplate);
UrlMatch Match(string upstreamUrlPath, string upstreamUrlTemplate);
}
}

View File

@ -5,31 +5,31 @@ namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public class UrlPathToUrlTemplateMatcher : IUrlPathToUrlTemplateMatcher
{
public UrlMatch Match(string downstreamUrlPath, string downstreamUrlTemplate)
public UrlMatch Match(string upstreamUrlPath, string upstreamUrlTemplate)
{
var urlPathTemplateCopy = downstreamUrlTemplate;
var urlPathTemplateCopy = upstreamUrlTemplate;
var templateKeysAndValues = new List<TemplateVariableNameAndValue>();
int counterForUrl = 0;
for (int counterForTemplate = 0; counterForTemplate < downstreamUrlTemplate.Length; counterForTemplate++)
for (int counterForTemplate = 0; counterForTemplate < upstreamUrlTemplate.Length; counterForTemplate++)
{
if (CharactersDontMatch(downstreamUrlTemplate[counterForTemplate], downstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,downstreamUrlPath.Length))
if (CharactersDontMatch(upstreamUrlTemplate[counterForTemplate], upstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,upstreamUrlPath.Length))
{
if (IsPlaceholder(downstreamUrlTemplate[counterForTemplate]))
if (IsPlaceholder(upstreamUrlTemplate[counterForTemplate]))
{
var variableName = GetPlaceholderVariableName(downstreamUrlTemplate, counterForTemplate);
var variableName = GetPlaceholderVariableName(upstreamUrlTemplate, counterForTemplate);
var variableValue = GetPlaceholderVariableValue(downstreamUrlPath, counterForUrl);
var variableValue = GetPlaceholderVariableValue(upstreamUrlPath, counterForUrl);
var templateVariableNameAndValue = new TemplateVariableNameAndValue(variableName, variableValue);
templateKeysAndValues.Add(templateVariableNameAndValue);
counterForTemplate = GetNextCounterPosition(downstreamUrlTemplate, counterForTemplate, '}');
counterForTemplate = GetNextCounterPosition(upstreamUrlTemplate, counterForTemplate, '}');
counterForUrl = GetNextCounterPosition(downstreamUrlPath, counterForUrl, '/');
counterForUrl = GetNextCounterPosition(upstreamUrlPath, counterForUrl, '/');
continue;
}

View File

@ -5,11 +5,11 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
{
public class DownstreamUrlTemplateVariableReplacer : IDownstreamUrlTemplateVariableReplacer
{
public string ReplaceTemplateVariable(string downstreamPathTemplate, UrlMatch urlMatch)
public string ReplaceTemplateVariable(UrlMatch urlMatch)
{
var upstreamUrl = new StringBuilder();
upstreamUrl.Append(downstreamPathTemplate);
upstreamUrl.Append(urlMatch.DownstreamUrlTemplate);
foreach (var templateVarAndValue in urlMatch.TemplateVariableNameAndValues)
{

View File

@ -4,6 +4,6 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
{
public interface IDownstreamUrlTemplateVariableReplacer
{
string ReplaceTemplateVariable(string downstreamPathTemplate, UrlMatch urlMatch);
string ReplaceTemplateVariable(UrlMatch urlMatch);
}
}

View File

@ -1,12 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlTemplateRepository
{
public class DownstreamUrlTemplateAlreadyExists : Error
{
public DownstreamUrlTemplateAlreadyExists()
: base("This key has already been used")
{
}
}
}

View File

@ -1,12 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlTemplateRepository
{
public class DownstreamUrlTemplateDoesNotExist : Error
{
public DownstreamUrlTemplateDoesNotExist()
: base("This key does not exist")
{
}
}
}

View File

@ -1,11 +0,0 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlTemplateRepository
{
public interface IUrlTemplateMapRepository
{
Response AddUrlTemplateMap(UrlTemplateMap urlPathMap);
Response<List<UrlTemplateMap>> All { get; }
}
}

View File

@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlTemplateRepository
{
public class InMemoryUrlTemplateMapRepository : IUrlTemplateMapRepository
{
private readonly Dictionary<string, string> _urlTemplates;
public InMemoryUrlTemplateMapRepository()
{
_urlTemplates = new Dictionary<string,string>();
}
public Response<List<UrlTemplateMap>> All
{
get
{
var routes = _urlTemplates
.Select(r => new UrlTemplateMap(r.Key, r.Value))
.ToList();
return new OkResponse<List<UrlTemplateMap>>(routes);
}
}
public Response AddUrlTemplateMap(UrlTemplateMap urlMap)
{
if(_urlTemplates.ContainsKey(urlMap.DownstreamUrlTemplate))
{
return new ErrorResponse(new List<Error>(){new DownstreamUrlTemplateAlreadyExists()});
}
_urlTemplates.Add(urlMap.DownstreamUrlTemplate, urlMap.UpstreamUrlPathTemplate);
return new OkResponse();
}
}
}

View File

@ -1,14 +0,0 @@
namespace Ocelot.Library.Infrastructure.UrlTemplateRepository
{
public class UrlTemplateMap
{
public UrlTemplateMap(string downstreamUrlTemplate, string upstreamUrlPathTemplate)
{
DownstreamUrlTemplate = downstreamUrlTemplate;
UpstreamUrlPathTemplate = upstreamUrlPathTemplate;
}
public string DownstreamUrlTemplate {get;private set;}
public string UpstreamUrlPathTemplate {get;private set;}
}
}

View File

@ -1,7 +1,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Ocelot.Library.Infrastructure.UrlTemplateRepository;
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
namespace Ocelot.Library.Middleware
@ -14,40 +13,31 @@ namespace Ocelot.Library.Middleware
{
private readonly RequestDelegate _next;
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
private readonly IUrlTemplateMapRepository _urlTemplateMapRepository;
private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
private readonly IOptions<Configuration> _optionsAccessor;
private readonly IOptions<Configuration> _configuration;
public ProxyMiddleware(RequestDelegate next,
IUrlPathToUrlTemplateMatcher urlMatcher,
IUrlTemplateMapRepository urlPathRepository,
IDownstreamUrlTemplateVariableReplacer urlReplacer, IOptions<Configuration> optionsAccessor)
IDownstreamUrlTemplateVariableReplacer urlReplacer, IOptions<Configuration> configuration)
{
_next = next;
_urlMatcher = urlMatcher;
_urlTemplateMapRepository = urlPathRepository;
_urlReplacer = urlReplacer;
_optionsAccessor = optionsAccessor;
_configuration = configuration;
}
public async Task Invoke(HttpContext context)
{
var downstreamUrlPath = context.Request.Path.ToString();
var upstreamUrlTemplates = _urlTemplateMapRepository.All;
{
var upstreamUrlPath = context.Request.Path.ToString();
UrlMatch urlMatch = null;
string downstreamUrlTemplate = string.Empty;
foreach (var template in upstreamUrlTemplates.Data)
foreach (var template in _configuration.Value.ReRoutes)
{
urlMatch = _urlMatcher.Match(downstreamUrlPath, template.DownstreamUrlTemplate);
urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplate);
if (urlMatch.Match)
{
downstreamUrlTemplate = template.DownstreamUrlTemplate;
break;
}
}
@ -58,7 +48,7 @@ namespace Ocelot.Library.Middleware
return;
}
var downstreamUrl = _urlReplacer.ReplaceTemplateVariable(downstreamUrlTemplate, urlMatch);
var downstreamUrl = _urlReplacer.ReplaceTemplateVariable(urlMatch);
//make a http request to this endpoint...maybe bring in a library

View File

@ -10,7 +10,6 @@ namespace Ocelot
using Library.Infrastructure.Configuration;
using Library.Infrastructure.UrlMatcher;
using Library.Infrastructure.UrlTemplateReplacer;
using Library.Infrastructure.UrlTemplateRepository;
public class Startup
{
@ -37,7 +36,6 @@ namespace Ocelot
// Add framework services.
services.AddSingleton<IUrlPathToUrlTemplateMatcher, UrlPathToUrlTemplateMatcher>();
services.AddSingleton<IDownstreamUrlTemplateVariableReplacer, DownstreamUrlTemplateVariableReplacer>();
services.AddSingleton<IUrlTemplateMapRepository, InMemoryUrlTemplateMapRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.