messing around with the proxy mdh the proxy middleware

This commit is contained in:
Tom Gardham-Pallister
2016-07-21 20:28:22 +01:00
parent 5e8719cde4
commit dbff2b9530
15 changed files with 213 additions and 35 deletions

View File

@ -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);
}
}