naming and plan

This commit is contained in:
Tom Gardham-Pallister
2016-07-10 17:38:54 +01:00
parent 711a3d6a91
commit cea6a557e9
22 changed files with 182 additions and 159 deletions

View File

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

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

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

View File

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

View File

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

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

View File

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

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

View File

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

View File

@ -0,0 +1,37 @@
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.DownstreamHostUrl))
{
return new ErrorResponse(new List<Error>(){new HostUrlMapKeyAlreadyExists()});
}
_routes.Add(baseUrlMap.DownstreamHostUrl, baseUrlMap.UpstreamHostUrl);
return new OkResponse();
}
public Response<HostUrlMap> GetBaseUrlMap(string downstreamUrl)
{
string upstreamUrl = null;
if(_routes.TryGetValue(downstreamUrl, out upstreamUrl))
{
return new OkResponse<HostUrlMap>(new HostUrlMap(downstreamUrl, upstreamUrl));
}
return new ErrorResponse<HostUrlMap>(new List<Error>(){new HostUrlMapKeyDoesNotExist()});
}
}
}

View File

@ -15,5 +15,13 @@ namespace Ocelot.Library.Infrastructure.Responses
}
public List<Error> Errors { get; private set; }
public bool IsError
{
get
{
return Errors.Count > 0;
}
}
}
}

View File

@ -14,5 +14,6 @@ namespace Ocelot.Library.Infrastructure.Responses
}
public T Data { get; private set; }
}
}

View File

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

View File

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

View File

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

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

@ -1,12 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlPathRepository
{
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.UrlPathRepository
{
public class DownstreamUrlPathTemplateDoesNotExist : Error
{
public DownstreamUrlPathTemplateDoesNotExist()
: base("This key does not exist")
{
}
}
}

View File

@ -14,6 +14,13 @@ namespace Ocelot.Library.Middleware
public async Task Invoke(HttpContext context)
{
//get the downstream host from the request context
//get the upstream host from the host repository
//if no upstream host fail this request
//get the downstream path from the request context
//get the downstream path template from the path template finder
//todo think about variables..
//add any query string..
await _next.Invoke(context);
}
}