[backend/openapi] Add scalar-ui, add OpenAPI overview page

This commit is contained in:
Laura Hausmann 2024-11-18 18:18:01 +01:00
parent 01bd71c97d
commit 4186388d4c
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
4 changed files with 49 additions and 0 deletions

View file

@ -10,6 +10,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration.Ini;
using Microsoft.Extensions.Options;
using Scalar.AspNetCore;
namespace Iceshrimp.Backend.Core.Extensions;
@ -52,6 +53,20 @@ public static class WebApplicationExtensions
options.ConfigObject.AdditionalItems.Add("tagsSorter", "alpha"); // Sort tags alphabetically
});
app.MapScalarApiReference(options =>
{
options.Title = "Iceshrimp API documentation";
options.OpenApiRoutePattern = "/openapi/{documentName}.json";
options.EndpointPathPrefix = "/scalar/{documentName}";
options.HideModels = true;
options.CustomCss = """
.open-api-client-button, .darklight-reference-promo { display: none !important; }
.darklight { height: 14px !important; }
.darklight-reference { padding: 14px !important; }
""";
});
return app;
}

View file

@ -36,6 +36,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="9.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0" />
<PackageReference Include="Scalar.AspNetCore" Version="1.2.39" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6-iceshrimp" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />

View file

@ -0,0 +1,25 @@
@page "/openapi"
@attribute [Route("/scalar")]
@using Microsoft.Extensions.Options
@using Swashbuckle.AspNetCore.SwaggerGen
@inject IOptions<SwaggerGenOptions> Options;
<h2>OpenAPI definitions</h2>
<p>Please select a OpenAPI document below.</p>
<table class="auto-table">
<thead>
<th class="width0">Document</th>
<th>Links</th>
</thead>
@foreach (var doc in Options.Value.SwaggerGeneratorOptions.SwaggerDocs)
{
<tr>
<td>@doc.Value.Title</td>
<td>
<a href="/openapi/@(doc.Key).json">JSON</a> -
<a href="/swagger/index.html?urls.primaryName=@(doc.Value.Title)">SwaggerUI</a> -
<a href="/scalar/@(doc.Key)">ScalarUI</a>
</td>
</tr>
}
</table>

View file

@ -0,0 +1,8 @@
.width0 {
width: 0;
}
.auto-table {
table-layout: auto;
width: 100%;
}