-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Different configuration for ocelot swagger generator endpoint and dow…
…nstreat service (#29) * Different configuration for ocelot swagger generator endpoint and downstream service swager * code factor fixed
- Loading branch information
Showing
11 changed files
with
262 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 23 additions & 4 deletions
27
src/MMLib.SwaggerForOcelot/Configuration/SwaggerForOcelotUIOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters