mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 06:48:16 +08:00
* #463 save both files * #463 made it so we dont save to disk on startup unless using admin api
This commit is contained in:
@ -9,15 +9,16 @@ namespace Ocelot.Configuration.Repository
|
||||
{
|
||||
public class DiskFileConfigurationRepository : IFileConfigurationRepository
|
||||
{
|
||||
private readonly string _configFilePath;
|
||||
|
||||
private readonly string _environmentFilePath;
|
||||
private readonly string _ocelotFilePath;
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
private const string ConfigurationFileName = "ocelot";
|
||||
|
||||
public DiskFileConfigurationRepository(IHostingEnvironment hostingEnvironment)
|
||||
{
|
||||
_configFilePath = $"{AppContext.BaseDirectory}/{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
|
||||
_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
|
||||
|
||||
_ocelotFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}.json";
|
||||
}
|
||||
|
||||
public Task<Response<FileConfiguration>> Get()
|
||||
@ -26,7 +27,7 @@ namespace Ocelot.Configuration.Repository
|
||||
|
||||
lock(_lock)
|
||||
{
|
||||
jsonConfiguration = System.IO.File.ReadAllText(_configFilePath);
|
||||
jsonConfiguration = System.IO.File.ReadAllText(_environmentFilePath);
|
||||
}
|
||||
|
||||
var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(jsonConfiguration);
|
||||
@ -40,12 +41,19 @@ namespace Ocelot.Configuration.Repository
|
||||
|
||||
lock(_lock)
|
||||
{
|
||||
if (System.IO.File.Exists(_configFilePath))
|
||||
if (System.IO.File.Exists(_environmentFilePath))
|
||||
{
|
||||
System.IO.File.Delete(_configFilePath);
|
||||
System.IO.File.Delete(_environmentFilePath);
|
||||
}
|
||||
|
||||
System.IO.File.WriteAllText(_configFilePath, jsonConfiguration);
|
||||
System.IO.File.WriteAllText(_environmentFilePath, jsonConfiguration);
|
||||
|
||||
if (System.IO.File.Exists(_ocelotFilePath))
|
||||
{
|
||||
System.IO.File.Delete(_ocelotFilePath);
|
||||
}
|
||||
|
||||
System.IO.File.WriteAllText(_ocelotFilePath, jsonConfiguration);
|
||||
}
|
||||
|
||||
return Task.FromResult<Response>(new OkResponse());
|
||||
|
@ -6,6 +6,7 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics;
|
||||
using DependencyInjection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Creator;
|
||||
@ -27,18 +28,18 @@
|
||||
await builder.UseOcelot(new OcelotPipelineConfiguration());
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static async Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder builder, Action<OcelotPipelineConfiguration> pipelineConfiguration)
|
||||
{
|
||||
var config = new OcelotPipelineConfiguration();
|
||||
pipelineConfiguration?.Invoke(config);
|
||||
return await builder.UseOcelot(config);
|
||||
}
|
||||
{
|
||||
var config = new OcelotPipelineConfiguration();
|
||||
pipelineConfiguration?.Invoke(config);
|
||||
return await builder.UseOcelot(config);
|
||||
}
|
||||
public static async Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder builder, OcelotPipelineConfiguration pipelineConfiguration)
|
||||
{
|
||||
var configuration = await CreateConfiguration(builder);
|
||||
|
||||
var configuration = await CreateConfiguration(builder);
|
||||
|
||||
CreateAdministrationArea(builder, configuration);
|
||||
|
||||
if (UsingRafty(builder))
|
||||
@ -105,9 +106,9 @@
|
||||
{
|
||||
// make configuration from file system?
|
||||
// earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this
|
||||
var fileConfig = (IOptions<FileConfiguration>)builder.ApplicationServices.GetService(typeof(IOptions<FileConfiguration>));
|
||||
|
||||
// now create the config
|
||||
var fileConfig = (IOptions<FileConfiguration>)builder.ApplicationServices.GetService(typeof(IOptions<FileConfiguration>));
|
||||
|
||||
// now create the config
|
||||
var internalConfigCreator = (IInternalConfigurationCreator)builder.ApplicationServices.GetService(typeof(IInternalConfigurationCreator));
|
||||
var internalConfig = await internalConfigCreator.Create(fileConfig.Value);
|
||||
|
||||
@ -115,22 +116,32 @@
|
||||
var internalConfigRepo = (IInternalConfigurationRepository)builder.ApplicationServices.GetService(typeof(IInternalConfigurationRepository));
|
||||
internalConfigRepo.AddOrReplace(internalConfig.Data);
|
||||
|
||||
var fileConfigSetter = (IFileConfigurationSetter)builder.ApplicationServices.GetService(typeof(IFileConfigurationSetter));
|
||||
|
||||
var fileConfigRepo = (IFileConfigurationRepository)builder.ApplicationServices.GetService(typeof(IFileConfigurationRepository));
|
||||
|
||||
var adminPath = (IAdministrationPath)builder.ApplicationServices.GetService(typeof(IAdministrationPath));
|
||||
|
||||
if (UsingConsul(fileConfigRepo))
|
||||
{
|
||||
{
|
||||
//Lots of jazz happens in here..check it out if you are using consul to store your config.
|
||||
await SetFileConfigInConsul(builder, fileConfigRepo, fileConfig, internalConfigCreator, internalConfigRepo);
|
||||
}
|
||||
else
|
||||
{
|
||||
else if(AdministrationApiInUse(adminPath))
|
||||
{
|
||||
//We have to make sure the file config is set for the ocelot.env.json and ocelot.json so that if we pull it from the
|
||||
//admin api it works...boy this is getting a spit spags boll.
|
||||
var fileConfigSetter = (IFileConfigurationSetter)builder.ApplicationServices.GetService(typeof(IFileConfigurationSetter));
|
||||
|
||||
await SetFileConfig(fileConfigSetter, fileConfig);
|
||||
}
|
||||
|
||||
return GetOcelotConfigAndReturn(internalConfigRepo);
|
||||
}
|
||||
|
||||
private static bool AdministrationApiInUse(IAdministrationPath adminPath)
|
||||
{
|
||||
return adminPath.GetType() != typeof(NullAdministrationPath);
|
||||
}
|
||||
|
||||
private static async Task SetFileConfigInConsul(IApplicationBuilder builder,
|
||||
IFileConfigurationRepository fileConfigRepo, IOptions<FileConfiguration> fileConfig,
|
||||
IInternalConfigurationCreator internalConfigCreator, IInternalConfigurationRepository internalConfigRepo)
|
||||
@ -179,8 +190,7 @@
|
||||
|
||||
private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptions<FileConfiguration> fileConfig)
|
||||
{
|
||||
Response response;
|
||||
response = await fileConfigSetter.Set(fileConfig.Value);
|
||||
var response = await fileConfigSetter.Set(fileConfig.Value);
|
||||
|
||||
if (IsError(response))
|
||||
{
|
||||
@ -245,8 +255,8 @@
|
||||
var listener = (OcelotDiagnosticListener)builder.ApplicationServices.GetService(typeof(OcelotDiagnosticListener));
|
||||
var diagnosticListener = (DiagnosticListener)builder.ApplicationServices.GetService(typeof(DiagnosticListener));
|
||||
diagnosticListener.SubscribeWithAdapter(listener);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void OnShutdown(IApplicationBuilder app)
|
||||
{
|
||||
var node = (INode)app.ApplicationServices.GetService(typeof(INode));
|
||||
|
Reference in New Issue
Block a user