after much hacking unit tests passing

This commit is contained in:
Tom Gardham-Pallister
2017-02-21 07:34:47 +00:00
parent d548a86327
commit 2dfdf0bb86
44 changed files with 313 additions and 194 deletions

View File

@ -2,7 +2,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
using Ocelot.Middleware;

View File

@ -1,7 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
using Ocelot.Middleware;

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Net.Http;
using Ocelot.Values;

View File

@ -56,14 +56,21 @@ namespace Ocelot.Configuration.Creator
public async Task<Response<IOcelotConfiguration>> Create()
{
var config = await SetUpConfiguration();
var config = await SetUpConfiguration(_options.Value);
return new OkResponse<IOcelotConfiguration>(config);
}
private async Task<IOcelotConfiguration> SetUpConfiguration()
public async Task<Response<IOcelotConfiguration>> Create(FileConfiguration fileConfiguration)
{
var config = await SetUpConfiguration(fileConfiguration);
return new OkResponse<IOcelotConfiguration>(config);
}
private async Task<IOcelotConfiguration> SetUpConfiguration(FileConfiguration fileConfiguration)
{
var response = _configurationValidator.IsValid(_options.Value);
var response = _configurationValidator.IsValid(fileConfiguration);
if (response.Data.IsError)
{

View File

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Ocelot.Configuration.File;
using Ocelot.Responses;
namespace Ocelot.Configuration.Creator
@ -6,5 +7,6 @@ namespace Ocelot.Configuration.Creator
public interface IOcelotConfigurationCreator
{
Task<Response<IOcelotConfiguration>> Create();
Task<Response<IOcelotConfiguration>> Create(FileConfiguration fileConfiguration);
}
}

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ocelot.Configuration.File
namespace Ocelot.Configuration.File
{
public class FileQoSOptions
{

View File

@ -1,10 +1,9 @@
using System.Threading.Tasks;
using Ocelot.Responses;
using Ocelot.Responses;
namespace Ocelot.Configuration.Provider
{
public interface IOcelotConfigurationProvider
{
Task<Response<IOcelotConfiguration>> Get();
Response<IOcelotConfiguration> Get();
}
}

View File

@ -1,6 +1,4 @@
using System.Threading.Tasks;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.Repository;
using Ocelot.Configuration.Repository;
using Ocelot.Responses;
namespace Ocelot.Configuration.Provider
@ -11,16 +9,13 @@ namespace Ocelot.Configuration.Provider
public class OcelotConfigurationProvider : IOcelotConfigurationProvider
{
private readonly IOcelotConfigurationRepository _repo;
private readonly IOcelotConfigurationCreator _creator;
public OcelotConfigurationProvider(IOcelotConfigurationRepository repo,
IOcelotConfigurationCreator creator)
public OcelotConfigurationProvider(IOcelotConfigurationRepository repo)
{
_repo = repo;
_creator = creator;
}
public async Task<Response<IOcelotConfiguration>> Get()
public Response<IOcelotConfiguration> Get()
{
var repoConfig = _repo.Get();
@ -29,20 +24,6 @@ namespace Ocelot.Configuration.Provider
return new ErrorResponse<IOcelotConfiguration>(repoConfig.Errors);
}
if (repoConfig.Data == null)
{
var creatorConfig = await _creator.Create();
if (creatorConfig.IsError)
{
return new ErrorResponse<IOcelotConfiguration>(creatorConfig.Errors);
}
_repo.AddOrReplace(creatorConfig.Data);
return new OkResponse<IOcelotConfiguration>(creatorConfig.Data);
}
return new OkResponse<IOcelotConfiguration>(repoConfig.Data);
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Net.Http;
using Ocelot.Values;

View File

@ -0,0 +1,32 @@
using System.Threading.Tasks;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Repository;
using Ocelot.Responses;
namespace Ocelot.Configuration.Setter
{
public class FileConfigurationSetter : IFileConfigurationSetter
{
private readonly IOcelotConfigurationRepository _configRepo;
private readonly IOcelotConfigurationCreator _configCreator;
public FileConfigurationSetter(IOcelotConfigurationRepository configRepo, IOcelotConfigurationCreator configCreator)
{
_configRepo = configRepo;
_configCreator = configCreator;
}
public async Task<Response> Set(FileConfiguration fileConfig)
{
var config = await _configCreator.Create(fileConfig);
if(!config.IsError)
{
_configRepo.AddOrReplace(config.Data);
}
return new ErrorResponse(config.Errors);
}
}
}

View File

@ -0,0 +1,11 @@
using System.Threading.Tasks;
using Ocelot.Configuration.File;
using Ocelot.Responses;
namespace Ocelot.Configuration.Setter
{
public interface IFileConfigurationSetter
{
Task<Response> Set(FileConfiguration config);
}
}

View File

@ -1,5 +1,8 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Setter;
using Ocelot.Services;
namespace Ocelot.Controllers
@ -8,17 +11,39 @@ namespace Ocelot.Controllers
[Route("configuration")]
public class FileConfigurationController : Controller
{
private readonly IGetFileConfiguration _getFileConfig;
private readonly IFileConfigurationProvider _configGetter;
private readonly IFileConfigurationSetter _configSetter;
public FileConfigurationController(IGetFileConfiguration getFileConfig)
public FileConfigurationController(IFileConfigurationProvider getFileConfig, IFileConfigurationSetter configSetter)
{
_getFileConfig = getFileConfig;
_configGetter = getFileConfig;
_configSetter = configSetter;
}
[HttpGet]
public IActionResult Get()
{
return new OkObjectResult(_getFileConfig.Invoke().Data);
var response = _configGetter.Get();
if(response.IsError)
{
return new BadRequestObjectResult(response.Errors);
}
return new OkObjectResult(response.Data);
}
[HttpPost]
public async Task<IActionResult> Post(FileConfiguration fileConfiguration)
{
var response = await _configSetter.Set(fileConfiguration);
if(response.IsError)
{
return new BadRequestObjectResult(response.Errors);
}
return new OkObjectResult(fileConfiguration);
}
}
}

View File

@ -1,14 +1,12 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Claims;
using CacheManager.Core;
using IdentityServer4.Models;
using IdentityServer4.Test;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Ocelot.Authentication.Handler.Creator;
using Ocelot.Authentication.Handler.Factory;
using Ocelot.Authorisation;
@ -46,7 +44,6 @@ namespace Ocelot.DependencyInjection
{
var cacheManagerOutputCache = CacheFactory.Build<HttpResponseMessage>("OcelotOutputCache", settings);
var ocelotCacheManager = new OcelotCacheManagerCache<HttpResponseMessage>(cacheManagerOutputCache);
services.AddSingleton<ICacheManager<HttpResponseMessage>>(cacheManagerOutputCache);
services.AddSingleton<IOcelotCache<HttpResponseMessage>>(ocelotCacheManager);
@ -55,11 +52,9 @@ namespace Ocelot.DependencyInjection
public static IServiceCollection AddOcelotFileConfiguration(this IServiceCollection services, IConfigurationRoot configurationRoot)
{
services.Configure<FileConfiguration>(configurationRoot);
services.AddSingleton<IOcelotConfigurationCreator, FileOcelotConfigurationCreator>();
services.AddSingleton<IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
services.AddSingleton<IConfigurationValidator, FileConfigurationValidator>();
return services;
}
@ -116,7 +111,7 @@ namespace Ocelot.DependencyInjection
.AddAuthorization()
.AddJsonFormatters();
services.AddLogging();
services.AddSingleton<IGetFileConfiguration, GetFileConfiguration>();
services.AddSingleton<IFileConfigurationProvider, Services.FileConfigurationProvider>();
services.AddSingleton<IQosProviderHouse, QosProviderHouse>();
services.AddSingleton<IQoSProviderFactory, QoSProviderFactory>();
services.AddSingleton<IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Configuration.Provider;
using Ocelot.DownstreamRouteFinder.UrlMatcher;
using Ocelot.Errors;
@ -22,9 +21,9 @@ namespace Ocelot.DownstreamRouteFinder.Finder
_urlPathPlaceholderNameAndValueFinder = urlPathPlaceholderNameAndValueFinder;
}
public async Task<Response<DownstreamRoute>> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod)
public Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod)
{
var configuration = await _configProvider.Get();
var configuration = _configProvider.Get();
var applicableReRoutes = configuration.Data.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod.Method, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase));

