mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-18 21:08:16 +08:00
messing around with the proxy mdh the proxy middleware
This commit is contained in:
@ -2,13 +2,13 @@ namespace Ocelot.Library.Infrastructure.HostUrlRepository
|
||||
{
|
||||
public class HostUrlMap
|
||||
{
|
||||
public HostUrlMap(string downstreamHostUrl, string upstreamHostUrl)
|
||||
public HostUrlMap(string urlPathTemplate, string upstreamHostUrl)
|
||||
{
|
||||
DownstreamHostUrl = downstreamHostUrl;
|
||||
UrlPathTemplate = urlPathTemplate;
|
||||
UpstreamHostUrl = upstreamHostUrl;
|
||||
}
|
||||
|
||||
public string DownstreamHostUrl {get;private set;}
|
||||
public string UrlPathTemplate {get;private set;}
|
||||
public string UpstreamHostUrl {get;private set;}
|
||||
}
|
||||
}
|
@ -5,6 +5,6 @@ namespace Ocelot.Library.Infrastructure.HostUrlRepository
|
||||
public interface IHostUrlMapRepository
|
||||
{
|
||||
Response AddBaseUrlMap(HostUrlMap baseUrlMap);
|
||||
Response<HostUrlMap> GetBaseUrlMap(string downstreamUrl);
|
||||
Response<HostUrlMap> GetBaseUrlMap(string urlPathTemplate);
|
||||
}
|
||||
}
|
@ -12,23 +12,23 @@ namespace Ocelot.Library.Infrastructure.HostUrlRepository
|
||||
}
|
||||
public Response AddBaseUrlMap(HostUrlMap baseUrlMap)
|
||||
{
|
||||
if(_routes.ContainsKey(baseUrlMap.DownstreamHostUrl))
|
||||
if(_routes.ContainsKey(baseUrlMap.UrlPathTemplate))
|
||||
{
|
||||
return new ErrorResponse(new List<Error>(){new HostUrlMapKeyAlreadyExists()});
|
||||
}
|
||||
|
||||
_routes.Add(baseUrlMap.DownstreamHostUrl, baseUrlMap.UpstreamHostUrl);
|
||||
_routes.Add(baseUrlMap.UrlPathTemplate, baseUrlMap.UpstreamHostUrl);
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
|
||||
public Response<HostUrlMap> GetBaseUrlMap(string downstreamUrl)
|
||||
public Response<HostUrlMap> GetBaseUrlMap(string urlPathTemplate)
|
||||
{
|
||||
string upstreamUrl = null;
|
||||
|
||||
if(_routes.TryGetValue(downstreamUrl, out upstreamUrl))
|
||||
if(_routes.TryGetValue(urlPathTemplate, out upstreamUrl))
|
||||
{
|
||||
return new OkResponse<HostUrlMap>(new HostUrlMap(downstreamUrl, upstreamUrl));
|
||||
return new OkResponse<HostUrlMap>(new HostUrlMap(urlPathTemplate, upstreamUrl));
|
||||
}
|
||||
|
||||
return new ErrorResponse<HostUrlMap>(new List<Error>(){new HostUrlMapKeyDoesNotExist()});
|
@ -4,12 +4,15 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
|
||||
{
|
||||
public class UrlPathMatch
|
||||
{
|
||||
public UrlPathMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues)
|
||||
public UrlPathMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues, string urlPathTemplate)
|
||||
{
|
||||
Match = match;
|
||||
TemplateVariableNameAndValues = templateVariableNameAndValues;
|
||||
UrlPathTemplate = urlPathTemplate;
|
||||
}
|
||||
public bool Match {get;private set;}
|
||||
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues {get;private set;}
|
||||
|
||||
public string UrlPathTemplate {get;private set;}
|
||||
}
|
||||
}
|
@ -7,6 +7,8 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
|
||||
{
|
||||
public UrlPathMatch Match(string urlPath, string urlPathTemplate)
|
||||
{
|
||||
var urlPathTemplateCopy = urlPathTemplate;
|
||||
|
||||
var templateKeysAndValues = new List<TemplateVariableNameAndValue>();
|
||||
|
||||
urlPath = urlPath.ToLower();
|
||||
@ -37,12 +39,12 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
|
||||
}
|
||||
else
|
||||
{
|
||||
return new UrlPathMatch(false, templateKeysAndValues);
|
||||
return new UrlPathMatch(false, templateKeysAndValues, string.Empty);
|
||||
}
|
||||
}
|
||||
counterForUrl++;
|
||||
}
|
||||
return new UrlPathMatch(true, templateKeysAndValues);
|
||||
return new UrlPathMatch(true, templateKeysAndValues, urlPathTemplateCopy);
|
||||
}
|
||||
|
||||
private string GetPlaceholderVariableValue(string urlPath, int counterForUrl)
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
|
||||
@ -6,5 +7,6 @@ namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
|
||||
{
|
||||
Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap);
|
||||
Response<UrlPathTemplateMap> GetUrlPathTemplateMap(string downstreamUrlPathTemplate);
|
||||
Response<List<UrlPathTemplateMap>> All { get; }
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
|
||||
@ -11,6 +12,18 @@ namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
|
||||
{
|
||||
_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))
|
||||
|
@ -1,26 +1,61 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.HostUrlRepository;
|
||||
using Ocelot.Library.Infrastructure.UrlPathMatcher;
|
||||
using Ocelot.Library.Infrastructure.UrlPathTemplateRepository;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
{
|
||||
public class ProxyMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public ProxyMiddleware(RequestDelegate next)
|
||||
private readonly IUrlPathToUrlPathTemplateMatcher _urlMatcher;
|
||||
private readonly IUrlPathTemplateMapRepository _urlPathRepository;
|
||||
private readonly IHostUrlMapRepository _hostUrlRepository;
|
||||
public ProxyMiddleware(RequestDelegate next,
|
||||
IUrlPathToUrlPathTemplateMatcher urlMatcher,
|
||||
IUrlPathTemplateMapRepository urlPathRepository,
|
||||
IHostUrlMapRepository hostUrlRepository)
|
||||
{
|
||||
_next = next;
|
||||
_urlMatcher = urlMatcher;
|
||||
_urlPathRepository = urlPathRepository;
|
||||
_hostUrlRepository = hostUrlRepository;
|
||||
}
|
||||
|
||||
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..
|
||||
|
||||
var path = context.Request.Path.ToString();
|
||||
|
||||
var templates = _urlPathRepository.All;
|
||||
|
||||
UrlPathMatch urlPathMatch = null;
|
||||
string upstreamPathUrl = string.Empty;
|
||||
|
||||
foreach (var template in templates.Data)
|
||||
{
|
||||
urlPathMatch = _urlMatcher.Match(path, template.DownstreamUrlPathTemplate);
|
||||
|
||||
if (urlPathMatch.Match)
|
||||
{
|
||||
upstreamPathUrl = template.UpstreamUrlPathTemplate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!urlPathMatch.Match)
|
||||
{
|
||||
throw new Exception("BOOOM TING! no match");
|
||||
}
|
||||
|
||||
var upstreamHostUrl = _hostUrlRepository.GetBaseUrlMap(urlPathMatch.UrlPathTemplate);
|
||||
|
||||
//now map the variables from the url path to the upstream url path
|
||||
|
||||
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
|
@ -37,11 +37,11 @@ namespace Ocelot
|
||||
loggerFactory.AddDebug();
|
||||
|
||||
app.UseProxy();
|
||||
|
||||
app.Run(async context =>
|
||||
{
|
||||
await context.Response.WriteAsync("Hello from Tom");
|
||||
});
|
||||
//app.Run()
|
||||
// app.Run(async context =>
|
||||
// {
|
||||
// await context.Response.WriteAsync("Hello from Tom");
|
||||
// });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user