mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 12:58:16 +08:00
* change config creator to not throw exception in there is an error......lord i hate this config creator code I need to sort it out. * Remove method that we are not using anymore.. * throw exception and add errors to message * train hacking and some refactoring * bs test for code coverage * actually return the errors in the exception
This commit is contained in:
@ -66,35 +66,20 @@ namespace Ocelot.Configuration.Creator
|
||||
_fileReRouteOptionsCreator = fileReRouteOptionsCreator;
|
||||
_httpHandlerOptionsCreator = httpHandlerOptionsCreator;
|
||||
}
|
||||
|
||||
public async Task<Response<IOcelotConfiguration>> Create()
|
||||
{
|
||||
var config = await SetUpConfiguration(_options.Value);
|
||||
|
||||
return new OkResponse<IOcelotConfiguration>(config);
|
||||
}
|
||||
|
||||
|
||||
public async Task<Response<IOcelotConfiguration>> Create(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var config = await SetUpConfiguration(fileConfiguration);
|
||||
|
||||
return new OkResponse<IOcelotConfiguration>(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
private async Task<IOcelotConfiguration> SetUpConfiguration(FileConfiguration fileConfiguration)
|
||||
private async Task<Response<IOcelotConfiguration>> SetUpConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var response = await _configurationValidator.IsValid(fileConfiguration);
|
||||
|
||||
if (response.Data.IsError)
|
||||
{
|
||||
var errorBuilder = new StringBuilder();
|
||||
|
||||
foreach (var error in response.Errors)
|
||||
{
|
||||
errorBuilder.AppendLine(error.Message);
|
||||
}
|
||||
|
||||
throw new Exception($"Unable to start Ocelot..configuration, errors were {errorBuilder}");
|
||||
return new ErrorResponse<IOcelotConfiguration>(response.Data.Errors);
|
||||
}
|
||||
|
||||
var reRoutes = new List<ReRoute>();
|
||||
@ -107,7 +92,9 @@ namespace Ocelot.Configuration.Creator
|
||||
|
||||
var serviceProviderConfiguration = _serviceProviderConfigCreator.Create(fileConfiguration.GlobalConfiguration);
|
||||
|
||||
return new OcelotConfiguration(reRoutes, fileConfiguration.GlobalConfiguration.AdministrationPath, serviceProviderConfiguration);
|
||||
var config = new OcelotConfiguration(reRoutes, fileConfiguration.GlobalConfiguration.AdministrationPath, serviceProviderConfiguration);
|
||||
|
||||
return new OkResponse<IOcelotConfiguration>(config);
|
||||
}
|
||||
|
||||
private ReRoute SetUpReRoute(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration)
|
||||
|
@ -6,7 +6,6 @@ namespace Ocelot.Configuration.Creator
|
||||
{
|
||||
public interface IOcelotConfigurationCreator
|
||||
{
|
||||
Task<Response<IOcelotConfiguration>> Create();
|
||||
Task<Response<IOcelotConfiguration>> Create(FileConfiguration fileConfiguration);
|
||||
}
|
||||
}
|
@ -10,5 +10,10 @@ namespace Ocelot.Errors
|
||||
|
||||
public string Message { get; private set; }
|
||||
public OcelotErrorCode Code { get; private set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Message;
|
||||
}
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ using Ocelot.RateLimit.Middleware;
|
||||
namespace Ocelot.Middleware
|
||||
{
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Authorisation.Middleware;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@ -150,61 +151,108 @@ namespace Ocelot.Middleware
|
||||
|
||||
private static async Task<IOcelotConfiguration> CreateConfiguration(IApplicationBuilder builder)
|
||||
{
|
||||
var fileConfig = (IOptions<FileConfiguration>)builder.ApplicationServices.GetService(typeof(IOptions<FileConfiguration>));
|
||||
|
||||
var configSetter = (IFileConfigurationSetter)builder.ApplicationServices.GetService(typeof(IFileConfigurationSetter));
|
||||
|
||||
var configProvider = (IOcelotConfigurationProvider)builder.ApplicationServices.GetService(typeof(IOcelotConfigurationProvider));
|
||||
var deps = GetDependencies(builder);
|
||||
|
||||
var ocelotConfiguration = await configProvider.Get();
|
||||
var ocelotConfiguration = await deps.provider.Get();
|
||||
|
||||
if (ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError)
|
||||
if (ConfigurationNotSetUp(ocelotConfiguration))
|
||||
{
|
||||
Response config = null;
|
||||
var fileConfigRepo = builder.ApplicationServices.GetService(typeof(IFileConfigurationRepository));
|
||||
if (fileConfigRepo.GetType() == typeof(ConsulFileConfigurationRepository))
|
||||
var response = await SetConfig(builder, deps.fileConfiguration, deps.setter, deps.provider, deps.repo);
|
||||
|
||||
if (UnableToSetConfig(response))
|
||||
{
|
||||
var consulFileConfigRepo = (ConsulFileConfigurationRepository) fileConfigRepo;
|
||||
var ocelotConfigurationRepository =
|
||||
(IOcelotConfigurationRepository) builder.ApplicationServices.GetService(
|
||||
typeof(IOcelotConfigurationRepository));
|
||||
var ocelotConfigurationCreator =
|
||||
(IOcelotConfigurationCreator) builder.ApplicationServices.GetService(
|
||||
typeof(IOcelotConfigurationCreator));
|
||||
|
||||
var fileConfigFromConsul = await consulFileConfigRepo.Get();
|
||||
if (fileConfigFromConsul.Data == null)
|
||||
{
|
||||
config = await configSetter.Set(fileConfig.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
var ocelotConfig = await ocelotConfigurationCreator.Create(fileConfigFromConsul.Data);
|
||||
config = await ocelotConfigurationRepository.AddOrReplace(ocelotConfig.Data);
|
||||
var hack = builder.ApplicationServices.GetService(typeof(ConsulFileConfigurationPoller));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
config = await configSetter.Set(fileConfig.Value);
|
||||
}
|
||||
|
||||
if (config == null || config.IsError)
|
||||
{
|
||||
throw new Exception("Unable to start Ocelot: configuration was not set up correctly.");
|
||||
ThrowToStopOcelotStarting(response);
|
||||
}
|
||||
}
|
||||
|
||||
ocelotConfiguration = await configProvider.Get();
|
||||
return await GetOcelotConfigAndReturn(deps.provider);
|
||||
}
|
||||
|
||||
private static async Task<Response> SetConfig(IApplicationBuilder builder, IOptions<FileConfiguration> fileConfiguration, IFileConfigurationSetter setter, IOcelotConfigurationProvider provider, IFileConfigurationRepository repo)
|
||||
{
|
||||
if (UsingConsul(repo))
|
||||
{
|
||||
return await SetUpConfigFromConsul(builder, repo, setter, fileConfiguration);
|
||||
}
|
||||
|
||||
return await setter.Set(fileConfiguration.Value);
|
||||
}
|
||||
|
||||
private static bool UnableToSetConfig(Response response)
|
||||
{
|
||||
return response == null || response.IsError;
|
||||
}
|
||||
|
||||
private static bool ConfigurationNotSetUp(Response<IOcelotConfiguration> ocelotConfiguration)
|
||||
{
|
||||
return ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError;
|
||||
}
|
||||
|
||||
private static (IOptions<FileConfiguration> fileConfiguration, IFileConfigurationSetter setter, IOcelotConfigurationProvider provider, IFileConfigurationRepository repo) GetDependencies(IApplicationBuilder builder)
|
||||
{
|
||||
var fileConfiguration = (IOptions<FileConfiguration>)builder.ApplicationServices.GetService(typeof(IOptions<FileConfiguration>));
|
||||
|
||||
var setter = (IFileConfigurationSetter)builder.ApplicationServices.GetService(typeof(IFileConfigurationSetter));
|
||||
|
||||
var provider = (IOcelotConfigurationProvider)builder.ApplicationServices.GetService(typeof(IOcelotConfigurationProvider));
|
||||
|
||||
var repo = (IFileConfigurationRepository)builder.ApplicationServices.GetService(typeof(IFileConfigurationRepository));
|
||||
|
||||
return (fileConfiguration, setter, provider, repo);
|
||||
}
|
||||
|
||||
private static async Task<IOcelotConfiguration> GetOcelotConfigAndReturn(IOcelotConfigurationProvider provider)
|
||||
{
|
||||
var ocelotConfiguration = await provider.Get();
|
||||
|
||||
if(ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError)
|
||||
{
|
||||
throw new Exception("Unable to start Ocelot: ocelot configuration was not returned by provider.");
|
||||
ThrowToStopOcelotStarting(ocelotConfiguration);
|
||||
}
|
||||
|
||||
return ocelotConfiguration.Data;
|
||||
}
|
||||
|
||||
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 UsingConsul(IFileConfigurationRepository fileConfigRepo)
|
||||
{
|
||||
return fileConfigRepo.GetType() == typeof(ConsulFileConfigurationRepository);
|
||||
}
|
||||
|
||||
private static async Task<Response> SetUpConfigFromConsul(IApplicationBuilder builder, IFileConfigurationRepository consulFileConfigRepo, IFileConfigurationSetter setter, IOptions<FileConfiguration> fileConfig)
|
||||
{
|
||||
Response config = null;
|
||||
|
||||
var ocelotConfigurationRepository =
|
||||
(IOcelotConfigurationRepository) builder.ApplicationServices.GetService(
|
||||
typeof(IOcelotConfigurationRepository));
|
||||
var ocelotConfigurationCreator =
|
||||
(IOcelotConfigurationCreator) builder.ApplicationServices.GetService(
|
||||
typeof(IOcelotConfigurationCreator));
|
||||
|
||||
var fileConfigFromConsul = await consulFileConfigRepo.Get();
|
||||
if (fileConfigFromConsul.Data == null)
|
||||
{
|
||||
config = await setter.Set(fileConfig.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
var ocelotConfig = await ocelotConfigurationCreator.Create(fileConfigFromConsul.Data);
|
||||
if(ocelotConfig.IsError)
|
||||
{
|
||||
return new ErrorResponse(ocelotConfig.Errors);
|
||||
}
|
||||
config = await ocelotConfigurationRepository.AddOrReplace(ocelotConfig.Data);
|
||||
var hack = builder.ApplicationServices.GetService(typeof(ConsulFileConfigurationPoller));
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
|
||||
private static async Task CreateAdministrationArea(IApplicationBuilder builder, IOcelotConfiguration configuration)
|
||||
{
|
||||
var identityServerConfiguration = (IIdentityServerConfiguration)builder.ApplicationServices.GetService(typeof(IIdentityServerConfiguration));
|
||||
|
Reference in New Issue
Block a user