mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 14:10:50 +08:00 
			
		
		
		
	Add ConfigAwarePlaceholders class (#997)
* Add ConfigAwarePlaceholders class This allows placeholder values to be sourced from IConfiguration in addition to set values. * Rework how IPlaceholders is decorated in container
This commit is contained in:
		
				
					committed by
					
						
						Thiago Loureiro
					
				
			
			
				
	
			
			
			
						parent
						
							b6f3f0f28a
						
					
				
				
					commit
					bef40041ba
				
			@@ -24,5 +24,7 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
 | 
			
		||||
        IOcelotBuilder AddTransientDefinedAggregator<T>()
 | 
			
		||||
            where T : class, IDefinedAggregator;
 | 
			
		||||
 | 
			
		||||
        IOcelotBuilder AddConfigPlaceholders();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -37,6 +37,7 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
    using Ocelot.Security.IPSecurity;
 | 
			
		||||
    using Ocelot.ServiceDiscovery;
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using System.Reflection;
 | 
			
		||||
 | 
			
		||||
@@ -209,5 +210,39 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public IOcelotBuilder AddConfigPlaceholders()
 | 
			
		||||
        {
 | 
			
		||||
            // see: https://greatrexpectations.com/2018/10/25/decorators-in-net-core-with-dependency-injection
 | 
			
		||||
            var wrappedDescriptor = Services.First(x => x.ServiceType == typeof(IPlaceholders));
 | 
			
		||||
            
 | 
			
		||||
            var objectFactory = ActivatorUtilities.CreateFactory(
 | 
			
		||||
                typeof(ConfigAwarePlaceholders),
 | 
			
		||||
                new[] { typeof(IPlaceholders) });
 | 
			
		||||
 | 
			
		||||
            Services.Replace(ServiceDescriptor.Describe(
 | 
			
		||||
                typeof(IPlaceholders),
 | 
			
		||||
                s => (IPlaceholders) objectFactory(s,
 | 
			
		||||
                    new[] {CreateInstance(s, wrappedDescriptor)}),
 | 
			
		||||
                wrappedDescriptor.Lifetime
 | 
			
		||||
            ));
 | 
			
		||||
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        private static object CreateInstance(IServiceProvider services, ServiceDescriptor descriptor)
 | 
			
		||||
        {
 | 
			
		||||
            if (descriptor.ImplementationInstance != null)
 | 
			
		||||
            {
 | 
			
		||||
                return descriptor.ImplementationInstance;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (descriptor.ImplementationFactory != null)
 | 
			
		||||
            {
 | 
			
		||||
                return descriptor.ImplementationFactory(services);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return ActivatorUtilities.GetServiceOrCreateInstance(services, descriptor.ImplementationType);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										61
									
								
								src/Ocelot/Infrastructure/ConfigAwarePlaceholders.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								src/Ocelot/Infrastructure/ConfigAwarePlaceholders.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
namespace Ocelot.Infrastructure
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Text.RegularExpressions;
 | 
			
		||||
    using Microsoft.Extensions.Configuration;
 | 
			
		||||
    using Request.Middleware;
 | 
			
		||||
    using Responses;
 | 
			
		||||
 | 
			
		||||
    public class ConfigAwarePlaceholders : IPlaceholders
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IConfiguration _configuration;
 | 
			
		||||
        private readonly IPlaceholders _placeholders;
 | 
			
		||||
 | 
			
		||||
        public ConfigAwarePlaceholders(IConfiguration configuration, IPlaceholders placeholders)
 | 
			
		||||
        {
 | 
			
		||||
            _configuration = configuration;
 | 
			
		||||
            _placeholders = placeholders;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public Response<string> Get(string key)
 | 
			
		||||
        {
 | 
			
		||||
            var placeholderResponse = _placeholders.Get(key);
 | 
			
		||||
 | 
			
		||||
            if (!placeholderResponse.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                return placeholderResponse;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return GetFromConfig(CleanKey(key));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Response<string> Get(string key, DownstreamRequest request)
 | 
			
		||||
        {
 | 
			
		||||
            var placeholderResponse = _placeholders.Get(key, request);
 | 
			
		||||
            
 | 
			
		||||
            if (!placeholderResponse.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                return placeholderResponse;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return GetFromConfig(CleanKey(key));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Response Add(string key, Func<Response<string>> func)
 | 
			
		||||
            => _placeholders.Add(key, func);
 | 
			
		||||
 | 
			
		||||
        public Response Remove(string key)
 | 
			
		||||
            => _placeholders.Remove(key);
 | 
			
		||||
 | 
			
		||||
        private string CleanKey(string key) 
 | 
			
		||||
            => Regex.Replace(key, @"[{}]", string.Empty, RegexOptions.None);
 | 
			
		||||
 | 
			
		||||
        private Response<string> GetFromConfig(string key)
 | 
			
		||||
        {
 | 
			
		||||
            var valueFromConfig = _configuration[key];
 | 
			
		||||
            return valueFromConfig == null
 | 
			
		||||
                ? (Response<string>) new ErrorResponse<string>(new CouldNotFindPlaceholderError(key))
 | 
			
		||||
                : new OkResponse<string>(valueFromConfig);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user