Skip to content

Commit

Permalink
Allow for headers to be included when the gateway is requesting downs…
Browse files Browse the repository at this point in the history
…tream swagger endpoint (#39)

* allow for headers to be included when the gateway is requesting downstream swagger endpoint

* refactor and documentation
  • Loading branch information
duncan-G authored and Burgyn committed Jul 30, 2019
1 parent 78e9aca commit 1e2c72b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>("Auth-Key", "AuthValue"),
};
})
```
6. Show your microservices interactive documentation.
> `http://ocelotserviceurl/swagger`
Expand Down
12 changes: 10 additions & 2 deletions demo/ApiGateway/Startup.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, string>("Key", "Value"),
new KeyValuePair<string, string>("Key2", "Value2"),
};
})
.UseOcelot()
.Wait();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;

namespace MMLib.SwaggerForOcelot.Configuration
{
Expand Down Expand Up @@ -32,5 +33,10 @@ public string PathToSwaggerGenerator
/// <see cref="EndPointBasePath"/> + <see cref="SwaggerEndPointConfig.Version"/> + <see cref="SwaggerEndPointOptions.Key"/>
/// </summary>
public string DownstreamSwaggerEndPointBasePath { get; set; } = "/swagger/docs";

/// <summary>
/// Headers to include when requesting a downstream swagger endpoint.
/// </summary>
public IEnumerable<KeyValuePair<string, string>> DownstreamSwaggerHeaders { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class SwaggerForOcelotMiddleware
private readonly Lazy<Dictionary<string, SwaggerEndPointOptions>> _swaggerEndPoints;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ISwaggerJsonTransformer _transformer;
private readonly SwaggerForOcelotUIOptions _options;

/// <summary>
/// Initializes a new instance of the <see cref="SwaggerForOcelotMiddleware"/> class.
Expand All @@ -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<Dictionary<string, SwaggerEndPointOptions>>(()
=> swaggerEndPoints.Value.ToDictionary(p => $"/{p.KeyToPath}", p => p));
Expand All @@ -58,14 +60,23 @@ 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);

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);
}
}

/// <summary>
/// Get Url and Endpoint from path
/// </summary>
Expand Down

0 comments on commit 1e2c72b

Please sign in to comment.