mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 13:30:49 +08:00 
			
		
		
		
	messing around with the proxy mdh the proxy middleware
This commit is contained in:
		@@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user