mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	changed to json configuration to get rid of yaml imports
This commit is contained in:
		@@ -3,9 +3,9 @@ using System.Collections.Generic;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using Microsoft.Extensions.Logging;
 | 
			
		||||
using Microsoft.Extensions.Options;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Configuration.Parser;
 | 
			
		||||
using Ocelot.Configuration.Validator;
 | 
			
		||||
using Ocelot.Configuration.Yaml;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Creator
 | 
			
		||||
@@ -13,20 +13,20 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Register as singleton
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class YamlOcelotConfigurationCreator : IOcelotConfigurationCreator
 | 
			
		||||
    public class FileOcelotConfigurationCreator : IOcelotConfigurationCreator
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IOptions<YamlConfiguration> _options;
 | 
			
		||||
        private readonly IOptions<FileConfiguration> _options;
 | 
			
		||||
        private readonly IConfigurationValidator _configurationValidator;
 | 
			
		||||
        private const string RegExMatchEverything = ".*";
 | 
			
		||||
        private const string RegExMatchEndString = "$";
 | 
			
		||||
        private readonly IClaimToThingConfigurationParser _claimToThingConfigurationParser;
 | 
			
		||||
        private readonly ILogger<YamlOcelotConfigurationCreator> _logger;
 | 
			
		||||
        private readonly ILogger<FileOcelotConfigurationCreator> _logger;
 | 
			
		||||
 | 
			
		||||
        public YamlOcelotConfigurationCreator(
 | 
			
		||||
            IOptions<YamlConfiguration> options, 
 | 
			
		||||
        public FileOcelotConfigurationCreator(
 | 
			
		||||
            IOptions<FileConfiguration> options, 
 | 
			
		||||
            IConfigurationValidator configurationValidator, 
 | 
			
		||||
            IClaimToThingConfigurationParser claimToThingConfigurationParser, 
 | 
			
		||||
            ILogger<YamlOcelotConfigurationCreator> logger)
 | 
			
		||||
            ILogger<FileOcelotConfigurationCreator> logger)
 | 
			
		||||
        {
 | 
			
		||||
            _options = options;
 | 
			
		||||
            _configurationValidator = configurationValidator;
 | 
			
		||||
@@ -42,7 +42,7 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// This method is meant to be tempoary to convert a yaml config to an ocelot config...probably wont keep this but we will see
 | 
			
		||||
        /// This method is meant to be tempoary to convert a config to an ocelot config...probably wont keep this but we will see
 | 
			
		||||
        /// will need a refactor at some point as its crap
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        private IOcelotConfiguration SetUpConfiguration()
 | 
			
		||||
@@ -63,16 +63,16 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
 | 
			
		||||
            var reRoutes = new List<ReRoute>();
 | 
			
		||||
 | 
			
		||||
            foreach (var yamlReRoute in _options.Value.ReRoutes)
 | 
			
		||||
            foreach (var reRoute in _options.Value.ReRoutes)
 | 
			
		||||
            {
 | 
			
		||||
                var ocelotReRoute = SetUpReRoute(yamlReRoute);
 | 
			
		||||
                var ocelotReRoute = SetUpReRoute(reRoute);
 | 
			
		||||
                reRoutes.Add(ocelotReRoute);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            return new OcelotConfiguration(reRoutes);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private ReRoute SetUpReRoute(YamlReRoute reRoute)
 | 
			
		||||
        private ReRoute SetUpReRoute(FileReRoute reRoute)
 | 
			
		||||
        {
 | 
			
		||||
            var upstreamTemplate = reRoute.UpstreamTemplate;
 | 
			
		||||
 | 
			
		||||
@@ -1,9 +1,14 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Yaml
 | 
			
		||||
namespace Ocelot.Configuration.File
 | 
			
		||||
{
 | 
			
		||||
    public class YamlAuthenticationOptions
 | 
			
		||||
    public class FileAuthenticationOptions
 | 
			
		||||
    {
 | 
			
		||||
        public FileAuthenticationOptions()
 | 
			
		||||
        {
 | 
			
		||||
            AdditionalScopes = new List<string>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Provider { get; set; }
 | 
			
		||||
        public string ProviderRootUrl { get; set; }
 | 
			
		||||
        public string ScopeName { get; set; }
 | 
			
		||||
							
								
								
									
										14
									
								
								src/Ocelot/Configuration/File/FileConfiguration.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/Ocelot/Configuration/File/FileConfiguration.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.File
 | 
			
		||||
{
 | 
			
		||||
    public class FileConfiguration
 | 
			
		||||
    {
 | 
			
		||||
        public FileConfiguration()
 | 
			
		||||
        {
 | 
			
		||||
            ReRoutes = new List<FileReRoute>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<FileReRoute> ReRoutes { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,21 +1,22 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Yaml
 | 
			
		||||
namespace Ocelot.Configuration.File
 | 
			
		||||
{
 | 
			
		||||
    public class YamlReRoute
 | 
			
		||||
    public class FileReRoute
 | 
			
		||||
    {
 | 
			
		||||
        public YamlReRoute()
 | 
			
		||||
        public FileReRoute()
 | 
			
		||||
        {
 | 
			
		||||
            AddHeadersToRequest = new Dictionary<string, string>();
 | 
			
		||||
            AddClaimsToRequest = new Dictionary<string, string>();
 | 
			
		||||
            RouteClaimsRequirement = new Dictionary<string, string>();
 | 
			
		||||
            AddQueriesToRequest = new Dictionary<string, string>();
 | 
			
		||||
            AuthenticationOptions = new FileAuthenticationOptions();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string DownstreamTemplate { get; set; }
 | 
			
		||||
        public string UpstreamTemplate { get; set; }
 | 
			
		||||
        public string UpstreamHttpMethod { get; set; }
 | 
			
		||||
        public YamlAuthenticationOptions AuthenticationOptions { get; set; }
 | 
			
		||||
        public FileAuthenticationOptions AuthenticationOptions { get; set; }
 | 
			
		||||
        public Dictionary<string, string> AddHeadersToRequest { get; set; }
 | 
			
		||||
        public Dictionary<string, string> AddClaimsToRequest { get; set; }
 | 
			
		||||
        public Dictionary<string, string> RouteClaimsRequirement { get; set; }
 | 
			
		||||
@@ -2,15 +2,15 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Ocelot.Authentication.Handler;
 | 
			
		||||
using Ocelot.Configuration.Yaml;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Validator
 | 
			
		||||
{
 | 
			
		||||
    public class YamlConfigurationValidator : IConfigurationValidator
 | 
			
		||||
    public class FileConfigurationValidator : IConfigurationValidator
 | 
			
		||||
    {
 | 
			
		||||
        public Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration)
 | 
			
		||||
        public Response<ConfigurationValidationResult> IsValid(FileConfiguration configuration)
 | 
			
		||||
        {
 | 
			
		||||
            var result = CheckForDupliateReRoutes(configuration);
 | 
			
		||||
 | 
			
		||||
@@ -29,25 +29,25 @@ namespace Ocelot.Configuration.Validator
 | 
			
		||||
            return new OkResponse<ConfigurationValidationResult>(result);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private ConfigurationValidationResult CheckForUnsupportedAuthenticationProviders(YamlConfiguration configuration)
 | 
			
		||||
        private ConfigurationValidationResult CheckForUnsupportedAuthenticationProviders(FileConfiguration configuration)
 | 
			
		||||
        {
 | 
			
		||||
            var errors = new List<Error>();
 | 
			
		||||
 | 
			
		||||
            foreach (var yamlReRoute in configuration.ReRoutes)
 | 
			
		||||
            foreach (var reRoute in configuration.ReRoutes)
 | 
			
		||||
            {
 | 
			
		||||
                var isAuthenticated = !string.IsNullOrEmpty(yamlReRoute.AuthenticationOptions?.Provider);
 | 
			
		||||
                var isAuthenticated = !string.IsNullOrEmpty(reRoute.AuthenticationOptions?.Provider);
 | 
			
		||||
 | 
			
		||||
                if (!isAuthenticated)
 | 
			
		||||
                {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (IsSupportedAuthenticationProvider(yamlReRoute.AuthenticationOptions?.Provider))
 | 
			
		||||
                if (IsSupportedAuthenticationProvider(reRoute.AuthenticationOptions?.Provider))
 | 
			
		||||
                {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                var error = new UnsupportedAuthenticationProviderError($"{yamlReRoute.AuthenticationOptions?.Provider} is unsupported authentication provider, upstream template is {yamlReRoute.UpstreamTemplate}, upstream method is {yamlReRoute.UpstreamHttpMethod}");
 | 
			
		||||
                var error = new UnsupportedAuthenticationProviderError($"{reRoute.AuthenticationOptions?.Provider} is unsupported authentication provider, upstream template is {reRoute.UpstreamTemplate}, upstream method is {reRoute.UpstreamHttpMethod}");
 | 
			
		||||
                errors.Add(error);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -63,7 +63,7 @@ namespace Ocelot.Configuration.Validator
 | 
			
		||||
            return Enum.TryParse(provider, true, out supportedProvider);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private ConfigurationValidationResult CheckForDupliateReRoutes(YamlConfiguration configuration)
 | 
			
		||||
        private ConfigurationValidationResult CheckForDupliateReRoutes(FileConfiguration configuration)
 | 
			
		||||
        {
 | 
			
		||||
            var hasDupes = configuration.ReRoutes
 | 
			
		||||
                   .GroupBy(x => new { x.UpstreamTemplate, x.UpstreamHttpMethod }).Any(x => x.Skip(1).Any());
 | 
			
		||||
@@ -1,10 +1,10 @@
 | 
			
		||||
using Ocelot.Configuration.Yaml;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Validator
 | 
			
		||||
{
 | 
			
		||||
    public interface IConfigurationValidator
 | 
			
		||||
    {
 | 
			
		||||
        Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration);
 | 
			
		||||
        Response<ConfigurationValidationResult> IsValid(FileConfiguration configuration);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +0,0 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Yaml
 | 
			
		||||
{
 | 
			
		||||
    public class YamlConfiguration
 | 
			
		||||
    {
 | 
			
		||||
        public YamlConfiguration()
 | 
			
		||||
        {
 | 
			
		||||
            ReRoutes = new List<YamlReRoute>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<YamlReRoute> ReRoutes { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,11 +6,11 @@ using Ocelot.Authentication.Handler.Factory;
 | 
			
		||||
using Ocelot.Authorisation;
 | 
			
		||||
using Ocelot.Claims;
 | 
			
		||||
using Ocelot.Configuration.Creator;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Configuration.Parser;
 | 
			
		||||
using Ocelot.Configuration.Provider;
 | 
			
		||||
using Ocelot.Configuration.Repository;
 | 
			
		||||
using Ocelot.Configuration.Validator;
 | 
			
		||||
using Ocelot.Configuration.Yaml;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.Finder;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
 | 
			
		||||
@@ -26,15 +26,14 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
{
 | 
			
		||||
    public static class ServiceCollectionExtensions
 | 
			
		||||
    {
 | 
			
		||||
        public static IServiceCollection AddOcelotYamlConfiguration(this IServiceCollection services, IConfigurationRoot configurationRoot)
 | 
			
		||||
        public static IServiceCollection AddOcelotFileConfiguration(this IServiceCollection services, IConfigurationRoot configurationRoot)
 | 
			
		||||
        {
 | 
			
		||||
            // initial configuration from yaml
 | 
			
		||||
            services.Configure<YamlConfiguration>(configurationRoot);
 | 
			
		||||
            services.Configure<FileConfiguration>(configurationRoot);
 | 
			
		||||
 | 
			
		||||
            // ocelot services.
 | 
			
		||||
            services.AddSingleton<IOcelotConfigurationCreator, YamlOcelotConfigurationCreator>();
 | 
			
		||||
            services.AddSingleton<IOcelotConfigurationCreator, FileOcelotConfigurationCreator>();
 | 
			
		||||
            services.AddSingleton<IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
 | 
			
		||||
            services.AddSingleton<IConfigurationValidator, YamlConfigurationValidator>();
 | 
			
		||||
            services.AddSingleton<IConfigurationValidator, FileConfigurationValidator>();
 | 
			
		||||
 | 
			
		||||
            return services;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -27,16 +27,12 @@
 | 
			
		||||
        "Microsoft.NETCore.App": {
 | 
			
		||||
            "version": "1.0.1",
 | 
			
		||||
            "type": "platform"
 | 
			
		||||
        },
 | 
			
		||||
        "NetEscapades.Configuration.Yaml": "1.2.0",
 | 
			
		||||
        "YamlDotNet": "4.0.0"
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    "frameworks": {
 | 
			
		||||
        "netcoreapp1.4": {
 | 
			
		||||
            "imports": [
 | 
			
		||||
                "dotnet5.6",
 | 
			
		||||
                "portable-net45+win8"
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user