Added base url finder for when nothing set in Program.cs

This commit is contained in:
TomPallister
2017-02-25 17:02:36 +00:00
parent 07c334cc98
commit be24f9a9ca
5 changed files with 97 additions and 6 deletions

View File

@ -5,8 +5,6 @@ using System.Net.Http;
using CacheManager.Core;
using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Models;
using IdentityServer4.Test;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -32,12 +30,14 @@ using Ocelot.Infrastructure.Claims.Parser;
using Ocelot.Infrastructure.RequestData;
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.QueryStrings;
using Ocelot.Request.Builder;
using Ocelot.Requester;
using Ocelot.Requester.QoS;
using Ocelot.Responder;
using Ocelot.ServiceDiscovery;
using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider;
namespace Ocelot.DependencyInjection
{
@ -59,6 +59,7 @@ namespace Ocelot.DependencyInjection
services.AddSingleton<IOcelotConfigurationCreator, FileOcelotConfigurationCreator>();
services.AddSingleton<IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
services.AddSingleton<IConfigurationValidator, FileConfigurationValidator>();
services.AddSingleton<IBaseUrlFinder, BaseUrlFinder>();
var identityServerConfiguration = GetIdentityServerConfiguration();
@ -107,7 +108,7 @@ namespace Ocelot.DependencyInjection
services.AddLogging();
services.AddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
services.AddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();
services.AddSingleton<Configuration.Provider.IFileConfigurationProvider, Configuration.Provider.FileConfigurationProvider>();
services.AddSingleton<IFileConfigurationProvider, FileConfigurationProvider>();
services.AddSingleton<IQosProviderHouse, QosProviderHouse>();
services.AddSingleton<IQoSProviderFactory, QoSProviderFactory>();
services.AddSingleton<IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();

View File

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Hosting;
namespace Ocelot.Middleware
{
public class BaseUrlFinder : IBaseUrlFinder
{
private readonly IWebHostBuilder _webHostBuilder;
public BaseUrlFinder(IWebHostBuilder webHostBuilder)
{
_webHostBuilder = webHostBuilder;
}
public string Find()
{
var baseSchemeUrlAndPort = _webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey);
return string.IsNullOrEmpty(baseSchemeUrlAndPort) ? "http://localhost:5000" : baseSchemeUrlAndPort;
}
}
}

View File

@ -0,0 +1,7 @@
namespace Ocelot.Middleware
{
public interface IBaseUrlFinder
{
string Find();
}
}

View File

@ -162,9 +162,9 @@ namespace Ocelot.Middleware
if(!string.IsNullOrEmpty(configuration.AdministrationPath) && identityServerConfiguration != null)
{
var webHostBuilder = (IWebHostBuilder)builder.ApplicationServices.GetService(typeof(IWebHostBuilder));
var baseSchemeUrlAndPort = webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey);
var urlFinder = (IBaseUrlFinder)builder.ApplicationServices.GetService(typeof(IBaseUrlFinder));
var baseSchemeUrlAndPort = urlFinder.Find();
builder.Map(configuration.AdministrationPath, app =>
{
@ -186,6 +186,7 @@ namespace Ocelot.Middleware
});
}
}
private static void UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
{
if (middleware != null)