Permalink
Browse files

Add TransformToExternalPath setting (#1728)

* Add nginx sample

* Add TransformToExternalPath setting

* Add comments

* Remove /

* Improve sample

* Transform RedirectUrl to external

* Delete .gitignore

* Update gitignore

* Improve startup

* Update startup

* Improve middleware api

* Rename path param

* Fix whitespace

* Add launchSettings

* Fix whitespaces
  • Loading branch information...
RSuter committed Nov 12, 2018
1 parent 1be21fb commit 088a2b7639c5d88b54fbe841b9d952010127e1c9
Showing with 664 additions and 165 deletions.
  1. +2 −0 .gitignore
  2. +10 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/.dockerignore
  3. +45 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/Controllers/ValuesController.cs
  4. +19 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/Dockerfile
  5. +17 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/Program.cs
  6. +29 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/Properties/launchSettings.json
  7. +26 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/Sample.AspNetCore21.Nginx.csproj
  8. +77 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/Sample.AspNetCore21.Nginx.sln
  9. +99 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/Startup.cs
  10. +9 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/appsettings.Development.json
  11. +8 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/appsettings.json
  12. +18 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/docker-compose.dcproj
  13. +8 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/docker-compose.override.yml
  14. +14 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/docker-compose.yml
  15. +30 −0 samples/WithMiddleware/Sample.AspNetCore21.Nginx/nginx.conf
  16. +0 −43 src/NSwag.AspNet.Owin/Middlewares/RedirectMiddleware.cs
  17. +55 −0 src/NSwag.AspNet.Owin/Middlewares/RedirectToIndexMiddleware.cs
  18. +3 −3 src/NSwag.AspNet.Owin/Middlewares/{SwaggerMiddleware.cs → SwaggerDocumentMiddleware.cs}
  19. +1 −1 src/NSwag.AspNet.Owin/Middlewares/SwaggerUiIndexMiddleware.cs
  20. +13 −13 src/NSwag.AspNet.Owin/SwaggerExtensions.cs
  21. +19 −18 src/NSwag.AspNetCore/Extensions/NSwagApplicationBuilderExtensions .cs
  22. +21 −11 src/NSwag.AspNetCore/Middlewares/{RedirectMiddleware.cs → RedirectToIndexMiddleware.cs}
  23. +7 −6 src/NSwag.AspNetCore/Middlewares/{SwaggerMiddleware.cs → SwaggerDocumentMiddleware.cs}
  24. +1 −1 src/NSwag.AspNetCore/Middlewares/SwaggerUiIndexMiddleware.cs
  25. +9 −12 ...Swag.AspNetCore/{Middlewares/SwaggerMiddlewareSettings.cs → SwaggerDocumentMiddlewareSettings.cs}
  26. +20 −17 src/NSwag.AspNetCore/SwaggerExtensions.cs
  27. +9 −2 src/NSwag.AspNetCore/SwaggerReDocSettings.cs
  28. +14 −5 src/NSwag.AspNetCore/SwaggerSettings.cs
  29. +13 −5 src/NSwag.AspNetCore/SwaggerUi3Settings.cs
  30. +10 −2 src/NSwag.AspNetCore/SwaggerUiSettings.cs
  31. +24 −5 src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs
  32. +8 −8 src/NSwag.Sample.NETCore11/Startup.cs
  33. +2 −2 src/NSwag.Sample.NETCore20/Properties/launchSettings.json
  34. +18 −11 src/NSwag.Sample.NETCore20/Startup.cs
  35. +1 −0 src/NSwag.Sample.NETCore21/Properties/launchSettings.json
  36. +5 −0 src/NSwag.Sample.NETCore21/Startup.cs
@@ -67,3 +67,5 @@ NSwagStudio*.nupkg
/samples/.vs/*
.vscode/
/src/.cr/*
/samples/WithMiddleware/Sample.AspNetCore21.Nginx/.vs
/samples/WithMiddleware/Sample.AspNetCore21.Nginx/Properties
@@ -0,0 +1,10 @@
.dockerignore
.env
.git
.gitignore
.vs
.vscode
docker-compose.yml
docker-compose.*.yml
*/bin
*/obj
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace SimpleApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
@@ -0,0 +1,19 @@
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["Sample.AspNetCore21.Nginx.csproj", "."]
RUN dotnet restore "Sample.AspNetCore21.Nginx.csproj"
COPY . .
WORKDIR "/src"
RUN dotnet build "Sample.AspNetCore21.Nginx.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Sample.AspNetCore21.Nginx.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Sample.AspNetCore21.Nginx.dll"]
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace SimpleApp
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59185/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SimpleApp": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:59185/"
}
}
}
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<DockerTargetOS>Windows</DockerTargetOS>
<DockerComposeProjectPath>docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.5" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\NSwag.AspNetCore\NSwag.AspNetCore.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,77 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.AspNetCore21.Nginx", "Sample.AspNetCore21.Nginx.csproj", "{1D7FCD75-55BD-4D44-823B-AE3787BDE313}"
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{3093F9F5-4FEC-4238-A84D-7B6BB5D9EA1C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.AspNetCore", "..\..\..\src\NSwag.AspNetCore\NSwag.AspNetCore.csproj", "{48CEB61F-A6EC-473B-A097-A03AAF127CA3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.Annotations", "..\..\..\src\NSwag.Annotations\NSwag.Annotations.csproj", "{BFA5720C-246B-416F-B6EC-BAC139517BF8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.Core", "..\..\..\src\NSwag.Core\NSwag.Core.csproj", "{798471D1-631C-4426-8C0D-D566EC6587D2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.SwaggerGeneration", "..\..\..\src\NSwag.SwaggerGeneration\NSwag.SwaggerGeneration.csproj", "{490E6D93-6E51-4130-96D5-FE350CE0AA3E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.SwaggerGeneration.WebApi", "..\..\..\src\NSwag.SwaggerGeneration.WebApi\NSwag.SwaggerGeneration.WebApi.csproj", "{E5F6FAE1-6957-45E0-A71D-07F198ACA97B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.SwaggerGeneration.AspNetCore", "..\..\..\src\NSwag.SwaggerGeneration.AspNetCore\NSwag.SwaggerGeneration.AspNetCore.csproj", "{005B89AC-649E-4E1E-89EB-48E60AAD692D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NSwag", "NSwag", "{522B6825-350D-4A96-940D-F4BC31FBB5CB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1D7FCD75-55BD-4D44-823B-AE3787BDE313}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D7FCD75-55BD-4D44-823B-AE3787BDE313}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D7FCD75-55BD-4D44-823B-AE3787BDE313}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D7FCD75-55BD-4D44-823B-AE3787BDE313}.Release|Any CPU.Build.0 = Release|Any CPU
{3093F9F5-4FEC-4238-A84D-7B6BB5D9EA1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3093F9F5-4FEC-4238-A84D-7B6BB5D9EA1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3093F9F5-4FEC-4238-A84D-7B6BB5D9EA1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3093F9F5-4FEC-4238-A84D-7B6BB5D9EA1C}.Release|Any CPU.Build.0 = Release|Any CPU
{48CEB61F-A6EC-473B-A097-A03AAF127CA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48CEB61F-A6EC-473B-A097-A03AAF127CA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48CEB61F-A6EC-473B-A097-A03AAF127CA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48CEB61F-A6EC-473B-A097-A03AAF127CA3}.Release|Any CPU.Build.0 = Release|Any CPU
{BFA5720C-246B-416F-B6EC-BAC139517BF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFA5720C-246B-416F-B6EC-BAC139517BF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFA5720C-246B-416F-B6EC-BAC139517BF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFA5720C-246B-416F-B6EC-BAC139517BF8}.Release|Any CPU.Build.0 = Release|Any CPU
{798471D1-631C-4426-8C0D-D566EC6587D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{798471D1-631C-4426-8C0D-D566EC6587D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{798471D1-631C-4426-8C0D-D566EC6587D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{798471D1-631C-4426-8C0D-D566EC6587D2}.Release|Any CPU.Build.0 = Release|Any CPU
{490E6D93-6E51-4130-96D5-FE350CE0AA3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{490E6D93-6E51-4130-96D5-FE350CE0AA3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{490E6D93-6E51-4130-96D5-FE350CE0AA3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{490E6D93-6E51-4130-96D5-FE350CE0AA3E}.Release|Any CPU.Build.0 = Release|Any CPU
{E5F6FAE1-6957-45E0-A71D-07F198ACA97B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5F6FAE1-6957-45E0-A71D-07F198ACA97B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5F6FAE1-6957-45E0-A71D-07F198ACA97B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5F6FAE1-6957-45E0-A71D-07F198ACA97B}.Release|Any CPU.Build.0 = Release|Any CPU
{005B89AC-649E-4E1E-89EB-48E60AAD692D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{005B89AC-649E-4E1E-89EB-48E60AAD692D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{005B89AC-649E-4E1E-89EB-48E60AAD692D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{005B89AC-649E-4E1E-89EB-48E60AAD692D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{48CEB61F-A6EC-473B-A097-A03AAF127CA3} = {522B6825-350D-4A96-940D-F4BC31FBB5CB}
{BFA5720C-246B-416F-B6EC-BAC139517BF8} = {522B6825-350D-4A96-940D-F4BC31FBB5CB}
{798471D1-631C-4426-8C0D-D566EC6587D2} = {522B6825-350D-4A96-940D-F4BC31FBB5CB}
{490E6D93-6E51-4130-96D5-FE350CE0AA3E} = {522B6825-350D-4A96-940D-F4BC31FBB5CB}
{E5F6FAE1-6957-45E0-A71D-07F198ACA97B} = {522B6825-350D-4A96-940D-F4BC31FBB5CB}
{005B89AC-649E-4E1E-89EB-48E60AAD692D} = {522B6825-350D-4A96-940D-F4BC31FBB5CB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F4533C7E-C2A0-4845-9A3D-2408380AED4C}
EndGlobalSection
EndGlobal
@@ -0,0 +1,99 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NSwag.AspNetCore;
using System.Linq;
namespace SimpleApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
services.AddSwaggerDocument();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
app.UseMvc();
// There are two ways to run this app:
// 1. Run docker-compose and access http://localhost:8080/externalpath/swagger
// 2. Run Sample.AspNetCore21.Nginx and access http://localhost:59185/swagger/
// both URLs should be correctly served...
// Config with support for multiple documents
app.UseSwagger(config => config.PostProcess = (document, request) =>
{
if (request.Headers.ContainsKey("X-External-Host"))
{
// Change document server settings to public
document.Host = request.Headers["X-External-Host"].First();
document.BasePath = request.Headers["X-External-Path"].First();
}
});
app.UseSwaggerUi3(config => config.TransformToExternalPath = (internalUiRoute, request) =>
{
// The header X-External-Path is set in the nginx.conf file
var externalPath = request.Headers.ContainsKey("X-External-Path") ? request.Headers["X-External-Path"].First() : "";
return externalPath + internalUiRoute;
});
// Config with single document
//app.UseSwagger(config =>
//{
// config.Path = "/swagger/v1/swagger.json";
// config.PostProcess = (document, request) =>
// {
// if (request.Headers.ContainsKey("X-External-Host"))
// {
// // Change document server settings to public
// document.Host = request.Headers["X-External-Host"].First();
// document.BasePath = request.Headers["X-External-Path"].First();
// }
// };
//});
//app.UseSwaggerUi3(config =>
//{
// config.SwaggerRoute = "/swagger/v1/swagger.json";
// config.TransformToExternalPath = (internalUiRoute, request) =>
// {
// // The header X-External-Path is set in the nginx.conf file
// var externalPath = request.Headers.ContainsKey("X-External-Path") ? request.Headers["X-External-Path"].First() : "";
// return externalPath + internalUiRoute;
// };
//});
}
}
}
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectGuid>3093f9f5-4fec-4238-a84d-7b6bb5d9ea1c</ProjectGuid>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}</DockerServiceUrl>
<DockerServiceName>simpleapp</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>
@@ -0,0 +1,8 @@
version: '3.4'
services:
sampleaspnetcore21nginx:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "80"
Oops, something went wrong.

0 comments on commit 088a2b7

Please sign in to comment.