mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 02:58:15 +08:00
Added base url finder for when nothing set in Program.cs
This commit is contained in:
@ -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>();
|
||||
|
21
src/Ocelot/Middleware/BaseUrlFinder.cs
Normal file
21
src/Ocelot/Middleware/BaseUrlFinder.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
7
src/Ocelot/Middleware/IBaseUrlFinder.cs
Normal file
7
src/Ocelot/Middleware/IBaseUrlFinder.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Ocelot.Middleware
|
||||
{
|
||||
public interface IBaseUrlFinder
|
||||
{
|
||||
string Find();
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Reference in New Issue
Block a user