Skip to content

Commit b4fe9de

Browse files
committed
Add Scalar
1 parent dddd37d commit b4fe9de

File tree

8 files changed

+29
-72
lines changed

8 files changed

+29
-72
lines changed

App/Api/Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<ProjectReference Include="..\Services\Services.csproj" />
44
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" />
55
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
6+
<PackageReference Include="Scalar.AspNetCore" />
67
<PackageReference Include="Serilog" />
78
<PackageReference Include="Serilog.Extensions.Hosting" />
89
<PackageReference Include="Serilog.Settings.Configuration" />

App/Api/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"hotReloadEnabled": true,
66
"dotnetRunMessages": true,
77
"launchBrowser": true,
8-
"launchUrl": "http://localhost:5000/",
8+
"launchUrl": "http://localhost:5000/scalar",
99
"environmentVariables": {
1010
"ASPNETCORE_ENVIRONMENT": "Development",
1111
"ASPNETCORE_HTTP_PORTS": "5000"

App/Api/Setup/ApiMiddleware.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Scalar.AspNetCore;
2+
13
namespace Api.Setup;
24

35
internal static class ApiMiddleware
@@ -13,21 +15,23 @@ public static void ConfigureMiddleware(this WebApplication app, IWebHostEnvironm
1315

1416
app.UseOutputCache();
1517

16-
app.UseStaticFiles();
18+
app.MapOpenApi("/openapi/{documentName}.json");
19+
20+
app.UseSwagger(opt =>
21+
{
22+
opt.RouteTemplate = "swagger/{documentName}/swagger.json";
23+
});
1724

18-
app.UseSwagger();
1925
app.UseSwaggerUI(opt =>
2026
{
21-
var descriptions = app.DescribeApiVersions();
22-
foreach (var desc in descriptions)
23-
{
24-
var url = $"/swagger/{desc.GroupName}/swagger.json";
25-
var name = desc.GroupName.ToUpperInvariant();
26-
opt.SwaggerEndpoint(url, name);
27-
}
28-
opt.RoutePrefix = string.Empty;
29-
var indexPath = Path.Combine(environment.WebRootPath, "swagger-ui", "index.html");
30-
opt.IndexStream = () => new FileStream(indexPath, FileMode.Open, FileAccess.Read);
27+
opt.RoutePrefix = "swagger";
28+
});
29+
30+
app.MapScalarApiReference(endpointPrefix: "/scalar", opt =>
31+
{
32+
opt.Title = "Bitcoin Web API";
33+
opt.ShowSidebar = true;
34+
opt.DarkMode = true;
3135
});
3236

3337
app.MapHealthChecks("health");
@@ -41,7 +45,6 @@ public static void ConfigureMiddleware(this WebApplication app, IWebHostEnvironm
4145

4246
app.Use(async (context, next) =>
4347
{
44-
context.Response.Headers.Append("Content-Security-Policy", "default-src 'self'");
4548
context.Response.Headers.Append("X-Content-Type-Options", "nosniff");
4649
context.Response.Headers.Append("X-Frame-Options", "DENY");
4750
context.Response.Headers.Append("Referrer-Policy", "no-referrer");

App/Api/Setup/ApiServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public static void ConfigureServices(this IServiceCollection services)
2828
opt.SubstituteApiVersionInUrl = true;
2929
});
3030

31+
services.AddOpenApi();
3132

3233
services.AddSwaggerGen();
33-
services.ConfigureOptions<SwaggerOptions>();
3434

3535
services.Configure<JsonOptions>(opt =>
3636
{

App/Api/Setup/SwaggerOptions.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

App/Api/wwwroot/swagger-ui/index.html

Lines changed: 0 additions & 19 deletions
This file was deleted.

App/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<PackageVersion Include="Microsoft.AspNetCore.WebUtilities" Version="9.0.4" />
1010
<PackageVersion Include="Microsoft.Extensions.Http" Version="9.0.4" />
1111
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.4" />
12+
<PackageVersion Include="Scalar.AspNetCore" Version="2.1.11" />
1213
<PackageVersion Include="Serilog" Version="4.2.0" />
1314
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
1415
<PackageVersion Include="Serilog.Settings.Configuration" Version="9.0.0" />

Tests/IntegrationTests/ApiEndpointsTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ public async Task BuyAndSell(string? fromDate, string? toDate, HttpStatusCode st
6363
}
6464

6565
[Fact]
66-
public async Task Swagger()
66+
public async Task SwaggerUI()
6767
{
68-
var result = await _fixture.Client.GetAsync("/", cancellationToken: TestContext.Current.CancellationToken);
68+
var result = await _fixture.Client.GetAsync("/swagger", cancellationToken: TestContext.Current.CancellationToken);
69+
result.StatusCode.ShouldBe(HttpStatusCode.OK);
70+
}
71+
72+
[Fact]
73+
public async Task ScalarUI()
74+
{
75+
var result = await _fixture.Client.GetAsync("/scalar", cancellationToken: TestContext.Current.CancellationToken);
6976
result.StatusCode.ShouldBe(HttpStatusCode.OK);
7077
}
7178

0 commit comments

Comments
 (0)