mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 17:08:15 +08:00
naming and plan
This commit is contained in:
@ -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;}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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()});
|
||||
}
|
||||
}
|
||||
}
|
@ -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;}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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()});
|
||||
}
|
||||
}
|
||||
}
|
@ -15,5 +15,13 @@ namespace Ocelot.Library.Infrastructure.Responses
|
||||
}
|
||||
|
||||
public List<Error> Errors { get; private set; }
|
||||
|
||||
public bool IsError
|
||||
{
|
||||
get
|
||||
{
|
||||
return Errors.Count > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -14,5 +14,6 @@ namespace Ocelot.Library.Infrastructure.Responses
|
||||
}
|
||||
|
||||
public T Data { get; private set; }
|
||||
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlFinder
|
||||
{
|
||||
public interface IUpstreamBaseUrlFinder
|
||||
{
|
||||
Response<string> FindUpstreamBaseUrl(string downstreamBaseUrl);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlFinder
|
||||
{
|
||||
public interface IUpstreamHostUrlFinder
|
||||
{
|
||||
Response<string> FindUpstreamHostUrl(string downstreamHostUrl);
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user