diff --git a/README.md b/README.md index 34e90b5..bcfc423 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,15 @@ Direct via `http://ocelotprojecturl:port/swagger` provides documentation for dow opt.PathToSwaggerGenerator = "/swagger/docs"; }) ``` + You can optionally include headers that your Ocelot Gateway will send when requesting a swagger endpoint. This can be especially useful if your downstream microservices require contents from a header to authenticate. + ```CSharp + app.UseSwaggerForOcelotUI(Configuration, opt => { + opts.DownstreamSwaggerHeaders = new[] + { + new KeyValuePair("Auth-Key", "AuthValue"), + }; + }) + ``` 6. Show your microservices interactive documentation. > `http://ocelotserviceurl/swagger` diff --git a/demo/ApiGateway/Startup.cs b/demo/ApiGateway/Startup.cs index 94cbd1b..d270c36 100644 --- a/demo/ApiGateway/Startup.cs +++ b/demo/ApiGateway/Startup.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Builder; +using System.Collections.Generic; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; @@ -38,7 +39,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) } app.UseStaticFiles(); - app.UseSwaggerForOcelotUI(Configuration) + app.UseSwaggerForOcelotUI(Configuration, opt => + { + opt.DownstreamSwaggerHeaders = new[] + { + new KeyValuePair("Key", "Value"), + new KeyValuePair("Key2", "Value2"), + }; + }) .UseOcelot() .Wait(); } diff --git a/src/MMLib.SwaggerForOcelot/Configuration/SwaggerForOcelotUIOptions.cs b/src/MMLib.SwaggerForOcelot/Configuration/SwaggerForOcelotUIOptions.cs index e01f83f..8a2fe14 100644 --- a/src/MMLib.SwaggerForOcelot/Configuration/SwaggerForOcelotUIOptions.cs +++ b/src/MMLib.SwaggerForOcelot/Configuration/SwaggerForOcelotUIOptions.cs @@ -1,5 +1,6 @@ using Swashbuckle.AspNetCore.SwaggerUI; using System; +using System.Collections.Generic; namespace MMLib.SwaggerForOcelot.Configuration { @@ -32,5 +33,10 @@ public string PathToSwaggerGenerator /// + + /// public string DownstreamSwaggerEndPointBasePath { get; set; } = "/swagger/docs"; + + /// + /// Headers to include when requesting a downstream swagger endpoint. + /// + public IEnumerable> DownstreamSwaggerHeaders { get; set; } } } diff --git a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs index 1e5757f..4c25c87 100644 --- a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs +++ b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs @@ -23,6 +23,7 @@ public class SwaggerForOcelotMiddleware private readonly Lazy> _swaggerEndPoints; private readonly IHttpClientFactory _httpClientFactory; private readonly ISwaggerJsonTransformer _transformer; + private readonly SwaggerForOcelotUIOptions _options; /// /// Initializes a new instance of the class. @@ -45,6 +46,7 @@ public SwaggerForOcelotMiddleware( _reRoutes = Check.NotNull(reRoutes, nameof(reRoutes)); Check.NotNull(swaggerEndPoints, nameof(swaggerEndPoints)); _httpClientFactory = Check.NotNull(httpClientFactory, nameof(httpClientFactory)); + _options = options; _swaggerEndPoints = new Lazy>(() => swaggerEndPoints.Value.ToDictionary(p => $"/{p.KeyToPath}", p => p)); @@ -58,7 +60,7 @@ public async Task Invoke(HttpContext context) { var endPoint = GetEndPoint(context.Request.Path); var httpClient = _httpClientFactory.CreateClient(); - + AddHeaders(httpClient); var content = await httpClient.GetStringAsync(endPoint.Url); var hostName = endPoint.EndPoint.HostOverride ?? context.Request.Host.Value; content = _transformer.Transform(content, _reRoutes.Value.Where(p => p.SwaggerKey == endPoint.EndPoint.Key), hostName); @@ -66,6 +68,15 @@ public async Task Invoke(HttpContext context) await context.Response.WriteAsync(content); } + private void AddHeaders(HttpClient httpClient) + { + if (_options.DownstreamSwaggerHeaders == null) return; + foreach (var kvp in _options.DownstreamSwaggerHeaders) + { + httpClient.DefaultRequestHeaders.Add(kvp.Key, kvp.Value); + } + } + /// /// Get Url and Endpoint from path ///