diff --git a/docs/introduction/gettingstarted.rst b/docs/introduction/gettingstarted.rst index 840505f1..8f635fdc 100644 --- a/docs/introduction/gettingstarted.rst +++ b/docs/introduction/gettingstarted.rst @@ -24,13 +24,21 @@ The following is a very basic configuration.json. It won't do anything but shoul { "ReRoutes": [], - "GlobalConfiguration": {} + "GlobalConfiguration": { + "BaseUrl": "https://api.mybusiness.com" + } } +The most important thing to note here is BaseUrl. Ocelot needs to know the URL it is running under +in order to do Header find & replace and for certain administration configurations. When setting this URL it should be the external URL that clients will see Ocelot running on e.g. If you are running containers Ocelot might run on the url http://123.12.1.1:6543 but has something like nginx in front of it responding on https://api.mybusiness.com. In this case the Ocelot base url should be https://api.mybusiness.com. + +If for some reason you are using containers and do want Ocelot to respond to client on http://123.12.1.1:6543 +then you can do this but if you are deploying multiple Ocelot's you will probably want to pass this on the command line in some kind of script. Hopefully whatever scheduler you are using can pass the IP. + **Program** -Then in your Program.cs you will want to have the following. The main things to note are AddOcelotBaseUrl("http://localhost:5000") (adds the url this instance of Ocelot will run under), -AddOcelot() (adds ocelot services), UseOcelot().Wait() (sets up all the Ocelot middleware). It is important to call AddOcelotBaseUrl as Ocelot needs to know the URL that it is exposed to the outside world on. +Then in your Program.cs you will want to have the following. The main things to note are +AddOcelot() (adds ocelot services), UseOcelot().Wait() (sets up all the Ocelot middleware). .. code-block:: csharp @@ -48,8 +56,7 @@ AddOcelot() (adds ocelot services), UseOcelot().Wait() (sets up all the Ocelot m .AddJsonFile("appsettings.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile("configuration.json") - .AddEnvironmentVariables() - .AddOcelotBaseUrl("http://localhost:5000"); + .AddEnvironmentVariables(); }) .ConfigureServices(s => { s.AddOcelot(); @@ -68,15 +75,6 @@ AddOcelot() (adds ocelot services), UseOcelot().Wait() (sets up all the Ocelot m } } -AddOcelotBaseUrl -^^^^^^^^^^^^^^^^ - -The most important thing to note here is AddOcelotBaseUrl. Ocelot needs to know the URL it is running under -in order to do Header find & replace and for certain administration configurations. When setting this URL it should be the external URL that clients will see Ocelot running on e.g. If you are running containers Ocelot might run on the url http://123.12.1.1:6543 but has something like nginx in front of it responding on https://api.mybusiness.com. In this case the Ocelot base url should be https://api.mybusiness.com. - -If for some reason you are using containers and do want Ocelot to respond to client on http://123.12.1.1:6543 -then you can do this but if you are deploying multiple Ocelot's you will probably want to pass this on the command line in some kind of script. Hopefully whatever scheduler you are using can pass the IP. - .NET Core 1.0 ^^^^^^^^^^^^^ @@ -138,8 +136,7 @@ An example startup using a json file for configuration can be seen below. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddJsonFile("configuration.json") - .AddEnvironmentVariables() - .AddOcelotBaseUrl("http://localhost:5000"); + .AddEnvironmentVariables(); Configuration = builder.Build(); } diff --git a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs index 5788d921..adbbdce1 100644 --- a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs +++ b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs @@ -11,8 +11,10 @@ namespace Ocelot.Configuration.File public string RequestIdKey { get; set; } - public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;} + public FileServiceDiscoveryProvider ServiceDiscoveryProvider { get;set; } public FileRateLimitOptions RateLimitOptions { get; set; } + + public string BaseUrl { get ;set; } } } diff --git a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs index c525f67f..56360ad5 100644 --- a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs +++ b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Memory; @@ -6,6 +7,7 @@ namespace Ocelot.DependencyInjection { public static class ConfigurationBuilderExtensions { + [Obsolete("Please set BaseUrl in configuration.json GlobalConfiguration.BaseUrl")] public static IConfigurationBuilder AddOcelotBaseUrl(this IConfigurationBuilder builder, string baseUrl) { var memorySource = new MemoryConfigurationSource(); diff --git a/src/Ocelot/Middleware/BaseUrlFinder.cs b/src/Ocelot/Middleware/BaseUrlFinder.cs index de24e682..fe3acea7 100644 --- a/src/Ocelot/Middleware/BaseUrlFinder.cs +++ b/src/Ocelot/Middleware/BaseUrlFinder.cs @@ -13,9 +13,11 @@ namespace Ocelot.Middleware public string Find() { - var baseUrl = _config.GetValue("BaseUrl", ""); + //tries to get base url out of file... + var baseUrl = _config.GetValue("GlobalConfiguration:BaseUrl", ""); - return string.IsNullOrEmpty(baseUrl) ? "http://localhost:5000" : baseUrl; + //falls back to memory config then finally default.. + return string.IsNullOrEmpty(baseUrl) ? _config.GetValue("BaseUrl", "http://localhost:5000") : baseUrl; } } } diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index b33778e7..5aabc750 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -152,7 +152,6 @@ namespace Ocelot.AcceptanceTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(_baseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(s => @@ -189,7 +188,6 @@ namespace Ocelot.AcceptanceTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(_baseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(s => @@ -227,7 +225,6 @@ namespace Ocelot.AcceptanceTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(_baseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(s => @@ -256,7 +253,6 @@ namespace Ocelot.AcceptanceTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(_baseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(s => diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index cddd4368..3b2f6a35 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -74,13 +74,19 @@ namespace Ocelot.IntegrationTests } [Fact] - public void should_return_response_200_with_call_re_routes_controller_using_base_url_added_in_memory_with_no_webhostbuilder_registered() + public void should_return_response_200_with_call_re_routes_controller_using_base_url_added_in_file_config() { _httpClient = new HttpClient(); _ocelotBaseUrl = "http://localhost:5011"; _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); - var configuration = new FileConfiguration(); + var configuration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + BaseUrl = _ocelotBaseUrl + } + }; this.Given(x => GivenThereIsAConfiguration(configuration)) .And(x => GivenOcelotIsRunningWithNoWebHostBuilder(_ocelotBaseUrl)) @@ -457,7 +463,6 @@ namespace Ocelot.IntegrationTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(baseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(x => @@ -610,7 +615,6 @@ namespace Ocelot.IntegrationTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(_ocelotBaseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(x => @@ -651,7 +655,6 @@ namespace Ocelot.IntegrationTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(baseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(x => { diff --git a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs index 5b360358..f8533a04 100644 --- a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs +++ b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs @@ -109,7 +109,6 @@ namespace Ocelot.IntegrationTests config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddJsonFile("configuration.json"); - config.AddOcelotBaseUrl(_ocelotBaseUrl); config.AddEnvironmentVariables(); }) .ConfigureServices(x => diff --git a/test/Ocelot.ManualTest/Program.cs b/test/Ocelot.ManualTest/Program.cs index a7c5bc61..8cb4e917 100644 --- a/test/Ocelot.ManualTest/Program.cs +++ b/test/Ocelot.ManualTest/Program.cs @@ -22,8 +22,7 @@ namespace Ocelot.ManualTest .AddJsonFile("appsettings.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile("configuration.json") - .AddEnvironmentVariables() - .AddOcelotBaseUrl("http://localhost:5000"); + .AddEnvironmentVariables(); }) .ConfigureServices(s => { diff --git a/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs b/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs index f22107a5..47e00ab9 100644 --- a/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs +++ b/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs @@ -15,46 +15,62 @@ namespace Ocelot.UnitTests.Middleware { public class BaseUrlFinderTests { - private readonly BaseUrlFinder _baseUrlFinder; - private readonly Mock _config; - + private BaseUrlFinder _baseUrlFinder; + private IConfiguration _config; + private List> _data; private string _result; public BaseUrlFinderTests() { - _config = new Mock(); - _baseUrlFinder = new BaseUrlFinder(_config.Object); + _data = new List>(); } [Fact] public void should_use_default_base_url() { - this.Given(x => GivenTheConfigBaseUrlIs("")) - .And(x => GivenTheConfigBaseUrlIs("")) - .When(x => WhenIFindTheUrl()) - .Then(x => ThenTheUrlIs("http://localhost:5000")) - .BDDfy(); + this.When(x => WhenIFindTheUrl()) + .Then(x => ThenTheUrlIs("http://localhost:5000")) + .BDDfy(); } [Fact] - public void should_use_file_config_base_url() + public void should_use_memory_config_base_url() { - this.Given(x => GivenTheConfigBaseUrlIs("http://localhost:7000")) - .And(x => GivenTheConfigBaseUrlIs("http://baseurlfromconfig.com:5181")) + this.Given(x => GivenTheMemoryBaseUrlIs("http://baseurlfromconfig.com:5181")) .When(x => WhenIFindTheUrl()) .Then(x => ThenTheUrlIs("http://baseurlfromconfig.com:5181")) .BDDfy(); } - private void GivenTheConfigBaseUrlIs(string configValue) + [Fact] + public void should_use_file_config_base_url() { - var configSection = new ConfigurationSection(new ConfigurationRoot(new List{new MemoryConfigurationProvider(new MemoryConfigurationSource())}), ""); - configSection.Value = configValue; - _config.Setup(x => x.GetSection(It.IsAny())).Returns(configSection); + this.Given(x => GivenTheMemoryBaseUrlIs("http://localhost:7000")) + .And(x => GivenTheFileBaseUrlIs("http://baseurlfromconfig.com:5181")) + .When(x => WhenIFindTheUrl()) + .Then(x => ThenTheUrlIs("http://baseurlfromconfig.com:5181")) + .BDDfy(); + } + + private void GivenTheMemoryBaseUrlIs(string configValue) + { + _data.Add(new KeyValuePair("BaseUrl", configValue)); + } + + private void GivenTheFileBaseUrlIs(string configValue) + { + _data.Add(new KeyValuePair("GlobalConfiguration:BaseUrl", configValue)); } private void WhenIFindTheUrl() { + var source = new MemoryConfigurationSource(); + source.InitialData = _data; + var provider = new MemoryConfigurationProvider(source); + _config = new ConfigurationRoot(new List() { + provider + }); + _baseUrlFinder = new BaseUrlFinder(_config); _result = _baseUrlFinder.Find(); }