-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dir): ✨ migrate OpenAPI & SwaggerUI
- Loading branch information
1 parent
ac2dc80
commit e663ac5
Showing
7 changed files
with
230 additions
and
26 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
38 changes: 38 additions & 0 deletions
38
services/Directory/FilterLists.Directory.Api/OpenApi/OpenApiConfigurationExtension.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.OpenApi.Models; | ||
using ConfigurationExtensions = FilterLists.Directory.Application.ConfigurationExtensions; | ||
|
||
namespace FilterLists.Directory.Api.OpenApi; | ||
|
||
internal static class OpenApiConfigurationExtensions | ||
{ | ||
internal static void AddOpenApiGen(this IServiceCollection services) | ||
{ | ||
services.AddEndpointsApiExplorer(); | ||
services.AddSwaggerGen(options => | ||
{ | ||
options.SwaggerDoc( | ||
"v1", | ||
new OpenApiInfo | ||
{ | ||
Title = "FilterLists Directory API", | ||
Description = "An ASP.NET Core API serving the core FilterList information.", | ||
Version = "v1", | ||
TermsOfService = | ||
new Uri("https://github.com/collinbarrett/FilterLists/blob/main/.github/CODE_OF_CONDUCT.md"), | ||
Contact = new OpenApiContact { Name = "FilterLists", Url = new Uri("https://filterlists.com") }, | ||
License = new OpenApiLicense | ||
{ | ||
Name = "MIT License", | ||
Url = new Uri("https://github.com/collinbarrett/FilterLists/blob/main/LICENSE") | ||
} | ||
}); | ||
|
||
// include view model xml comments | ||
var xmlFilename = $"{typeof(ConfigurationExtensions).Assembly.GetName().Name}.xml"; | ||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); | ||
|
||
// include OpenApiTag Description and ExternalDocs | ||
options.DocumentFilter<OpenApiTags.TagDescriptionsDocumentFilter>(); | ||
}); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
services/Directory/FilterLists.Directory.Api/OpenApi/OpenApiTags.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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context; | ||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities; | ||
using JetBrains.Annotations; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace FilterLists.Directory.Api.OpenApi; | ||
|
||
internal static class OpenApiTags | ||
{ | ||
internal static readonly OpenApiTag LanguagesTag = new() | ||
{ | ||
Name = nameof(QueryDbContext.Languages), | ||
Description = "A written form of communication used by sites targeted by a FilterList", | ||
ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = $"{nameof(FilterLists)} {nameof(Language)} Wiki", | ||
Url = new Uri($"https://github.com/collinbarrett/FilterLists/wiki/{nameof(Language)}") | ||
} | ||
}; | ||
|
||
internal static readonly OpenApiTag LicensesTag = new() | ||
{ | ||
Name = nameof(QueryDbContext.Licenses), | ||
Description = "A legal document governing the use or redistribution of a FilterList", | ||
ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = $"{nameof(FilterLists)} {nameof(License)} Wiki", | ||
Url = new Uri($"https://github.com/collinbarrett/FilterLists/wiki/{nameof(License)}") | ||
} | ||
}; | ||
|
||
internal static readonly OpenApiTag FilterListsTag = new() | ||
{ | ||
Name = nameof(QueryDbContext.FilterLists), | ||
Description = "A text file containing a list of rules for blocking or manipulating internet traffic", | ||
ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = $"{nameof(FilterLists)} {nameof(FilterList)} Wiki", | ||
Url = new Uri($"https://github.com/collinbarrett/FilterLists/wiki/{nameof(FilterList)}") | ||
} | ||
}; | ||
|
||
internal static readonly OpenApiTag MaintainersTag = new() | ||
{ | ||
Name = nameof(QueryDbContext.Maintainers), | ||
Description = "An individual, group, or organization who maintains one or more FilterLists", | ||
ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = $"{nameof(FilterLists)} {nameof(Maintainer)} Wiki", | ||
Url = new Uri($"https://github.com/collinbarrett/FilterLists/wiki/{nameof(Maintainer)}") | ||
} | ||
}; | ||
|
||
internal static readonly OpenApiTag SoftwareTag = new() | ||
{ | ||
Name = nameof(QueryDbContext.Software), | ||
Description = "An application, browser extension, or other utility that consumes FilterLists", | ||
ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = $"{nameof(FilterLists)} {nameof(Software)} Wiki", | ||
Url = new Uri($"https://github.com/collinbarrett/FilterLists/wiki/{nameof(Software)}") | ||
} | ||
}; | ||
|
||
internal static readonly OpenApiTag SyntaxesTag = new() | ||
{ | ||
Name = nameof(QueryDbContext.Syntaxes), | ||
Description = "A named set of rules that govern the format of a FilterList", | ||
ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = $"{nameof(FilterLists)} {nameof(Syntax)} Wiki", | ||
Url = new Uri($"https://github.com/collinbarrett/FilterLists/wiki/{nameof(Syntax)}") | ||
} | ||
}; | ||
|
||
internal static readonly OpenApiTag TagsTag = new() | ||
{ | ||
Name = nameof(QueryDbContext.Tags), | ||
Description = | ||
"A generic taxonomy applied to a FilterList to provide information about its contents and/or purpose", | ||
ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = $"{nameof(FilterLists)} {nameof(Tag)} Wiki", | ||
Url = new Uri($"https://github.com/collinbarrett/FilterLists/wiki/{nameof(Tag)}") | ||
} | ||
}; | ||
|
||
[UsedImplicitly] | ||
internal class TagDescriptionsDocumentFilter : IDocumentFilter | ||
{ | ||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) | ||
{ | ||
swaggerDoc.Tags = | ||
[ | ||
LanguagesTag, | ||
LicensesTag, | ||
FilterListsTag, | ||
MaintainersTag, | ||
SoftwareTag, | ||
SyntaxesTag, | ||
TagsTag | ||
]; | ||
} | ||
} | ||
} |
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,16 +1,20 @@ | ||
using FilterLists.Directory.Api; | ||
using FilterLists.Directory.Api.OpenApi; | ||
using FilterLists.Directory.Application; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.AddServiceDefaults(); | ||
builder.Services.AddProblemDetails(); | ||
builder.Services.AddOpenApiGen(); | ||
builder.AddApplication(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseExceptionHandler(); | ||
app.MapEndpoints(); | ||
app.MapDefaultEndpoints(); | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
|
||
app.Run(); |
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