Skip to content

Commit

Permalink
Different configuration for ocelot swagger generator endpoint and dow…
Browse files Browse the repository at this point in the history
…nstreat service (#29)

* Different configuration for ocelot swagger generator endpoint and downstream service swager

* code factor fixed
  • Loading branch information
Burgyn authored Jun 6, 2019
1 parent 51a02d8 commit 5e007fe
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 9 deletions.
7 changes: 7 additions & 0 deletions MMLib.SwaggerForOcelot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PetstoreService", "demo\Pet
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrderService", "demo\OrderService\OrderService.csproj", "{F823FFA3-F5D9-488E-ADAB-15C4E9790DC5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiGatewayWithPath", "demo\ApiGatewayWithPath\ApiGatewayWithPath.csproj", "{DF18713F-3BE4-4300-BB41-D8239AAC2CA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{F823FFA3-F5D9-488E-ADAB-15C4E9790DC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F823FFA3-F5D9-488E-ADAB-15C4E9790DC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F823FFA3-F5D9-488E-ADAB-15C4E9790DC5}.Release|Any CPU.Build.0 = Release|Any CPU
{DF18713F-3BE4-4300-BB41-D8239AAC2CA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF18713F-3BE4-4300-BB41-D8239AAC2CA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF18713F-3BE4-4300-BB41-D8239AAC2CA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF18713F-3BE4-4300-BB41-D8239AAC2CA4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -69,6 +75,7 @@ Global
{D66F3C86-8D57-4802-B2F9-15F91262F12D} = {A9111054-B663-41BA-AE67-E6FE3E7515AF}
{0C20742E-0D4E-4407-AC5B-4162EEBD8FC5} = {75938D16-D280-4200-A970-8D33B719D252}
{F823FFA3-F5D9-488E-ADAB-15C4E9790DC5} = {75938D16-D280-4200-A970-8D33B719D252}
{DF18713F-3BE4-4300-BB41-D8239AAC2CA4} = {75938D16-D280-4200-A970-8D33B719D252}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AAA408E9-E738-4F2B-A6C2-B35AAADDB00F}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Direct via `http://ocelotprojecturl:port/swagger` provides documentation for dow
5. In `Configure` method, insert the `SwaggerForOcelot` middleware to expose interactive documentation.
```CSharp
app.UseSwaggerForOcelotUI(Configuration, opt => {
opt.EndPointBasePath = "/swagger/docs";
opt.PathToSwaggerGenerator = "/swagger/docs";
})
```
6. Show your microservices interactive documentation.
Expand Down
18 changes: 18 additions & 0 deletions demo/ApiGatewayWithPath/ApiGatewayWithPath.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Ocelot" Version="12.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MMLib.SwaggerForOcelot\MMLib.SwaggerForOcelot.csproj" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions demo/ApiGatewayWithPath/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace ApiGatewayWithPath
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json",
optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.local.json", optional: true, reloadOnChange: true)
.AddJsonFile("ocelot.json")
.AddEnvironmentVariables();
})
.UseStartup<Startup>();
}
}
48 changes: 48 additions & 0 deletions demo/ApiGatewayWithPath/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace ApiGatewayWithPath
{
public class Startup
{
private readonly IHostingEnvironment _env;
public Startup(IHostingEnvironment env, IConfiguration config)
{
_env = env;
Configuration = config;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot();
services.AddSwaggerForOcelot(Configuration);

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UsePathBase("/gateway");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseSwaggerForOcelotUI(Configuration, opt => {
opt.DownstreamSwaggerEndPointBasePath = "/gateway/swagger/docs";
opt.PathToSwaggerGenerator= "/swagger/docs";
})
.UseOcelot()
.Wait();
}
}
}
9 changes: 9 additions & 0 deletions demo/ApiGatewayWithPath/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
8 changes: 8 additions & 0 deletions demo/ApiGatewayWithPath/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
115 changes: 115 additions & 0 deletions demo/ApiGatewayWithPath/ocelot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5100
}
],
"UpstreamPathTemplate": "/api/contacts/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "contacts"
},
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5200
}
],
"UpstreamPathTemplate": "/api/projects/{everything}",
"SwaggerKey": "projects"
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5300
}
],
"UpstreamHttpMethod": [ "Get", "Post" ],
"UpstreamPathTemplate": "/api/pets/{everything}",
"SwaggerKey": "pets"
},
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5400
}
],
"UpstreamPathTemplate": "/api/orders/{everything}",
"UpstreamHttpMethod": [],
"SwaggerKey": "orders"
}
],
"SwaggerEndPoints": [
{
"Key": "contacts",
"Config": [
{
"Name": "Contacts API",
"Version": "v1",
"Url": "http://localhost:5000/swagger/v1/swagger.json"
}
]
},
{
"Key": "projects",
"Config": [
{
"Name": "Projects API",
"Version": "v1",
"Url": "http://localhost:5200/swagger/v1/swagger.json"
}
]
},
{
"Key": "pets",
"Config": [
{
"Name": "Pets API",
"Version": "v1",
"Url": "http://localhost:5300/swagger.json"
}
]
},
{
"Key": "orders",
"Config": [
{
"Name": "Orders API",
"Version": "v0.9",
"Url": "http://localhost:5400/swagger/v0.9/swagger.json"
},
{
"Name": "Orders API",
"Version": "v1",
"Url": "http://localhost:5400/swagger/v1/swagger.json"
},
{
"Name": "Orders API",
"Version": "v2",
"Url": "http://localhost:5400/swagger/v2/swagger.json"
},
{
"Name": "Orders API",
"Version": "v3",
"Url": "http://localhost:5400/swagger/v3/swagger.json"
}
]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SwaggerEndPointOptions
/// The swagger endpoint config collection
/// </summary>
public List<SwaggerEndPointConfig> Config { get; set; }

/// <summary>
/// This host url is use used to overwrite the host of the upstream service.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
using Swashbuckle.AspNetCore.SwaggerUI;
using System;

namespace MMLib.SwaggerForOcelot.Configuration
{
/// <summary>
/// Configuration for Swagger UI.
/// </summary>
/// <seealso cref="Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIOptions" />
public class SwaggerForOcelotUIOptions: SwaggerUIOptions
public class SwaggerForOcelotUIOptions : SwaggerUIOptions
{
private string _pathToSwaggerUI = "/swagger/docs";

/// <summary>
/// The relative path to gateway swagger generator.
/// </summary>
public string PathToSwaggerGenerator
{
get => !string.IsNullOrWhiteSpace(EndPointBasePath) ? EndPointBasePath : _pathToSwaggerUI;
set => _pathToSwaggerUI = value;
}

/// <summary>
/// The base path to ocelot gateway swagger UI.
/// </summary>
[Obsolete("Use the 'PathToSwaggerUI' property.")]
public string EndPointBasePath { get; set; }

/// <summary>
/// The end point base path. The final path to swagger endpoint is
/// <see cref="EndPointBasePath"/> + <see cref="SwaggerEndPointOptions.Key"/>
/// The base path to downstream service api swagger generator endpoint.
/// Final path is:
/// <see cref="EndPointBasePath"/> + <see cref="SwaggerEndPointConfig.Version"/> + <see cref="SwaggerEndPointOptions.Key"/>
/// </summary>
public string EndPointBasePath { get; set; } = "/swagger/docs";
public string DownstreamSwaggerEndPointBasePath { get; set; } = "/swagger/docs";
}
}
6 changes: 3 additions & 3 deletions src/MMLib.SwaggerForOcelot/Middleware/BuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public static IApplicationBuilder UseSwaggerForOcelotUI(
{
InitUIOption(c, options);
var endPoints = GetConfiguration(configuration);
AddSwaggerEndPoints(c, endPoints, options.EndPointBasePath);
AddSwaggerEndPoints(c, endPoints, options.DownstreamSwaggerEndPointBasePath);
});

return app;
}

private static void UseSwaggerForOcelot(IApplicationBuilder app, SwaggerForOcelotUIOptions options)
=> app.Map(options.EndPointBasePath, builder => builder.UseMiddleware<SwaggerForOcelotMiddleware>(options));
=> app.Map(options.PathToSwaggerGenerator, builder => builder.UseMiddleware<SwaggerForOcelotMiddleware>(options));

private static void AddSwaggerEndPoints(SwaggerUIOptions c, IEnumerable<SwaggerEndPointOptions> endPoints, string basePath)
{
Expand All @@ -63,7 +63,7 @@ private static void AddSwaggerEndPoints(SwaggerUIOptions c, IEnumerable<SwaggerE
foreach (var config in endPoint.Config)
{
c.SwaggerEndpoint($"{basePath}/{config.Version}/{endPoint.KeyToPath}", $"{config.Name} - {config.Version}");
}
}
}
}

Expand Down

0 comments on commit 5e007fe

Please sign in to comment.