| | | 1 | | using Anichron.API; |
| | | 2 | | using Microsoft.AspNetCore.OpenApi; |
| | | 3 | | using Microsoft.OpenApi; |
| | | 4 | | |
| | | 5 | | namespace Anichron.OpenApiGenerator; |
| | | 6 | | |
| | | 7 | | public static class ApiDocument |
| | | 8 | | { |
| | | 9 | | public static async Task<OpenApiDocument> ComposeAsync() |
| | 1 | 10 | | { |
| | 1 | 11 | | var builder = WebApplication.CreateBuilder(new WebApplicationOptions |
| | 1 | 12 | | { |
| | 1 | 13 | | ApplicationName = "Anichron.API", |
| | 1 | 14 | | EnvironmentName = Environments.Production, |
| | 1 | 15 | | }); |
| | 1 | 16 | | builder.Services.AddApiSurface(builder.Configuration); |
| | 1 | 17 | | WebApplication? app = null; |
| | 1 | 18 | | builder.Services.AddSingleton<EndpointDataSource>( |
| | 1 | 19 | | _ => new CompositeEndpointDataSource(((IEndpointRouteBuilder)app!).DataSources)); |
| | | 20 | | |
| | 1 | 21 | | await using (app = builder.Build()) |
| | 1 | 22 | | { |
| | 1 | 23 | | app.MapApiEndpoints(); |
| | 1 | 24 | | return await app.Services.GetRequiredKeyedService<IOpenApiDocumentProvider>("v1").GetOpenApiDocumentAsync(); |
| | | 25 | | } |
| | 1 | 26 | | } |
| | | 27 | | |
| | | 28 | | public static async Task WriteAsync(string path) |
| | 0 | 29 | | { |
| | 0 | 30 | | var document = await ComposeAsync(); |
| | 0 | 31 | | var json = await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1); |
| | 0 | 32 | | await File.WriteAllTextAsync(path, json.ReplaceLineEndings("\n") + "\n"); |
| | 0 | 33 | | } |
| | | 34 | | } |