mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 11:58:15 +08:00
Reload config on change (#527)
* Reload config on change. Added test case. * added testing for adding the json with reloadOnChange = false
This commit is contained in:

committed by
Tom Pallister

parent
89f0cc786a
commit
ad8025df1b
@ -113,12 +113,12 @@
|
||||
{
|
||||
// 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 = builder.ApplicationServices.GetService<IOptions<FileConfiguration>>();
|
||||
var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>();
|
||||
|
||||
// now create the config
|
||||
var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>();
|
||||
var internalConfig = await internalConfigCreator.Create(fileConfig.Value);
|
||||
//Configuration error, throw error message
|
||||
var internalConfig = await internalConfigCreator.Create(fileConfig.CurrentValue);
|
||||
//Configuration error, throw error message
|
||||
if (internalConfig.IsError)
|
||||
{
|
||||
ThrowToStopOcelotStarting(internalConfig);
|
||||
@ -128,6 +128,12 @@
|
||||
var internalConfigRepo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>();
|
||||
internalConfigRepo.AddOrReplace(internalConfig.Data);
|
||||
|
||||
fileConfig.OnChange(async (config) =>
|
||||
{
|
||||
var newInternalConfig = await internalConfigCreator.Create(config);
|
||||
internalConfigRepo.AddOrReplace(newInternalConfig.Data);
|
||||
});
|
||||
|
||||
var fileConfigRepo = builder.ApplicationServices.GetService<IFileConfigurationRepository>();
|
||||
|
||||
var adminPath = builder.ApplicationServices.GetService<IAdministrationPath>();
|
||||
@ -155,7 +161,7 @@
|
||||
}
|
||||
|
||||
private static async Task SetFileConfigInConsul(IApplicationBuilder builder,
|
||||
IFileConfigurationRepository fileConfigRepo, IOptions<FileConfiguration> fileConfig,
|
||||
IFileConfigurationRepository fileConfigRepo, IOptionsMonitor<FileConfiguration> fileConfig,
|
||||
IInternalConfigurationCreator internalConfigCreator, IInternalConfigurationRepository internalConfigRepo)
|
||||
{
|
||||
// get the config from consul.
|
||||
@ -168,7 +174,7 @@
|
||||
else if (ConfigNotStoredInConsul(fileConfigFromConsul))
|
||||
{
|
||||
//there was no config in consul set the file in config in consul
|
||||
await fileConfigRepo.Set(fileConfig.Value);
|
||||
await fileConfigRepo.Set(fileConfig.CurrentValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -197,9 +203,9 @@
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptions<FileConfiguration> fileConfig)
|
||||
private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptionsMonitor<FileConfiguration> fileConfig)
|
||||
{
|
||||
var response = await fileConfigSetter.Set(fileConfig.Value);
|
||||
var response = await fileConfigSetter.Set(fileConfig.CurrentValue);
|
||||
|
||||
if (IsError(response))
|
||||
{
|
||||
|
Reference in New Issue
Block a user