Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added versioning support #16

Merged
merged 9 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions MMLib.SwaggerForOcelot.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.106
# Visual Studio Version 16
VisualStudioVersion = 16.0.28606.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjectService", "demo\ProjectService\ProjectService.csproj", "{DE0D4EF5-77F2-49D8-9941-A0D473744972}"
EndProject
Expand All @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MMLib.SwaggerForOcelot.Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PetstoreService", "demo\PetstoreService\PetstoreService.csproj", "{0C20742E-0D4E-4407-AC5B-4162EEBD8FC5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrderService", "demo\OrderService\OrderService.csproj", "{F823FFA3-F5D9-488E-ADAB-15C4E9790DC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{0C20742E-0D4E-4407-AC5B-4162EEBD8FC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C20742E-0D4E-4407-AC5B-4162EEBD8FC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C20742E-0D4E-4407-AC5B-4162EEBD8FC5}.Release|Any CPU.Build.0 = Release|Any CPU
{F823FFA3-F5D9-488E-ADAB-15C4E9790DC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -62,6 +68,7 @@ Global
{5D0346CB-0F8B-4D0F-94E5-0D7BAAAA0C5D} = {D0CA2BCE-C362-4C5A-8AC9-319742DDCDD8}
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AAA408E9-E738-4F2B-A6C2-B35AAADDB00F}
Expand Down
116 changes: 73 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,78 @@ Direct via `http://ocelotprojecturl:port/swagger` provides documentation for dow
2. Install Nuget package into yout ASP.NET Core Ocelot project.
> dotnet add package MMLib.SwaggerForOcelot
3. Configure SwaggerForOcelot in `ocelot.json`.
```Json
```Json
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5100
}
],
"UpstreamPathTemplate": "/api/contacts/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "contacts"
},
{
"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}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "projects"
}
],
"SwaggerEndPoints": [
{
"Key": "projects",
"Name": "Projects API",
"Url": "http://localhost:5200/swagger/v1/swagger.json"
},
{
"Key": "contacts",
"Name": "Contacts API",
"Url": "http://localhost:5100/swagger/v1/swagger.json"
}
]
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5200
}
],
"UpstreamPathTemplate": "/api/orders/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "orders"
}
```
],
"SwaggerEndPoints": [
{
"Key": "contacts",
"Config": [
{
"Name": "Contacts API",
"Version": "v1",
"Url": "http://localhost:5100/swagger/v1/swagger.json"
}
]
},
{
"Key": "orders",
"Config": [
{
"Name": "Orders API",
"Version": "v0.9",
"Url": "http://localhost:5200/swagger/v0.9/swagger.json"
},
{
"Name": "Orders API",
"Version": "v1",
"Url": "http://localhost:5200/swagger/v1/swagger.json"
},
{
"Name": "Orders API",
"Version": "v2",
"Url": "http://localhost:5200/swagger/v2/swagger.json"
},
{
"Name": "Orders API",
"Version": "v3",
"Url": "http://localhost:5200/swagger/v3/swagger.json"
}
]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost"
}
}
```
>`SwaggerEndPoint` is configuration for downstream service swagger generator endpoint.
Property `Key` is used to pair with the ReRoute configuration. `Name` is displayed in the combobox. `Url` is downstream service swagger generator endpoint.
4. In the `ConfigureServices` method of `Startup.cs`, register the SwaggerForOcelot generator.
Expand All @@ -64,7 +92,9 @@ 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)
app.UseSwaggerForOcelotUI(Configuration, opt => {
opt.EndPointBasePath = "/swagger/docs";
})
```
6. Show your microservices interactive documentation.
>`http://ocelotserviceurl/swagger`
Expand Down
1 change: 1 addition & 0 deletions demo/ApiGateway/ApiGateway.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<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>
Expand Down
1 change: 0 additions & 1 deletion demo/ApiGateway/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MMLib.SwaggerForOcelot;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

