mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 20:30:50 +08:00 
			
		
		
		
	#296 coming up with ideas for this config merging
This commit is contained in:
		@@ -5,6 +5,12 @@ using Microsoft.Extensions.Configuration.Memory;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.DependencyInjection
 | 
			
		||||
{
 | 
			
		||||
    using System.IO;
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using System.Text.RegularExpressions;
 | 
			
		||||
    using Configuration.File;
 | 
			
		||||
    using Newtonsoft.Json;
 | 
			
		||||
 | 
			
		||||
    public static class ConfigurationBuilderExtensions
 | 
			
		||||
    {
 | 
			
		||||
        [Obsolete("Please set BaseUrl in ocelot.json GlobalConfiguration.BaseUrl")]
 | 
			
		||||
@@ -21,6 +27,21 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
 | 
			
		||||
        public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder)
 | 
			
		||||
        {
 | 
			
		||||
            const string pattern = "(?i)ocelot(.*).json$";
 | 
			
		||||
 | 
			
		||||
            var reg = new Regex(pattern);
 | 
			
		||||
 | 
			
		||||
            var files = Directory.GetFiles(".")
 | 
			
		||||
                .Where(path => reg.IsMatch(path))
 | 
			
		||||
                .ToList();
 | 
			
		||||
 | 
			
		||||
            foreach (var file in files)
 | 
			
		||||
            {
 | 
			
		||||
                var lines = File.ReadAllText(file);
 | 
			
		||||
                var config = JsonConvert.DeserializeObject<FileConfiguration>(lines);
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            //var load all files with ocelot*.json
 | 
			
		||||
            //merge these files into one
 | 
			
		||||
            //save it as ocelot.json
 | 
			
		||||
 
 | 
			
		||||
@@ -70,13 +70,13 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var globalJson = JsonConvert.SerializeObject(globalConfig);
 | 
			
		||||
            //File.WriteAllText("ocelot.global.json", globalJson);
 | 
			
		||||
            File.WriteAllText("ocelot.global.json", globalJson);
 | 
			
		||||
 | 
			
		||||
            var reRouteJson = JsonConvert.SerializeObject(reRoute);
 | 
			
		||||
            //File.WriteAllText("ocelot.reRoute.json", reRouteJson);
 | 
			
		||||
            File.WriteAllText("ocelot.reRoute.json", reRouteJson);
 | 
			
		||||
 | 
			
		||||
            IConfigurationBuilder builder = new ConfigurationBuilder();
 | 
			
		||||
            //builder.AddOcelot();
 | 
			
		||||
            builder.AddOcelot();
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user