Skip to content

Commit

Permalink
Add Swagger for APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Jul 6, 2023
1 parent aff067a commit 8bc05b3
Show file tree
Hide file tree
Showing 12 changed files with 642 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Polly;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -84,6 +87,88 @@
};
});

services.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc(
$"ClassifiedAds",
new OpenApiInfo()
{
Title = "ClassifiedAds API",
Version = "1",
Description = "ClassifiedAds API Specification.",
Contact = new OpenApiContact
{
Email = "abc.xyz@gmail.com",
Name = "Phong Nguyen",
Url = new Uri("https://github.com/phongnguyend"),
},
License = new OpenApiLicense
{
Name = "MIT License",
Url = new Uri("https://opensource.org/licenses/MIT"),
},
});
setupAction.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
Description = "Input your Bearer token to access this API",
});
setupAction.AddSecurityDefinition("Oidc", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
TokenUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/token", UriKind.Absolute),
AuthorizationUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/authorize", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{ "openid", "OpenId" },
{ "profile", "Profile" },
{ "ClassifiedAds.WebAPI", "ClassifiedAds WebAPI" },
},
},
ClientCredentials = new OpenApiOAuthFlow
{
TokenUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/token", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{ "ClassifiedAds.WebAPI", "ClassifiedAds WebAPI" },
},
},
},
});
setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Oidc",
},
}, new List<string>()
},
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer",
},
}, new List<string>()
},
});
});

services.AddRateLimiter(options =>
{
options.AddPolicy<string, GetAuditLogsRateLimiterPolicy>(RateLimiterPolicyNames.GetAuditLogsPolicy);
Expand Down Expand Up @@ -116,6 +201,27 @@

app.UseCors("AllowAnyOrigin");

app.UseSwagger();

app.UseSwaggerUI(setupAction =>
{
setupAction.OAuthClientId("Swagger");
setupAction.OAuthClientSecret("secret");
setupAction.OAuthUsePkce();
setupAction.SwaggerEndpoint(
"/swagger/ClassifiedAds/swagger.json",
"ClassifiedAds API");
setupAction.RoutePrefix = string.Empty;
setupAction.DefaultModelExpandDepth(2);
setupAction.DefaultModelRendering(ModelRendering.Model);
setupAction.DocExpansion(DocExpansion.None);
setupAction.EnableDeepLinking();
setupAction.DisplayOperationId();
});

app.UseAuthentication();
app.UseAuthorization();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Polly;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -82,6 +85,88 @@
};
});

services.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc(
$"ClassifiedAds",
new OpenApiInfo()
{
Title = "ClassifiedAds API",
Version = "1",
Description = "ClassifiedAds API Specification.",
Contact = new OpenApiContact
{
Email = "abc.xyz@gmail.com",
Name = "Phong Nguyen",
Url = new Uri("https://github.com/phongnguyend"),
},
License = new OpenApiLicense
{
Name = "MIT License",
Url = new Uri("https://opensource.org/licenses/MIT"),
},
});
setupAction.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
Description = "Input your Bearer token to access this API",
});
setupAction.AddSecurityDefinition("Oidc", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
TokenUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/token", UriKind.Absolute),
AuthorizationUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/authorize", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{ "openid", "OpenId" },
{ "profile", "Profile" },
{ "ClassifiedAds.WebAPI", "ClassifiedAds WebAPI" },
},
},
ClientCredentials = new OpenApiOAuthFlow
{
TokenUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/token", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{ "ClassifiedAds.WebAPI", "ClassifiedAds WebAPI" },
},
},
},
});
setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Oidc",
},
}, new List<string>()
},
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer",
},
}, new List<string>()
},
});
});

// Configure the HTTP request pipeline.
var app = builder.Build();

Expand All @@ -105,6 +190,27 @@

app.UseCors("AllowAnyOrigin");

app.UseSwagger();

app.UseSwaggerUI(setupAction =>
{
setupAction.OAuthClientId("Swagger");
setupAction.OAuthClientSecret("secret");
setupAction.OAuthUsePkce();
setupAction.SwaggerEndpoint(
"/swagger/ClassifiedAds/swagger.json",
"ClassifiedAds API");
setupAction.RoutePrefix = string.Empty;
setupAction.DefaultModelExpandDepth(2);
setupAction.DefaultModelRendering(ModelRendering.Model);
setupAction.DocExpansion(DocExpansion.None);
setupAction.EnableDeepLinking();
setupAction.DisplayOperationId();
});

app.UseAuthentication();
app.UseAuthorization();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 8bc05b3

Please sign in to comment.