mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 14:58:16 +08:00
Merge branch 'feature/config_grow_when_merged' of https://github.com/snb83/Ocelot into snb83-feature/config_grow_when_merged
This commit is contained in:
@ -9,6 +9,7 @@ namespace Ocelot.DependencyInjection
|
||||
using System.Text.RegularExpressions;
|
||||
using Configuration.File;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
public static class ConfigurationBuilderExtensions
|
||||
{
|
||||
@ -28,37 +29,42 @@ namespace Ocelot.DependencyInjection
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder)
|
||||
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, IHostingEnvironment env = null)
|
||||
{
|
||||
return builder.AddOcelot(".");
|
||||
return builder.AddOcelot(".", env);
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, string folder)
|
||||
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, string folder, IHostingEnvironment env = null)
|
||||
{
|
||||
const string pattern = "(?i)ocelot\\.([a-zA-Z0-9]*)(\\.json)$";
|
||||
const string primaryConfigFile = "ocelot.json";
|
||||
|
||||
var reg = new Regex(pattern);
|
||||
const string globalConfigFile = "ocelot.global.json";
|
||||
|
||||
var files = Directory.GetFiles(folder)
|
||||
.Where(path => reg.IsMatch(path))
|
||||
const string subConfigPattern = @"^ocelot\.[a-zA-Z0-9]+\.json$";
|
||||
|
||||
string excludeConfigName = env?.EnvironmentName != null ? $"ocelot.{env.EnvironmentName}.json" : string.Empty;
|
||||
|
||||
var reg = new Regex(subConfigPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
|
||||
|
||||
var files = new DirectoryInfo(folder)
|
||||
.EnumerateFiles()
|
||||
.Where(fi => reg.IsMatch(fi.Name) && (fi.Name != excludeConfigName))
|
||||
.ToList();
|
||||
|
||||
var fileConfiguration = new FileConfiguration();
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
// windows and unix sigh...
|
||||
if(files.Count > 1 && (Path.GetFileName(file) == "ocelot.json"))
|
||||
if(files.Count > 1 && file.Name.Equals(primaryConfigFile, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var lines = File.ReadAllText(file);
|
||||
var lines = File.ReadAllText(file.FullName);
|
||||
|
||||
var config = JsonConvert.DeserializeObject<FileConfiguration>(lines);
|
||||
|
||||
// windows and unix sigh...
|
||||
if (Path.GetFileName(file) == "ocelot.global.json")
|
||||
if (file.Name.Equals(globalConfigFile, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
fileConfiguration.GlobalConfiguration = config.GlobalConfiguration;
|
||||
}
|
||||
@ -69,9 +75,9 @@ namespace Ocelot.DependencyInjection
|
||||
|
||||
var json = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
File.WriteAllText("ocelot.json", json);
|
||||
File.WriteAllText(primaryConfigFile, json);
|
||||
|
||||
builder.AddJsonFile("ocelot.json", false, false);
|
||||
builder.AddJsonFile(primaryConfigFile, false, false);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
Reference in New Issue
Block a user