simplifying stuff after i realised i added loads of crap i dont need

This commit is contained in:
Tom Gardham-Pallister
2016-08-27 14:13:45 +01:00
parent 267ad5e98b
commit c4e0bae4ce
36 changed files with 302 additions and 668 deletions

View File

@ -1,14 +0,0 @@
namespace Ocelot.Library.Infrastructure.HostUrlRepository
{
public class HostUrlMap
{
public HostUrlMap(string urlPathTemplate, string upstreamHostUrl)
{
UrlPathTemplate = urlPathTemplate;
UpstreamHostUrl = upstreamHostUrl;
}
public string UrlPathTemplate {get;private set;}
public string UpstreamHostUrl {get;private set;}
}
}

View File

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

View File

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

View File

@ -1,10 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.HostUrlRepository
{
public interface IHostUrlMapRepository
{
Response AddBaseUrlMap(HostUrlMap baseUrlMap);
Response<HostUrlMap> GetBaseUrlMap(string urlPathTemplate);
}
}

View File

@ -1,37 +0,0 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.HostUrlRepository
{
public class InMemoryHostUrlMapRepository : IHostUrlMapRepository
{
private readonly Dictionary<string, string> _routes;
public InMemoryHostUrlMapRepository()
{
_routes = new Dictionary<string,string>();
}
public Response AddBaseUrlMap(HostUrlMap baseUrlMap)
{
if(_routes.ContainsKey(baseUrlMap.UrlPathTemplate))
{
return new ErrorResponse(new List<Error>(){new HostUrlMapKeyAlreadyExists()});
}
_routes.Add(baseUrlMap.UrlPathTemplate, baseUrlMap.UpstreamHostUrl);
return new OkResponse();
}
public Response<HostUrlMap> GetBaseUrlMap(string urlPathTemplate)
{
string upstreamUrl = null;
if(_routes.TryGetValue(urlPathTemplate, out upstreamUrl))
{
return new OkResponse<HostUrlMap>(new HostUrlMap(urlPathTemplate, upstreamUrl));
}
return new ErrorResponse<HostUrlMap>(new List<Error>(){new HostUrlMapKeyDoesNotExist()});
}
}
}

View File

@ -1,9 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlFinder
{
public interface IUpstreamHostUrlFinder
{
Response<string> FindUpstreamHostUrl(string downstreamHostUrl);
}
}

View File

@ -1,12 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlFinder
{
public class UnableToFindUpstreamHostUrl : Error
{
public UnableToFindUpstreamHostUrl()
: base("Unable to find upstream base url")
{
}
}
}

View File

@ -1,28 +0,0 @@
using System;
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.HostUrlRepository;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlFinder
{
public class UpstreamHostUrlFinder : IUpstreamHostUrlFinder
{
private readonly IHostUrlMapRepository _hostUrlMapRepository;
public UpstreamHostUrlFinder(IHostUrlMapRepository hostUrlMapRepository)
{
_hostUrlMapRepository = hostUrlMapRepository;
}
public Response<string> FindUpstreamHostUrl(string downstreamBaseUrl)
{
var baseUrl = _hostUrlMapRepository.GetBaseUrlMap(downstreamBaseUrl);
if(baseUrl.IsError)
{
return new ErrorResponse<string>(new List<Error> {new UnableToFindUpstreamHostUrl()});
}
return new OkResponse<string>(baseUrl.Data.UpstreamHostUrl);
}
}
}

View File

@ -0,0 +1,7 @@
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public interface IUrlPathToUrlTemplateMatcher
{
UrlMatch Match(string downstreamUrlPath, string downstreamUrlTemplate);
}
}

View File