Expand Down
57 changes: 32 additions & 25 deletions demo/ApiGateway/ocelot.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,46 @@
"Port": 5200
}
],
"UpstreamPathTemplate": "/api/projects/{everything}",
"UpstreamPathTemplate": "/api/orders/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "projects"
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5300
}
],
"UpstreamPathTemplate": "/api/pets/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "pets"
"SwaggerKey": "orders"
}
],
"SwaggerEndPoints": [
{
"Key": "projects",
"Name": "Projects API",
"Url": "http://localhost:5200/swagger/v1/swagger.json"
},
{
"Key": "contacts",
"Name": "Contacts API",
"Url": "http://localhost:5100/swagger/v1/swagger.json"
"Config": [
{
"Name": "Contacts API",
"Version": "v1",
"Url": "http://localhost:5100/swagger/v1/swagger.json"
}
]
},
{
"Key": "pets",
"Name": "Petsstore API",
"Url": "http://localhost:5300/swagger.json"
"Key": "orders",
"Config": [
{
"Name": "Orders API",
"Version": "v0.9",
"Url": "http://localhost:5200/swagger/v0.9/swagger.json"
},
{
"Name": "Orders API",
"Version": "v1",
"Url": "http://localhost:5200/swagger/v1/swagger.json"
},
{
"Name": "Orders API",
"Version": "v2",
"Url": "http://localhost:5200/swagger/v2/swagger.json"
},
{
"Name": "Orders API",
"Version": "v3",
"Url": "http://localhost:5200/swagger/v3/swagger.json"
}
]
}
],
"GlobalConfiguration": {
Expand Down
55 changes: 55 additions & 0 deletions demo/OrderService/ConfigureSwaggerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace OrderService
{
/// <summary>
/// Configures the Swagger generation options.
/// </summary>
/// <remarks>This allows API versioning to define a Swagger document per API version after the
/// <see cref="IApiVersionDescriptionProvider"/> service has been resolved from the service container.</remarks>
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
readonly IApiVersionDescriptionProvider provider;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerOptions( IApiVersionDescriptionProvider provider ) => this.provider = provider;

/// <inheritdoc />
public void Configure( SwaggerGenOptions options )
{
// add a swagger document for each discovered API version
// note: you might choose to skip or document deprecated API versions differently
foreach ( var description in provider.ApiVersionDescriptions )
{
options.SwaggerDoc( description.GroupName, CreateInfoForApiVersion( description ) );
}
}

static Info CreateInfoForApiVersion( ApiVersionDescription description )
{
var info = new Info()
{
Title = "Sample API",
Version = description.ApiVersion.ToString(),
Description = "A sample application with Swagger, Swashbuckle, and API versioning.",
Contact = new Contact() { Name = "Bill Mei", Email = "[email protected]" },
TermsOfService = "Shareware",
License = new License() { Name = "MIT", Url = "https://opensource.org/licenses/MIT" }
};

if ( description.IsDeprecated )
{
info.Description += " This API version has been deprecated.";
}

return info;
}
}
}
19 changes: 19 additions & 0 deletions demo/OrderService/OrderService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<RootNamespace>OrderService</RootNamespace>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(MSBuildThisFileName).xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
</ItemGroup>


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

namespace OrderService
{
/// <summary>
/// Represents the current application.
/// </summary>
public static class Program
{
/// <summary>
/// The main entry point to the application.
/// </summary>
/// <param name="args">The arguments provided at start-up, if any.</param>
public static void Main( string[] args ) =>
CreateWebHostBuilder( args ).Build().Run();

/// <summary>
/// Builds a new web host for the application.
/// </summary>
/// <param name="args">The command-line arguments, if any.</param>
/// <returns>A new <see cref="IWebHostBuilder">web host builder</see>.</returns>
public static IWebHostBuilder CreateWebHostBuilder( string[] args ) =>
WebHost.CreateDefaultBuilder( args )
.UseStartup<Startup>();
}
}
Loading