Skip to content

Commit

Permalink
Merge pull request #4 from tsvx/patch-2
Browse files Browse the repository at this point in the history
Fix swagger to work correctly behind the proxy
  • Loading branch information
drwatson1 authored Aug 14, 2020
2 parents 529f296 + 92aff30 commit 606a406
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.OpenApi.Models;
using System.Collections.Generic;

namespace ReferenceProject
{
Expand All @@ -13,7 +15,22 @@ public static class MiddlewareConfig
public static IApplicationBuilder UseSwaggerWithOptions(this IApplicationBuilder app)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
SwaggerBuilderExtensions.UseSwagger(app);
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swaggerDoc, httpRequest) =>
{
if (!httpRequest.Headers.ContainsKey("X-Forwarded-Host")) return;

var serverUrl = $"{httpRequest.Headers["X-Forwarded-Proto"]}://" +
$"{httpRequest.Headers["X-Forwarded-Host"]}" +
$"{httpRequest.Headers["X-Forwarded-Prefix"]}";

swaggerDoc.Servers = new List<OpenApiServer>()
{
new OpenApiServer { Url = serverUrl }
};
});
});

// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
Expand Down

0 comments on commit 606a406

Please sign in to comment.