@ -1,4 +1,4 @@
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public class TemplateVariableNameAndValue
{

View File

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

View File

@ -1,25 +1,25 @@
using System;
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher
public class UrlPathToUrlTemplateMatcher : IUrlPathToUrlTemplateMatcher
{
public UrlPathMatch Match(string downstreamUrlPath, string downstreamUrlPathTemplate)
public UrlMatch Match(string downstreamUrlPath, string downstreamUrlTemplate)
{
var urlPathTemplateCopy = downstreamUrlPathTemplate;
var urlPathTemplateCopy = downstreamUrlTemplate;
var templateKeysAndValues = new List<TemplateVariableNameAndValue>();
int counterForUrl = 0;
for (int counterForTemplate = 0; counterForTemplate < downstreamUrlPathTemplate.Length; counterForTemplate++)
for (int counterForTemplate = 0; counterForTemplate < downstreamUrlTemplate.Length; counterForTemplate++)
{
if (CharactersDontMatch(downstreamUrlPathTemplate[counterForTemplate], downstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,downstreamUrlPath.Length))
if (CharactersDontMatch(downstreamUrlTemplate[counterForTemplate], downstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,downstreamUrlPath.Length))
{
if (IsPlaceholder(downstreamUrlPathTemplate[counterForTemplate]))
if (IsPlaceholder(downstreamUrlTemplate[counterForTemplate]))
{
var variableName = GetPlaceholderVariableName(downstreamUrlPathTemplate, counterForTemplate);
var variableName = GetPlaceholderVariableName(downstreamUrlTemplate, counterForTemplate);
var variableValue = GetPlaceholderVariableValue(downstreamUrlPath, counterForUrl);
@ -27,7 +27,7 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
templateKeysAndValues.Add(templateVariableNameAndValue);
counterForTemplate = GetNextCounterPosition(downstreamUrlPathTemplate, counterForTemplate, '}');
counterForTemplate = GetNextCounterPosition(downstreamUrlTemplate, counterForTemplate, '}');
counterForUrl = GetNextCounterPosition(downstreamUrlPath, counterForUrl, '/');
@ -35,12 +35,12 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
}
else
{
return new UrlPathMatch(false, templateKeysAndValues, string.Empty);
return new UrlMatch(false, templateKeysAndValues, string.Empty);
}
}
counterForUrl++;
}
return new UrlPathMatch(true, templateKeysAndValues, urlPathTemplateCopy);
return new UrlMatch(true, templateKeysAndValues, urlPathTemplateCopy);
}
private string GetPlaceholderVariableValue(string urlPath, int counterForUrl)

View File

@ -1,7 +0,0 @@
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public interface IUrlPathToUrlPathTemplateMatcher
{
UrlPathMatch Match(string downstreamUrlPath, string downStreamUrlPathTemplate);
}
}

View File

@ -1,17 +0,0 @@
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public class UrlPathMatch
{
public UrlPathMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues, string downstreamUrlPathTemplate)
{
Match = match;
TemplateVariableNameAndValues = templateVariableNameAndValues;
DownstreamUrlPathTemplate = downstreamUrlPathTemplate;
}
public bool Match {get;private set;}
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues {get;private set;}
public string DownstreamUrlPathTemplate {get;private set;}
}
}

View File

@ -1,10 +0,0 @@
using Ocelot.Library.Infrastructure.UrlPathMatcher;
namespace Ocelot.Library.Infrastructure.UrlPathReplacer
{
public interface IUpstreamUrlPathTemplateVariableReplacer
{
string ReplaceTemplateVariable(string upstreamPathTemplate, UrlPathMatch urlPathMatch);
}
}

View File

@ -1,22 +0,0 @@
using System;
using System.Text;
using Ocelot.Library.Infrastructure.UrlPathMatcher;
namespace Ocelot.Library.Infrastructure.UrlPathReplacer
{
public class UpstreamUrlPathTemplateVariableReplacer : IUpstreamUrlPathTemplateVariableReplacer
{
public string ReplaceTemplateVariable(string upstreamPathTemplate, UrlPathMatch urlPathMatch)
{
var upstreamUrl = new StringBuilder();
upstreamUrl.Append(upstreamPathTemplate);
foreach (var templateVarAndValue in urlPathMatch.TemplateVariableNameAndValues)
{
upstreamUrl.Replace(templateVarAndValue.TemplateVariableName, templateVarAndValue.TemplateVariableValue);
}
return upstreamUrl.ToString();
}
}
}

View File

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

View File

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

View File

@ -1,12 +0,0 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
{
public interface IUrlPathTemplateMapRepository
{
Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap);
Response<UrlPathTemplateMap> GetUrlPathTemplateMap(string downstreamUrlPathTemplate);
Response<List<UrlPathTemplateMap>> All { get; }
}
}

View File

@ -1,51 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
{
public class InMemoryUrlPathTemplateMapRepository : IUrlPathTemplateMapRepository
{
private readonly Dictionary<string, string> _routes;
public InMemoryUrlPathTemplateMapRepository()
{
_routes = new Dictionary<string,string>();
}
public Response<List<UrlPathTemplateMap>> All
{
get
{
var routes = _routes
.Select(r => new UrlPathTemplateMap(r.Key, r.Value))
.ToList();
return new OkResponse<List<UrlPathTemplateMap>>(routes);
}
}
public Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap)
{
if(_routes.ContainsKey(urlPathMap.DownstreamUrlPathTemplate))
{
return new ErrorResponse(new List<Error>(){new DownstreamUrlPathTemplateAlreadyExists()});
}
_routes.Add(urlPathMap.DownstreamUrlPathTemplate, urlPathMap.UpstreamUrlPathTemplate);
return new OkResponse();
}
public Response<UrlPathTemplateMap> GetUrlPathTemplateMap(string downstreamUrlPathTemplate)
{
string upstreamUrlPathTemplate = null;
if(_routes.TryGetValue(downstreamUrlPathTemplate, out upstreamUrlPathTemplate))
{
return new OkResponse<UrlPathTemplateMap>(new UrlPathTemplateMap(downstreamUrlPathTemplate, upstreamUrlPathTemplate));
}
return new ErrorResponse<UrlPathTemplateMap>(new List<Error>(){new DownstreamUrlPathTemplateDoesNotExist()});
}
}
}

View File

