mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 14:02:49 +08:00
allowing people to inject functions as custom middleware
This commit is contained in:
parent
b8951c4698
commit
ab5d7fa33d
@ -7,12 +7,12 @@ namespace Ocelot.Configuration.Provider
|
||||
/// <summary>
|
||||
/// Register as singleton
|
||||
/// </summary>
|
||||
public class YamlOcelotConfigurationProvider : IOcelotConfigurationProvider
|
||||
public class OcelotConfigurationProvider : IOcelotConfigurationProvider
|
||||
{
|
||||
private readonly IOcelotConfigurationRepository _repo;
|
||||
private readonly IOcelotConfigurationCreator _creator;
|
||||
|
||||
public YamlOcelotConfigurationProvider(IOcelotConfigurationRepository repo,
|
||||
public OcelotConfigurationProvider(IOcelotConfigurationRepository repo,
|
||||
IOcelotConfigurationCreator creator)
|
||||
{
|
||||
_repo = repo;
|
||||
@ -21,28 +21,28 @@ namespace Ocelot.Configuration.Provider
|
||||
|
||||
public Response<IOcelotConfiguration> Get()
|
||||
{
|
||||
var config = _repo.Get();
|
||||
var repoConfig = _repo.Get();
|
||||
|
||||
if (config.IsError)
|
||||
if (repoConfig.IsError)
|
||||
{
|
||||
return new ErrorResponse<IOcelotConfiguration>(config.Errors);
|
||||
return new ErrorResponse<IOcelotConfiguration>(repoConfig.Errors);
|
||||
}
|
||||
|
||||
if (config.Data == null)
|
||||
if (repoConfig.Data == null)
|
||||
{
|
||||
var configuration = _creator.Create();
|
||||
var creatorConfig = _creator.Create();
|
||||
|
||||
if (configuration.IsError)
|
||||
if (creatorConfig.IsError)
|
||||
{
|
||||
return new ErrorResponse<IOcelotConfiguration>(configuration.Errors);
|
||||
return new ErrorResponse<IOcelotConfiguration>(creatorConfig.Errors);
|
||||
}
|
||||
|
||||
_repo.AddOrReplace(configuration.Data);
|
||||
_repo.AddOrReplace(creatorConfig.Data);
|
||||
|
||||
return new OkResponse<IOcelotConfiguration>(configuration.Data);
|
||||
return new OkResponse<IOcelotConfiguration>(creatorConfig.Data);
|
||||
}
|
||||
|
||||
return new OkResponse<IOcelotConfiguration>(config.Data);
|
||||
return new OkResponse<IOcelotConfiguration>(repoConfig.Data);
|
||||
}
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ namespace Ocelot.DependencyInjection
|
||||
|
||||
// ocelot services.
|
||||
services.AddSingleton<IOcelotConfigurationCreator, YamlOcelotConfigurationCreator>();
|
||||
services.AddSingleton<IOcelotConfigurationProvider, YamlOcelotConfigurationProvider>();
|
||||
services.AddSingleton<IOcelotConfigurationProvider, OcelotConfigurationProvider>();
|
||||
services.AddSingleton<IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
|
||||
services.AddSingleton<IClaimToThingConfigurationParser, ClaimToThingConfigurationParser>();
|
||||
services.AddSingleton<IConfigurationValidator, ConfigurationValidator>();
|
||||
|
27
src/Ocelot/Middleware/OcelotMiddlewareConfiguration.cs
Normal file
27
src/Ocelot/Middleware/OcelotMiddlewareConfiguration.cs
Normal file
@ -0,0 +1,27 @@
|
||||
namespace Ocelot.Middleware
|
||||
{
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
public class OcelotMiddlewareConfiguration
|
||||
{
|
||||
public Func<HttpContext, Func<Task>, Task> PreHttpResponderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostHttpResponderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreDownstreamRouteFinderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostDownstreamRouteFinderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreAuthenticationMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostAuthenticationMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreClaimsBuilderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostClaimsBuilderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreAuthorisationMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostAuthorisationMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreHttpRequestHeadersBuilderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostHttpRequestHeadersBuilderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreDownstreamUrlCreatorMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostDownstreamUrlCreatorMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreHttpRequestBuilderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PostHttpRequestBuilderMiddleware { get; set; }
|
||||
public Func<HttpContext, Func<Task>, Task> PreHttpRequesterMiddleware { get; set; }
|
||||
}
|
||||
}
|
@ -9,8 +9,11 @@ using Ocelot.Responder.Middleware;
|
||||
|
||||
namespace Ocelot.Middleware
|
||||
{
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Authorisation.Middleware;
|
||||
using ClaimsBuilder.Middleware;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
public static class OcelotMiddlewareExtensions
|
||||
{
|
||||
@ -36,5 +39,70 @@ namespace Ocelot.Middleware
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseOcelot(this IApplicationBuilder builder, OcelotMiddlewareConfiguration middlewareConfiguration)
|
||||
{
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreHttpResponderMiddleware);
|
||||
|
||||
builder.UseHttpResponderMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostHttpResponderMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreDownstreamRouteFinderMiddleware);
|
||||
|
||||
builder.UseDownstreamRouteFinderMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostDownstreamRouteFinderMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreAuthenticationMiddleware);
|
||||
|
||||
builder.UseAuthenticationMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostAuthenticationMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreClaimsBuilderMiddleware);
|
||||
|
||||
builder.UseClaimsBuilderMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostClaimsBuilderMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreAuthorisationMiddleware);
|
||||
|
||||
builder.UseAuthorisationMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostAuthorisationMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreHttpRequestHeadersBuilderMiddleware);
|
||||
|
||||
builder.UseHttpRequestHeadersBuilderMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostHttpRequestHeadersBuilderMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreDownstreamUrlCreatorMiddleware);
|
||||
|
||||
builder.UseDownstreamUrlCreatorMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostDownstreamUrlCreatorMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreHttpRequestBuilderMiddleware);
|
||||
|
||||
builder.UseHttpRequestBuilderMiddleware();
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PostHttpRequestBuilderMiddleware);
|
||||
|
||||
builder.UseIfNotNull(middlewareConfiguration.PreHttpRequesterMiddleware);
|
||||
|
||||
builder.UseHttpRequesterMiddleware();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static void UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
|
||||
{
|
||||
if (middleware != null)
|
||||
{
|
||||
builder.Use(middleware);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
201
test/Ocelot.AcceptanceTests/CustomMiddlewareTests.cs
Normal file
201
test/Ocelot.AcceptanceTests/CustomMiddlewareTests.cs
Normal file
@ -0,0 +1,201 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Ocelot.Configuration.Yaml;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Middleware;
|
||||
using ScopedData;
|
||||
|
||||
public class CustomMiddlewareTests : IDisposable
|
||||
{
|
||||
private TestServer _server;
|
||||
private HttpClient _client;
|
||||
private HttpResponseMessage _response;
|
||||
private readonly string _configurationPath;
|
||||
private StringContent _postContent;
|
||||
private IWebHost _builder;
|
||||
|
||||
// Sadly we need to change this when we update the netcoreapp version to make the test update the config correctly
|
||||
private double _netCoreAppVersion = 1.4;
|
||||
|
||||
public CustomMiddlewareTests()
|
||||
{
|
||||
_configurationPath = $"configuration.yaml";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void response_should_come_from_pre_http_responder_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreHttpResponderMiddleware = async (ctx, next) =>
|
||||
{
|
||||
await ctx.Response.WriteAsync("PreHttpResponderMiddleware");
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://localhost:41879/",
|
||||
UpstreamTemplate = "/",
|
||||
UpstreamHttpMethod = "Get",
|
||||
}
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheApiGatewayIsRunning(configuration))
|
||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheResponseBodyShouldBe("PreHttpResponderMiddleware"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void response_should_come_from_pre_http_requester_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreHttpRequesterMiddleware = async (ctx, next) =>
|
||||
{
|
||||
var service = ctx.RequestServices.GetService<IScopedRequestDataRepository>();
|
||||
service.Add("Response",
|
||||
new HttpResponseMessage {Content = new StringContent("PreHttpRequesterMiddleware")});
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://localhost:41879/",
|
||||
UpstreamTemplate = "/",
|
||||
UpstreamHttpMethod = "Get",
|
||||
}
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheApiGatewayIsRunning(configuration))
|
||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheResponseBodyShouldBe("PreHttpRequesterMiddleware"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This is annoying cos it should be in the constructor but we need to set up the yaml file before calling startup so its a step.
|
||||
/// </summary>
|
||||
private void GivenTheApiGatewayIsRunning(OcelotMiddlewareConfiguration ocelotMiddlewareConfig)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddYamlFile("configuration.yaml")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
var configuration = builder.Build();
|
||||
|
||||
_server = new TestServer(new WebHostBuilder()
|
||||
.UseConfiguration(configuration)
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelotYamlConfiguration(configuration);
|
||||
s.AddOcelot();
|
||||
})
|
||||
.ConfigureLogging(l =>
|
||||
{
|
||||
l.AddConsole(configuration.GetSection("Logging"));
|
||||
l.AddDebug();
|
||||
})
|
||||
.Configure(a =>
|
||||
{
|
||||
a.UseOcelot(ocelotMiddlewareConfig);
|
||||
}));
|
||||
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(YamlConfiguration yamlConfiguration)
|
||||
{
|
||||
var serializer = new Serializer();
|
||||
|
||||
if (File.Exists(_configurationPath))
|
||||
{
|
||||
File.Delete(_configurationPath);
|
||||
}
|
||||
|
||||
using (TextWriter writer = File.CreateText(_configurationPath))
|
||||
{
|
||||
serializer.Serialize(writer, yamlConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _client.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
||||
{
|
||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
||||
}
|
||||
|
||||
private void ThenTheResponseBodyShouldBe(string expectedBody)
|
||||
{
|
||||
_response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_client.Dispose();
|
||||
_server.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +1,7 @@
|
||||
ReRoutes:
|
||||
- DownstreamTemplate: http://localhost:51876/
|
||||
- DownstreamTemplate: http://localhost:41879/
|
||||
UpstreamTemplate: /
|
||||
UpstreamHttpMethod: Post
|
||||
AuthenticationOptions:
|
||||
Provider: IdentityServer
|
||||
ProviderRootUrl: http://localhost:51888
|
||||
ScopeName: api
|
||||
AdditionalScopes: []
|
||||
ScopeSecret: secret
|
||||
ProxyRequestHeaders:
|
||||
- CustomerId: Claims[CustomerId]
|
||||
|
||||
UpstreamHttpMethod: Get
|
||||
AddHeadersToRequest: {}
|
||||
AddClaimsToRequest: {}
|
||||
RouteClaimsRequirement: {}
|
||||
|
@ -4,7 +4,7 @@
|
||||
"buildOptions": {
|
||||
"copyToOutput": {
|
||||
"include": [
|
||||
"configuration.yaml"
|
||||
"middlewareConfiguration.yaml"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
@ -39,7 +39,7 @@
|
||||
"preserveCompilationContext": true,
|
||||
"copyToOutput": {
|
||||
"include": [
|
||||
"configuration.yaml"
|
||||
"middlewareConfiguration.yaml"
|
||||
]
|
||||
}
|
||||
},
|
||||
@ -57,7 +57,7 @@
|
||||
"Areas/**/Views",
|
||||
"appsettings.json",
|
||||
"web.config",
|
||||
"configuration.yaml"
|
||||
"middlewareConfiguration.yaml"
|
||||
]
|
||||
},
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<middlewareConfiguration>
|
||||
|
||||
<!--
|
||||
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
|
||||
@ -11,4 +11,4 @@
|
||||
</handlers>
|
||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
|
||||
</system.webServer>
|
||||
</configuration>
|
||||
</middlewareConfiguration>
|
||||
|
@ -26,7 +26,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
_creator = new Mock<IOcelotConfigurationCreator>();
|
||||
_configurationRepository = new Mock<IOcelotConfigurationRepository>();
|
||||
_ocelotConfigurationProvider = new YamlOcelotConfigurationProvider(_configurationRepository.Object, _creator.Object);
|
||||
_ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object, _creator.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
Loading…
x
Reference in New Issue
Block a user