mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 01:18:15 +08:00
Feat/monorepo (#734)
* copied everything from repos back to ocelot repo * added src projects to sln * removed all test projects that have no tests * added all test projects to sln * removed test not on master * merged unit tests * merged acceptance tests * merged integration tests * fixed namepaces * build script creates packages for all projects * updated docs to make sure no references to external repos that we will remove * +semver: breaking
This commit is contained in:
@ -0,0 +1,93 @@
|
||||
namespace Ocelot.Provider.Consul
|
||||
{
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Configuration.Creator;
|
||||
using Configuration.File;
|
||||
using Configuration.Repository;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Middleware;
|
||||
using Responses;
|
||||
|
||||
public static class ConsulMiddlewareConfigurationProvider
|
||||
{
|
||||
public static OcelotMiddlewareConfigurationDelegate Get = async builder =>
|
||||
{
|
||||
var fileConfigRepo = builder.ApplicationServices.GetService<IFileConfigurationRepository>();
|
||||
var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>();
|
||||
var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>();
|
||||
var internalConfigRepo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>();
|
||||
|
||||
if (UsingConsul(fileConfigRepo))
|
||||
{
|
||||
await SetFileConfigInConsul(builder, fileConfigRepo, fileConfig, internalConfigCreator, internalConfigRepo);
|
||||
}
|
||||
};
|
||||
|
||||
private static bool UsingConsul(IFileConfigurationRepository fileConfigRepo)
|
||||
{
|
||||
return fileConfigRepo.GetType() == typeof(ConsulFileConfigurationRepository);
|
||||
}
|
||||
|
||||
private static async Task SetFileConfigInConsul(IApplicationBuilder builder,
|
||||
IFileConfigurationRepository fileConfigRepo, IOptionsMonitor<FileConfiguration> fileConfig,
|
||||
IInternalConfigurationCreator internalConfigCreator, IInternalConfigurationRepository internalConfigRepo)
|
||||
{
|
||||
// get the config from consul.
|
||||
var fileConfigFromConsul = await fileConfigRepo.Get();
|
||||
|
||||
if (IsError(fileConfigFromConsul))
|
||||
{
|
||||
ThrowToStopOcelotStarting(fileConfigFromConsul);
|
||||
}
|
||||
else if (ConfigNotStoredInConsul(fileConfigFromConsul))
|
||||
{
|
||||
//there was no config in consul set the file in config in consul
|
||||
await fileConfigRepo.Set(fileConfig.CurrentValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
// create the internal config from consul data
|
||||
var internalConfig = await internalConfigCreator.Create(fileConfigFromConsul.Data);
|
||||
|
||||
if (IsError(internalConfig))
|
||||
{
|
||||
ThrowToStopOcelotStarting(internalConfig);
|
||||
}
|
||||
else
|
||||
{
|
||||
// add the internal config to the internal repo
|
||||
var response = internalConfigRepo.AddOrReplace(internalConfig.Data);
|
||||
|
||||
if (IsError(response))
|
||||
{
|
||||
ThrowToStopOcelotStarting(response);
|
||||
}
|
||||
}
|
||||
|
||||
if (IsError(internalConfig))
|
||||
{
|
||||
ThrowToStopOcelotStarting(internalConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ThrowToStopOcelotStarting(Response config)
|
||||
{
|
||||
throw new Exception($"Unable to start Ocelot, errors are: {string.Join(",", config.Errors.Select(x => x.ToString()))}");
|
||||
}
|
||||
|
||||
private static bool IsError(Response response)
|
||||
{
|
||||
return response == null || response.IsError;
|
||||
}
|
||||
|
||||
private static bool ConfigNotStoredInConsul(Response<FileConfiguration> fileConfigFromConsul)
|
||||
{
|
||||
return fileConfigFromConsul.Data == null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user