Merge branch 'release-3.1.5'

This commit is contained in:
Tom Pallister 2018-02-15 09:53:15 +00:00
commit 8811ea3736
9 changed files with 64 additions and 48 deletions

View File

@ -24,13 +24,21 @@ The following is a very basic configuration.json. It won't do anything but shoul
{ {
"ReRoutes": [], "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** **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), 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). It is important to call AddOcelotBaseUrl as Ocelot needs to know the URL that it is exposed to the outside world on. AddOcelot() (adds ocelot services), UseOcelot().Wait() (sets up all the Ocelot middleware).
.. code-block:: csharp .. 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.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddJsonFile("configuration.json") .AddJsonFile("configuration.json")
.AddEnvironmentVariables() .AddEnvironmentVariables();
.AddOcelotBaseUrl("http://localhost:5000");
}) })
.ConfigureServices(s => { .ConfigureServices(s => {
s.AddOcelot(); 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 .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.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("configuration.json") .AddJsonFile("configuration.json")
.AddEnvironmentVariables() .AddEnvironmentVariables();
.AddOcelotBaseUrl("http://localhost:5000");
Configuration = builder.Build(); Configuration = builder.Build();
} }

View File

@ -11,8 +11,10 @@ namespace Ocelot.Configuration.File
public string RequestIdKey { get; set; } public string RequestIdKey { get; set; }
public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;} public FileServiceDiscoveryProvider ServiceDiscoveryProvider { get;set; }
public FileRateLimitOptions RateLimitOptions { get; set; } public FileRateLimitOptions RateLimitOptions { get; set; }
public string BaseUrl { get ;set; }
} }
} }

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory; using Microsoft.Extensions.Configuration.Memory;
@ -6,6 +7,7 @@ namespace Ocelot.DependencyInjection
{ {
public static class ConfigurationBuilderExtensions public static class ConfigurationBuilderExtensions
{ {
[Obsolete("Please set BaseUrl in configuration.json GlobalConfiguration.BaseUrl")]
public static IConfigurationBuilder AddOcelotBaseUrl(this IConfigurationBuilder builder, string baseUrl) public static IConfigurationBuilder AddOcelotBaseUrl(this IConfigurationBuilder builder, string baseUrl)
{ {
var memorySource = new MemoryConfigurationSource(); var memorySource = new MemoryConfigurationSource();

View File

@ -13,9 +13,11 @@ namespace Ocelot.Middleware
public string Find() 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;
} }
} }
} }

View File

