added a base url finder

This commit is contained in:
Tom Gardham-Pallister
2016-07-10 17:11:12 +01:00
parent 5b417ad466
commit 711a3d6a91
29 changed files with 356 additions and 186 deletions

View File

@ -0,0 +1,14 @@
namespace Ocelot.Library.Infrastructure.BaseUrlRepository
{
public class BaseUrlMap
{
public BaseUrlMap(string downstreamBaseUrl, string upstreamBaseUrl)
{
DownstreamBaseUrl = downstreamBaseUrl;
UpstreamBaseUrl = upstreamBaseUrl;
}
public string DownstreamBaseUrl {get;private set;}
public string UpstreamBaseUrl {get;private set;}
}
}

View File

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

View File

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

View File

@ -0,0 +1,10 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.BaseUrlRepository
{
public interface IBaseUrlMapRepository
{
Response AddBaseUrlMap(BaseUrlMap baseUrlMap);
Response<BaseUrlMap> GetBaseUrlMap(string downstreamUrl);
}
}

View File

@ -0,0 +1,37 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.BaseUrlRepository
{
public class InMemoryBaseUrlMapRepository : IBaseUrlMapRepository
{
private readonly Dictionary<string, string> _routes;
public InMemoryBaseUrlMapRepository()
{
_routes = new Dictionary<string,string>();
}
public Response AddBaseUrlMap(BaseUrlMap baseUrlMap)
{
if(_routes.ContainsKey(baseUrlMap.DownstreamBaseUrl))
{
return new ErrorResponse(new List<Error>(){new BaseUrlMapKeyAlreadyExists()});
}
_routes.Add(baseUrlMap.DownstreamBaseUrl, baseUrlMap.UpstreamBaseUrl);
return new OkResponse();
}
public Response<BaseUrlMap> GetBaseUrlMap(string downstreamUrl)
{
string upstreamUrl = null;
if(_routes.TryGetValue(downstreamUrl, out upstreamUrl))
{
return new OkResponse<BaseUrlMap>(new BaseUrlMap(downstreamUrl, upstreamUrl));
}
return new ErrorResponse<BaseUrlMap>(new List<Error>(){new BaseUrlMapKeyDoesNotExist()});
}
}
}

View File

@ -1,10 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public interface IUpstreamRouter
{
Response AddRoute(string downstreamUrl, string upstreamUrl);
Response<Route> GetRoute(string downstreamUrl);
}
}

View File

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public class InMemoryUpstreamRouter : IUpstreamRouter
{
private readonly Dictionary<string, string> _routes;
public InMemoryUpstreamRouter()
{
_routes = new Dictionary<string,string>();
}
public Response AddRoute(string downstreamUrl, string upstreamUrl)
{
if(_routes.ContainsKey(downstreamUrl))
{
return new ErrorResponse(new List<Error>(){new RouteKeyAlreadyExists()});
}
_routes.Add(downstreamUrl, upstreamUrl);
return new OkResponse();
}
public Response<Route> GetRoute(string downstreamUrl)
{
string upstreamUrl = null;
if(_routes.TryGetValue(downstreamUrl, out upstreamUrl))
{
return new OkResponse<Route>(new Route(downstreamUrl, upstreamUrl));
}
return new ErrorResponse<Route>(new List<Error>(){new RouteKeyDoesNotExist()});
}
}
}

View File

@ -1,14 +0,0 @@
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public class Route
{
public Route(string downstreamUrl, string upstreamUrl)
{
DownstreamUrl = downstreamUrl;
UpstreamUrl = upstreamUrl;
}
public string DownstreamUrl {get;private set;}
public string UpstreamUrl {get;private set;}
}
}

View File

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

View File

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

View File

@ -1,10 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
{
public interface IUrlPathRouter
{
Response AddRoute(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate);
Response<UrlPath> GetRoute(string downstreamUrlPathTemplate);
}
}

View File

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
{
public class InMemoryUrlPathRouter : IUrlPathRouter
{
private readonly Dictionary<string, string> _routes;
public InMemoryUrlPathRouter()
{
_routes = new Dictionary<string,string>();
}
public Response AddRoute(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate)
{
if(_routes.ContainsKey(downstreamUrlPathTemplate))
{
return new ErrorResponse(new List<Error>(){new DownstreamUrlPathTemplateAlreadyExists()});
}
_routes.Add(downstreamUrlPathTemplate, upstreamUrlPathTemplate);
return new OkResponse();
}
public Response<UrlPath> GetRoute(string downstreamUrlPathTemplate)
{
string upstreamUrlPathTemplate = null;
if(_routes.TryGetValue(downstreamUrlPathTemplate, out upstreamUrlPathTemplate))
{
return new OkResponse<UrlPath>(new UrlPath(downstreamUrlPathTemplate, upstreamUrlPathTemplate));
}
return new ErrorResponse<UrlPath>(new List<Error>(){new DownstreamUrlPathTemplateDoesNotExist()});
}
}
}

View File

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

View File

@ -0,0 +1,22 @@
using System;
using Ocelot.Library.Infrastructure.BaseUrlRepository;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlFinder
{
public class UpstreamBaseUrlFinder : IUpstreamBaseUrlFinder
{
private readonly IBaseUrlMapRepository _baseUrlMapRepository;
public UpstreamBaseUrlFinder(IBaseUrlMapRepository baseUrlMapRepository)
{
_baseUrlMapRepository = baseUrlMapRepository;
}
public Response<string> FindUpstreamBaseUrl(string downstreamBaseUrl)
{
var baseUrl = _baseUrlMapRepository.GetBaseUrlMap(downstreamBaseUrl);
return new OkResponse<string>(baseUrl.Data.UpstreamBaseUrl);
}
}
}

View File

@ -1,4 +1,4 @@
namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public interface IUrlPathToUrlPathTemplateMatcher
{

View File

@ -1,4 +1,4 @@
namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher
{

View File

@ -1,6 +1,6 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
namespace Ocelot.Library.Infrastructure.UrlPathRepository
{
public class DownstreamUrlPathTemplateAlreadyExists : Error
{

View File

@ -1,6 +1,6 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
namespace Ocelot.Library.Infrastructure.UrlPathRepository
{
public class DownstreamUrlPathTemplateDoesNotExist : Error
{

View File

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

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

View File

@ -0,0 +1,10 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
{
public interface IUrlPathTemplateMapRepository
{
Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap);
Response<UrlPathTemplateMap> GetUrlPathTemplateMap(string downstreamUrlPathTemplate);
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
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 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,8 +1,8 @@
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
{
public class UrlPath
public class UrlPathTemplateMap
{
public UrlPath(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate)
public UrlPathTemplateMap(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate)
{
DownstreamUrlPathTemplate = downstreamUrlPathTemplate;
UpstreamUrlPathTemplate = upstreamUrlPathTemplate;