Add Swagger
This commit is contained in:
@@ -7,11 +7,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
|
||||
<PackageReference Include="MQTTnet" Version="4.3.7.1207" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.11" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,6 +6,7 @@ using FrameProcessor.Mqtt;
|
||||
using FrameProcessor.Storage;
|
||||
using FrameProcessor.UrlFetch;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi;
|
||||
using Serilog;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -23,6 +24,34 @@ builder.Host.UseSerilog((context, services, configuration) => configuration
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddOpenApi(options =>
|
||||
{
|
||||
options.AddDocumentTransformer((document, context, _) =>
|
||||
{
|
||||
document.Info.Title = "Frame Processor";
|
||||
document.Info.Version = "v1";
|
||||
|
||||
document.Components ??= new OpenApiComponents();
|
||||
document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
|
||||
document.Components.SecuritySchemes["ApiKey"] = new OpenApiSecurityScheme
|
||||
{
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
In = ParameterLocation.Header,
|
||||
Name = "X-Api-Key",
|
||||
Description = "Shared API key required for /api/* endpoints.",
|
||||
};
|
||||
|
||||
document.Security =
|
||||
[
|
||||
new OpenApiSecurityRequirement
|
||||
{
|
||||
[new OpenApiSecuritySchemeReference("ApiKey", document)] = new List<string>(),
|
||||
},
|
||||
];
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddOptions<MqttOptions>()
|
||||
.Bind(builder.Configuration.GetSection(MqttOptions.SectionName))
|
||||
.ValidateDataAnnotations()
|
||||
@@ -77,6 +106,13 @@ var app = builder.Build();
|
||||
// Eagerly resolve FramesRegistry so an invalid frames.json fails startup fast.
|
||||
_ = app.Services.GetRequiredService<FramesRegistry>();
|
||||
|
||||
app.MapOpenApi();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.SwaggerEndpoint("/openapi/v1.json", "Frame Processor v1");
|
||||
options.RoutePrefix = "swagger";
|
||||
});
|
||||
|
||||
app.UseMiddleware<ApiKeyMiddleware>();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
Reference in New Issue
Block a user