@ -152,7 +152,6 @@ namespace Ocelot.AcceptanceTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(_baseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(s => .ConfigureServices(s =>
@ -189,7 +188,6 @@ namespace Ocelot.AcceptanceTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(_baseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(s => .ConfigureServices(s =>
@ -227,7 +225,6 @@ namespace Ocelot.AcceptanceTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(_baseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(s => .ConfigureServices(s =>
@ -256,7 +253,6 @@ namespace Ocelot.AcceptanceTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(_baseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(s => .ConfigureServices(s =>

View File

@ -74,13 +74,19 @@ namespace Ocelot.IntegrationTests
} }
[Fact] [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(); _httpClient = new HttpClient();
_ocelotBaseUrl = "http://localhost:5011"; _ocelotBaseUrl = "http://localhost:5011";
_httpClient.BaseAddress = new Uri(_ocelotBaseUrl); _httpClient.BaseAddress = new Uri(_ocelotBaseUrl);
var configuration = new FileConfiguration(); var configuration = new FileConfiguration
{
GlobalConfiguration = new FileGlobalConfiguration
{
BaseUrl = _ocelotBaseUrl
}
};
this.Given(x => GivenThereIsAConfiguration(configuration)) this.Given(x => GivenThereIsAConfiguration(configuration))
.And(x => GivenOcelotIsRunningWithNoWebHostBuilder(_ocelotBaseUrl)) .And(x => GivenOcelotIsRunningWithNoWebHostBuilder(_ocelotBaseUrl))
@ -457,7 +463,6 @@ namespace Ocelot.IntegrationTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(baseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(x => .ConfigureServices(x =>
@ -610,7 +615,6 @@ namespace Ocelot.IntegrationTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(_ocelotBaseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(x => .ConfigureServices(x =>
@ -651,7 +655,6 @@ namespace Ocelot.IntegrationTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(baseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(x => { .ConfigureServices(x => {

View File

@ -109,7 +109,6 @@ namespace Ocelot.IntegrationTests
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddJsonFile("configuration.json"); config.AddJsonFile("configuration.json");
config.AddOcelotBaseUrl(_ocelotBaseUrl);
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
}) })
.ConfigureServices(x => .ConfigureServices(x =>

View File

@ -22,8 +22,7 @@ namespace Ocelot.ManualTest
.AddJsonFile("appsettings.json", true, true) .AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddJsonFile("configuration.json") .AddJsonFile("configuration.json")
.AddEnvironmentVariables() .AddEnvironmentVariables();
.AddOcelotBaseUrl("http://localhost:5000");
}) })
.ConfigureServices(s => { .ConfigureServices(s => {

View File

@ -15,46 +15,62 @@ namespace Ocelot.UnitTests.Middleware
{ {
public class BaseUrlFinderTests public class BaseUrlFinderTests
{ {
private readonly BaseUrlFinder _baseUrlFinder; private BaseUrlFinder _baseUrlFinder;
private readonly Mock<IConfiguration> _config; private IConfiguration _config;
private List<KeyValuePair<string, string>> _data;
private string _result; private string _result;
public BaseUrlFinderTests() public BaseUrlFinderTests()
{ {
_config = new Mock<IConfiguration>(); _data = new List<KeyValuePair<string,string>>();
_baseUrlFinder = new BaseUrlFinder(_config.Object);
} }
[Fact] [Fact]
public void should_use_default_base_url() public void should_use_default_base_url()
{ {
this.Given(x => GivenTheConfigBaseUrlIs("")) this.When(x => WhenIFindTheUrl())
.And(x => GivenTheConfigBaseUrlIs("")) .Then(x => ThenTheUrlIs("http://localhost:5000"))
.When(x => WhenIFindTheUrl()) .BDDfy();
.Then(x => ThenTheUrlIs("http://localhost:5000"))
.BDDfy();
} }
[Fact] [Fact]
public void should_use_file_config_base_url() public void should_use_memory_config_base_url()
{ {
this.Given(x => GivenTheConfigBaseUrlIs("http://localhost:7000")) this.Given(x => GivenTheMemoryBaseUrlIs("http://baseurlfromconfig.com:5181"))
.And(x => GivenTheConfigBaseUrlIs("http://baseurlfromconfig.com:5181"))
.When(x => WhenIFindTheUrl()) .When(x => WhenIFindTheUrl())
.Then(x => ThenTheUrlIs("http://baseurlfromconfig.com:5181")) .Then(x => ThenTheUrlIs("http://baseurlfromconfig.com:5181"))
.BDDfy(); .BDDfy();
} }
private void GivenTheConfigBaseUrlIs(string configValue) [Fact]
public void should_use_file_config_base_url()
{ {
var configSection = new ConfigurationSection(new ConfigurationRoot(new List<IConfigurationProvider>{new MemoryConfigurationProvider(new MemoryConfigurationSource())}), ""); this.Given(x => GivenTheMemoryBaseUrlIs("http://localhost:7000"))
configSection.Value = configValue; .And(x => GivenTheFileBaseUrlIs("http://baseurlfromconfig.com:5181"))
_config.Setup(x => x.GetSection(It.IsAny<string>())).Returns(configSection); .When(x => WhenIFindTheUrl())
.Then(x => ThenTheUrlIs("http://baseurlfromconfig.com:5181"))
.BDDfy();
}
private void GivenTheMemoryBaseUrlIs(string configValue)
{
_data.Add(new KeyValuePair<string, string>("BaseUrl", configValue));
}
private void GivenTheFileBaseUrlIs(string configValue)
{
_data.Add(new KeyValuePair<string, string>("GlobalConfiguration:BaseUrl", configValue));
} }
private void WhenIFindTheUrl() private void WhenIFindTheUrl()
{ {
var source = new MemoryConfigurationSource();
source.InitialData = _data;
var provider = new MemoryConfigurationProvider(source);
_config = new ConfigurationRoot(new List<IConfigurationProvider>() {
provider
});
_baseUrlFinder = new BaseUrlFinder(_config);
_result = _baseUrlFinder.Find(); _result = _baseUrlFinder.Find();
} }