View File

@ -1,10 +1,9 @@
using System.Threading.Tasks;
using Ocelot.Responses;
using Ocelot.Responses;
namespace Ocelot.DownstreamRouteFinder.Finder
{
public interface IDownstreamRouteFinder
{
Task<Response<DownstreamRoute>> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod);
Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod);
}
}

View File

@ -1,6 +1,5 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.DownstreamRouteFinder.Finder;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
@ -34,7 +33,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
_logger.LogDebug("upstream url path is {upstreamUrlPath}", upstreamUrlPath);
var downstreamRoute = await _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.Request.Method);
var downstreamRoute = _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.Request.Method);
if (downstreamRoute.IsError)
{

View File

@ -1,6 +1,4 @@
using Ocelot.Configuration;
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
using Ocelot.Responses;
using Ocelot.Responses;
using Ocelot.Values;
namespace Ocelot.DownstreamUrlCreator

View File

@ -1,12 +1,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.Configuration;
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.Values;
namespace Ocelot.DownstreamUrlCreator.Middleware
{

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using Ocelot.Configuration;
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
using Ocelot.Errors;
using Ocelot.Responses;
using Ocelot.Values;

View File

@ -1,8 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;

View File

@ -1,7 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
using Ocelot.Middleware;

View File

@ -3,8 +3,6 @@ using Ocelot.Responses;
namespace Ocelot.Headers
{
using System;
public class RemoveOutputHeaders : IRemoveOutputHeaders
{
/// <summary>

View File

@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using Ocelot.Responses;
using Ocelot.Values;

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Ocelot.Responses;
namespace Ocelot.LoadBalancer.LoadBalancers

View File

@ -20,8 +20,11 @@ namespace Ocelot.Middleware
using System.Threading.Tasks;
using Authorisation.Middleware;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Ocelot.Configuration;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Provider;
using Ocelot.Configuration.Setter;
using Ocelot.LoadBalancer.Middleware;
public static class OcelotMiddlewareExtensions
@ -127,17 +130,27 @@ 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 config = await configProvider.Get();
var config = await configSetter.Set(fileConfig.Value);
//todo move this to config validators
if(config == null || config.Data == null || config.IsError)
if(config == null || config.IsError)
{
throw new Exception("Unable to start Ocelot: configuration was invalid");
throw new Exception("Unable to start Ocelot: configuration was not set up correctly.");
}
return config.Data;
var ocelotConfiguration = configProvider.Get();
if(ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError)
{
throw new Exception("Unable to start Ocelot: ocelot configuration was not returned by provider.");
}
return ocelotConfiguration.Data;
}
private static async Task CreateAdministrationArea(IApplicationBuilder builder)

View File

@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following

View File

@ -1,7 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
using Ocelot.Middleware;

View File

@ -5,6 +5,9 @@ namespace Ocelot.Responses
{
public class ErrorResponse : Response
{
public ErrorResponse(Error error) : base(new List<Error>{error})
{
}
public ErrorResponse(List<Error> errors) : base(errors)
{
}

View File

@ -6,9 +6,9 @@ using Ocelot.Responses;
namespace Ocelot.Services
{
public class GetFileConfiguration : IGetFileConfiguration
public class FileConfigurationProvider : IFileConfigurationProvider
{
public Response<FileConfiguration> Invoke()
public Response<FileConfiguration> Get()
{
var configFilePath = $"{AppContext.BaseDirectory}/configuration.json";
var json = File.ReadAllText(configFilePath);

View File

@ -3,8 +3,8 @@ using Ocelot.Responses;
namespace Ocelot.Services
{
public interface IGetFileConfiguration
public interface IFileConfigurationProvider
{
Response<FileConfiguration> Invoke();
Response<FileConfiguration> Get();
}
}

View File

@ -34,6 +34,7 @@
"runtimes": {
"win10-x64": {},
"osx.10.11-x64": {},
"osx.10.12-x64": {},
"win7-x64": {}
},
"frameworks": {