@ -1,14 +0,0 @@
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
{
public class UrlPathTemplateMap
{
public UrlPathTemplateMap(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate)
{
DownstreamUrlPathTemplate = downstreamUrlPathTemplate;
UpstreamUrlPathTemplate = upstreamUrlPathTemplate;
}
public string DownstreamUrlPathTemplate {get;private set;}
public string UpstreamUrlPathTemplate {get;private set;}
}
}

View File

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

View File

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

View File

@ -0,0 +1,12 @@
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

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

View File

@ -0,0 +1,11 @@
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

@ -0,0 +1,39 @@
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

@ -0,0 +1,14 @@
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,10 +1,8 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.HostUrlRepository;
using Ocelot.Library.Infrastructure.UrlPathMatcher;
using Ocelot.Library.Infrastructure.UrlPathReplacer;
using Ocelot.Library.Infrastructure.UrlPathTemplateRepository;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Ocelot.Library.Infrastructure.UrlTemplateRepository;
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
namespace Ocelot.Library.Middleware
{
@ -13,53 +11,48 @@ namespace Ocelot.Library.Middleware
public class ProxyMiddleware
{
private readonly RequestDelegate _next;
private readonly IUrlPathToUrlPathTemplateMatcher _urlMatcher;
private readonly IUrlPathTemplateMapRepository _urlPathRepository;
private readonly IHostUrlMapRepository _hostUrlRepository;
private readonly IUpstreamUrlPathTemplateVariableReplacer _urlReplacer;
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
private readonly IUrlTemplateMapRepository _urlTemplateMapRepository;
private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
public ProxyMiddleware(RequestDelegate next,
IUrlPathToUrlPathTemplateMatcher urlMatcher,
IUrlPathTemplateMapRepository urlPathRepository,
IHostUrlMapRepository hostUrlRepository,
IUpstreamUrlPathTemplateVariableReplacer urlReplacer)
IUrlPathToUrlTemplateMatcher urlMatcher,
IUrlTemplateMapRepository urlPathRepository,
IDownstreamUrlTemplateVariableReplacer urlReplacer)
{
_next = next;
_urlMatcher = urlMatcher;
_urlPathRepository = urlPathRepository;
_hostUrlRepository = hostUrlRepository;
_urlTemplateMapRepository = urlPathRepository;
_urlReplacer = urlReplacer;
}
public async Task Invoke(HttpContext context)
{
var path = context.Request.Path.ToString();
{
var downstreamUrlPath = context.Request.Path.ToString();
var urlPathTemplateMaps = _urlPathRepository.All;
var upstreamUrlTemplates = _urlTemplateMapRepository.All;
UrlPathMatch urlPathMatch = null;
string upstreamPathUrlTemplate = string.Empty;
UrlMatch urlMatch = null;
foreach (var template in urlPathTemplateMaps.Data)
string downstreamUrlTemplate = string.Empty;
foreach (var template in upstreamUrlTemplates.Data)
{
urlPathMatch = _urlMatcher.Match(path, template.DownstreamUrlPathTemplate);
urlMatch = _urlMatcher.Match(downstreamUrlPath, template.DownstreamUrlTemplate);
if (urlPathMatch.Match)
if (urlMatch.Match)
{
upstreamPathUrlTemplate = template.UpstreamUrlPathTemplate;
downstreamUrlTemplate = template.DownstreamUrlTemplate;
break;
}
}
if (urlPathMatch == null || !urlPathMatch.Match)
if (urlMatch == null || !urlMatch.Match)
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
return;
}
var upstreamHostUrl = _hostUrlRepository.GetBaseUrlMap(urlPathMatch.DownstreamUrlPathTemplate);
var pathUrl = _urlReplacer.ReplaceTemplateVariable(upstreamPathUrlTemplate, urlPathMatch);
var downstreamUrl = _urlReplacer.ReplaceTemplateVariable(downstreamUrlTemplate, urlMatch);
//make a http request to this endpoint...maybe bring in a library

View File

@ -8,10 +8,9 @@ using Ocelot.Library.Middleware;
namespace Ocelot
{
using Library.Infrastructure.HostUrlRepository;
using Library.Infrastructure.UrlPathMatcher;
using Library.Infrastructure.UrlPathReplacer;
using Library.Infrastructure.UrlPathTemplateRepository;
using Library.Infrastructure.UrlMatcher;
using Library.Infrastructure.UrlTemplateReplacer;
using Library.Infrastructure.UrlTemplateRepository;
public class Startup
{
@ -31,11 +30,9 @@ namespace Ocelot
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddSingleton<IHostUrlMapRepository, InMemoryHostUrlMapRepository>();
services.AddSingleton<IUrlPathToUrlPathTemplateMatcher, UrlPathToUrlPathTemplateMatcher>();
services.AddSingleton<IHostUrlMapRepository, InMemoryHostUrlMapRepository>();
services.AddSingleton<IUpstreamUrlPathTemplateVariableReplacer, UpstreamUrlPathTemplateVariableReplacer>();
services.AddSingleton<IUrlPathTemplateMapRepository, InMemoryUrlPathTemplateMapRepository>();
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.