diff --git a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs index 380e193f..8025f59d 100644 --- a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs @@ -85,7 +85,7 @@ namespace Ocelot.Configuration.Creator reRoutes.Add(ocelotReRoute); } - return new OcelotConfiguration(reRoutes); + return new OcelotConfiguration(reRoutes, _options.Value.GlobalConfiguration.AdministrationPath); } private async Task SetUpReRoute(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration) diff --git a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs index f414bc83..fd0f41a9 100644 --- a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs +++ b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs @@ -8,5 +8,6 @@ } public string RequestIdKey { get; set; } public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;} + public string AdministrationPath {get;set;} } } diff --git a/src/Ocelot/Configuration/IOcelotConfiguration.cs b/src/Ocelot/Configuration/IOcelotConfiguration.cs index 8359a2e1..566e2f91 100644 --- a/src/Ocelot/Configuration/IOcelotConfiguration.cs +++ b/src/Ocelot/Configuration/IOcelotConfiguration.cs @@ -5,5 +5,6 @@ namespace Ocelot.Configuration public interface IOcelotConfiguration { List ReRoutes { get; } + string AdministrationPath {get;} } } \ No newline at end of file diff --git a/src/Ocelot/Configuration/OcelotConfiguration.cs b/src/Ocelot/Configuration/OcelotConfiguration.cs index 3b0858eb..5655b3c2 100644 --- a/src/Ocelot/Configuration/OcelotConfiguration.cs +++ b/src/Ocelot/Configuration/OcelotConfiguration.cs @@ -4,11 +4,13 @@ namespace Ocelot.Configuration { public class OcelotConfiguration : IOcelotConfiguration { - public OcelotConfiguration(List reRoutes) + public OcelotConfiguration(List reRoutes, string administrationPath) { ReRoutes = reRoutes; + AdministrationPath = administrationPath; } public List ReRoutes { get; } + public string AdministrationPath {get;} } } \ No newline at end of file diff --git a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs index afc080a0..5ba19ca5 100644 --- a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs +++ b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs @@ -18,6 +18,7 @@ namespace Ocelot.Middleware using System.Threading.Tasks; using Authorisation.Middleware; using Microsoft.AspNetCore.Http; + using Ocelot.Configuration; using Ocelot.Configuration.Provider; using Ocelot.LoadBalancer.Middleware; @@ -30,9 +31,9 @@ namespace Ocelot.Middleware /// public static IApplicationBuilder UseOcelot(this IApplicationBuilder builder) { - CreateConfiguration(builder); + var configuration = CreateConfiguration(builder).Result; - builder.Map("/admin", x => + builder.Map(configuration.AdministrationPath, x => { x.UseMvc(); }); @@ -131,16 +132,19 @@ namespace Ocelot.Middleware return builder; } - private static void CreateConfiguration(IApplicationBuilder builder) + private static async Task CreateConfiguration(IApplicationBuilder builder) { var configProvider = (IOcelotConfigurationProvider)builder.ApplicationServices.GetService(typeof(IOcelotConfigurationProvider)); - var config = configProvider.Get(); + var config = await configProvider.Get(); - if(config == null) + //todo move this to config validators + if(config == null || config.Data == null || config.IsError) { - throw new Exception("Unable to start Ocelot: configuration was null"); + throw new Exception("Unable to start Ocelot: configuration was invalid"); } + + return config.Data; } private static void UseIfNotNull(this IApplicationBuilder builder, Func, Task> middleware) diff --git a/test/Ocelot.ManualTest/configuration.json b/test/Ocelot.ManualTest/configuration.json index fc0b6567..f6f3c787 100644 --- a/test/Ocelot.ManualTest/configuration.json +++ b/test/Ocelot.ManualTest/configuration.json @@ -300,6 +300,7 @@ ], "GlobalConfiguration": { - "RequestIdKey": "OcRequestId" + "RequestIdKey": "OcRequestId", + "AdministrationPath": "/admin" } } \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs index 98e01293..2573e32f 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs @@ -29,9 +29,9 @@ namespace Ocelot.UnitTests.Configuration [Fact] public void should_get_config() { - this.Given(x => x.GivenTheRepoReturns(new OkResponse(new OcelotConfiguration(new List())))) + this.Given(x => x.GivenTheRepoReturns(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) .When(x => x.WhenIGetTheConfig()) - .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List())))) + .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) .BDDfy(); } @@ -39,9 +39,9 @@ namespace Ocelot.UnitTests.Configuration public void should_create_config_if_it_doesnt_exist() { this.Given(x => x.GivenTheRepoReturns(new OkResponse(null))) - .And(x => x.GivenTheCreatorReturns(new OkResponse(new OcelotConfiguration(new List())))) + .And(x => x.GivenTheCreatorReturns(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) .When(x => x.WhenIGetTheConfig()) - .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List())))) + .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) .BDDfy(); } diff --git a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs index 9f437204..8e0e66d3 100644 --- a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs @@ -82,6 +82,14 @@ namespace Ocelot.UnitTests.Configuration _downstreamTemplatePath = downstreamTemplatePath; } + public string AdministrationPath + { + get + { + throw new NotImplementedException(); + } + } + public List ReRoutes => new List { new ReRouteBuilder() diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs index 2fb8f28f..0d272c63 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs @@ -48,7 +48,8 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .WithUpstreamHttpMethod("Get") .WithUpstreamTemplatePattern("someUpstreamPath") .Build() - })) + }, string.Empty + )) .And(x => x.GivenTheUrlMatcherReturns(new OkResponse(new UrlMatch(true)))) .And(x => x.GivenTheUpstreamHttpMethodIs("Get")) .When(x => x.WhenICallTheFinder()) @@ -80,7 +81,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .WithUpstreamHttpMethod("Get") .WithUpstreamTemplatePattern("someUpstreamPath") .Build() - } + }, string.Empty )) .And(x => x.GivenTheUrlMatcherReturns(new OkResponse(new UrlMatch(true)))) .And(x => x.GivenTheUpstreamHttpMethodIs("Get")) @@ -118,7 +119,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .WithUpstreamHttpMethod("Post") .WithUpstreamTemplatePattern("") .Build() - } + }, string.Empty )) .And(x => x.GivenTheUrlMatcherReturns(new OkResponse(new UrlMatch(true)))) .And(x => x.GivenTheUpstreamHttpMethodIs("Post")) @@ -145,7 +146,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .WithUpstreamHttpMethod("Get") .WithUpstreamTemplatePattern("somePath") .Build(), - } + }, string.Empty )) .And(x => x.GivenTheUrlMatcherReturns(new OkResponse(new UrlMatch(false)))) .And(x => x.GivenTheUpstreamHttpMethodIs("Get")) @@ -193,12 +194,12 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .Returns(_match); } - private void GivenTheConfigurationIs(List reRoutesConfig) + private void GivenTheConfigurationIs(List reRoutesConfig, string adminPath) { _reRoutesConfig = reRoutesConfig; _mockConfig .Setup(x => x.Get()) - .ReturnsAsync(new OkResponse(new OcelotConfiguration(_reRoutesConfig))); + .ReturnsAsync(new OkResponse(new OcelotConfiguration(_reRoutesConfig, adminPath))); } private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)