mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 08:15:27 +08:00 
			
		
		
		
	regex for url match, means annoying constructor ocelot configuration object but cant work out a better way to do this at the moment
This commit is contained in:
		@@ -0,0 +1,24 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
 | 
			
		||||
{
 | 
			
		||||
    public class ConfigurationValidationResult
 | 
			
		||||
    {
 | 
			
		||||
        public ConfigurationValidationResult(bool isError)
 | 
			
		||||
        {
 | 
			
		||||
            IsError = isError;
 | 
			
		||||
            Errors = new List<Error>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public ConfigurationValidationResult(bool isError, List<Error> errors)
 | 
			
		||||
        {
 | 
			
		||||
            IsError = isError;
 | 
			
		||||
            Errors = errors;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public bool IsError { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public List<Error> Errors { get; private set; } 
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,35 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
 | 
			
		||||
{
 | 
			
		||||
    public class ConfigurationValidator : IConfigurationValidator
 | 
			
		||||
    {
 | 
			
		||||
        public Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration)
 | 
			
		||||
        {
 | 
			
		||||
            var duplicateUpstreamTemplates = configuration.ReRoutes
 | 
			
		||||
                .Select(r => r.DownstreamTemplate)
 | 
			
		||||
                .GroupBy(r => r)
 | 
			
		||||
                .Where(r => r.Count() > 1)
 | 
			
		||||
                .Select(r => r.Key)
 | 
			
		||||
                .ToList();
 | 
			
		||||
 | 
			
		||||
            if (duplicateUpstreamTemplates.Count <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<ConfigurationValidationResult>(new ConfigurationValidationResult(false));
 | 
			
		||||
            }
 | 
			
		||||
                
 | 
			
		||||
            var errors = new List<Error>();
 | 
			
		||||
 | 
			
		||||
            foreach (var duplicateUpstreamTemplate in duplicateUpstreamTemplates)
 | 
			
		||||
            {
 | 
			
		||||
                var error = new DownstreamTemplateAlreadyUsedError(string.Format("Duplicate DownstreamTemplate: {0}", 
 | 
			
		||||
                    duplicateUpstreamTemplate));
 | 
			
		||||
                errors.Add(error);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new OkResponse<ConfigurationValidationResult>(new ConfigurationValidationResult(true, errors));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,11 @@
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
 | 
			
		||||
{
 | 
			
		||||
    public class DownstreamTemplateAlreadyUsedError : Error
 | 
			
		||||
    {
 | 
			
		||||
        public DownstreamTemplateAlreadyUsedError(string message) : base(message)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,9 @@
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
 | 
			
		||||
{
 | 
			
		||||
    public interface IConfigurationValidator
 | 
			
		||||
    {
 | 
			
		||||
        Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,14 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
 | 
			
		||||
{
 | 
			
		||||
    public class YamlConfiguration
 | 
			
		||||
    {
 | 
			
		||||
        public YamlConfiguration()
 | 
			
		||||
        {
 | 
			
		||||
            ReRoutes = new List<YamlReRoute>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<YamlReRoute> ReRoutes { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,9 @@
 | 
			
		||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
 | 
			
		||||
{
 | 
			
		||||
    public class YamlReRoute
 | 
			
		||||
    {
 | 
			
		||||
        public string DownstreamTemplate { get; set; }
 | 
			
		||||
        public string UpstreamTemplate { get; set; }
 | 
			
		||||
        public string UpstreamHttpMethod { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user