Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Swagger Endpoint Validation and Implement Custom Options Handling #315

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions MMLib.SwaggerForOcelot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiGatewayWithEndpointInter
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MMLib.SwaggerForOcelot.BenchmarkTests", "tests\MMLib.SwaggerForOcelot.BenchmarkTests\MMLib.SwaggerForOcelot.BenchmarkTests.csproj", "{BFB72205-5F70-474B-BC43-6C0B71C39FFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MMLib.ServiceDiscovery.Consul", "src\MMLib.ServiceDiscovery.Consul\MMLib.ServiceDiscovery.Consul.csproj", "{3935ABE3-B9AF-4BDB-8621-A59D49BF84ED}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ConsulAutoDiscovery", "ConsulAutoDiscovery", "{A91B5036-2064-4C83-B379-5AD8420C4A41}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsulApiGateway", "demo\ConsulAutoDiscovery\ConsulApiGateway\ConsulApiGateway.csproj", "{BC60A6E1-1844-4133-B0F4-DD26DD84E517}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoDiscoveryApi", "demo\ConsulAutoDiscovery\AutoDiscoveryApi\AutoDiscoveryApi.csproj", "{FDF51FDB-5D75-42E0-9FC1-193C222C624C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +83,18 @@ Global
{BFB72205-5F70-474B-BC43-6C0B71C39FFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFB72205-5F70-474B-BC43-6C0B71C39FFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFB72205-5F70-474B-BC43-6C0B71C39FFA}.Release|Any CPU.Build.0 = Release|Any CPU
{3935ABE3-B9AF-4BDB-8621-A59D49BF84ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3935ABE3-B9AF-4BDB-8621-A59D49BF84ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3935ABE3-B9AF-4BDB-8621-A59D49BF84ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3935ABE3-B9AF-4BDB-8621-A59D49BF84ED}.Release|Any CPU.Build.0 = Release|Any CPU
{BC60A6E1-1844-4133-B0F4-DD26DD84E517}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC60A6E1-1844-4133-B0F4-DD26DD84E517}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC60A6E1-1844-4133-B0F4-DD26DD84E517}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC60A6E1-1844-4133-B0F4-DD26DD84E517}.Release|Any CPU.Build.0 = Release|Any CPU
{FDF51FDB-5D75-42E0-9FC1-193C222C624C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FDF51FDB-5D75-42E0-9FC1-193C222C624C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDF51FDB-5D75-42E0-9FC1-193C222C624C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDF51FDB-5D75-42E0-9FC1-193C222C624C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -90,6 +110,10 @@ Global
{DF18713F-3BE4-4300-BB41-D8239AAC2CA4} = {75938D16-D280-4200-A970-8D33B719D252}
{4CF0F59A-E155-4F68-B47B-7042E5A1CB2B} = {75938D16-D280-4200-A970-8D33B719D252}
{BFB72205-5F70-474B-BC43-6C0B71C39FFA} = {A9111054-B663-41BA-AE67-E6FE3E7515AF}
{3935ABE3-B9AF-4BDB-8621-A59D49BF84ED} = {D0CA2BCE-C362-4C5A-8AC9-319742DDCDD8}
{A91B5036-2064-4C83-B379-5AD8420C4A41} = {75938D16-D280-4200-A970-8D33B719D252}
{BC60A6E1-1844-4133-B0F4-DD26DD84E517} = {A91B5036-2064-4C83-B379-5AD8420C4A41}
{FDF51FDB-5D75-42E0-9FC1-193C222C624C} = {A91B5036-2064-4C83-B379-5AD8420C4A41}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AAA408E9-E738-4F2B-A6C2-B35AAADDB00F}
Expand Down
1 change: 0 additions & 1 deletion demo/ApiGateway/ApiGateway.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.8" />
<PackageReference Include="MMLib.Ocelot.Provider.AppConfiguration" Version="2.0.0" />
<PackageReference Include="Ocelot" Version="19.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ocelot" Version="19.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
1 change: 0 additions & 1 deletion demo/ApiGatewayWithPath/ApiGatewayWithPath.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ocelot" Version="19.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
18 changes: 18 additions & 0 deletions demo/ConsulAutoDiscovery/AutoDiscoveryApi/AutoDiscoveryApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\MMLib.ServiceDiscovery.Consul\MMLib.ServiceDiscovery.Consul.csproj" />
</ItemGroup>

</Project>
50 changes: 50 additions & 0 deletions demo/ConsulAutoDiscovery/AutoDiscoveryApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using MMLib.ServiceDiscovery.Consul.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.AddConsulAutoServiceDiscovery("http://localhost:8500"); //This line added
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid hardcoding the Consul service discovery URL

Hardcoding the Consul service URL reduces flexibility and complicates deployment to different environments. It's recommended to retrieve the URL from configuration files or environment variables. This approach enhances maintainability and allows easy configuration changes without modifying code.

Proposed change:

- builder.AddConsulAutoServiceDiscovery("http://localhost:8500"); //This line added
+ var consulUrl = builder.Configuration["ConsulConfig:ConsulUrl"];
+ builder.AddConsulAutoServiceDiscovery(consulUrl);

Ensure that the ConsulConfig:ConsulUrl setting is defined in your configuration files (e.g., appsettings.json):

{
  "ConsulConfig": {
    "ConsulUrl": "http://localhost:8500"
  }
}


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseSwaggerForOcelotUI(); //This line added
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Limit Swagger UI exposure to development environment

The app.UseSwaggerForOcelotUI(); middleware exposes the Swagger UI, which is intended primarily for development purposes. Exposing detailed API documentation in production environments can pose security risks by revealing sensitive information about your API endpoints. It's advisable to limit Swagger UI to the development environment.

Proposed change:

15  if (app.Environment.IsDevelopment())
16  {
17      app.UseSwagger();
18      app.UseSwaggerUI();
+     app.UseSwaggerForOcelotUI(); // Moved inside the development check
19  }
- app.UseSwaggerForOcelotUI(); //This line removed
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
app.UseSwaggerForOcelotUI(); //This line added
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerForOcelotUI(); // Moved inside the development check
}


var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run("http://localhost:7002");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid hardcoding the application URL in app.Run()

Hardcoding the application URL directly in app.Run("http://localhost:7002"); reduces flexibility and may lead to issues when deploying to different environments or hosting configurations. It's recommended to configure the URLs via the launchSettings.json file or environment variables, allowing the hosting settings to be adjusted without code changes.

Proposed change:

- app.Run("http://localhost:7002");
+ app.Run();

Then, in your Properties/launchSettings.json, configure the application URL:

{
  "profiles": {
    "AutoDiscoveryApi": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:7002",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}


record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use standard formula for Celsius to Fahrenheit conversion

The current calculation for TemperatureF uses TemperatureC / 0.5556, which may be less precise and less readable than the standard conversion formula. Using the standard formula improves accuracy and code readability.

Proposed change:

-     public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
+     public int TemperatureF => 32 + (int)(TemperatureC * 9 / 5);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public int TemperatureF => 32 + (int)(TemperatureC * 9 / 5);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions demo/ConsulAutoDiscovery/AutoDiscoveryApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
18 changes: 18 additions & 0 deletions demo/ConsulAutoDiscovery/ConsulApiGateway/ConsulApiGateway.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\MMLib.SwaggerForOcelot\MMLib.SwaggerForOcelot.csproj" />
</ItemGroup>
rabdulatif marked this conversation as resolved.
Show resolved Hide resolved

</Project>
25 changes: 25 additions & 0 deletions demo/ConsulAutoDiscovery/ConsulApiGateway/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Ocelot.DependencyInjection;
using Ocelot.Provider.Consul;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();

builder.Configuration
.SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();

builder.Services
.AddOcelot(builder.Configuration)
.AddConsul();

builder.Services.AddSwaggerForOcelot(builder.Configuration);

var app = builder.Build();

app.UseSwaggerForOcelotUI();
await app.UseOcelot();

app.MapGet("/", () => "Hello World!").WithOpenApi();
app.Run("http://0.0.0.0:7001");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions demo/ConsulAutoDiscovery/ConsulApiGateway/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
14 changes: 14 additions & 0 deletions demo/ConsulAutoDiscovery/ConsulApiGateway/ocelot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Routes": [],
"SwaggerEndPoints": [],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Scheme": "http",
"Host": "localhost",
"Port": 8500,
"Type": "PollConsul",
"PollingInterval": 100
}
}
}

4 changes: 4 additions & 0 deletions demo/OrderService/OrderService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MMLib.ServiceDiscovery.Consul\MMLib.ServiceDiscovery.Consul.csproj" />
</ItemGroup>


</Project>
36 changes: 28 additions & 8 deletions demo/OrderService/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using MMLib.ServiceDiscovery.Consul.DependencyInjection;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -67,6 +67,7 @@ public void ConfigureServices(IServiceCollection services)
// integrate xml comments
options.IncludeXmlComments(XmlCommentsFilePath);
});

}

/// <summary>
Expand All @@ -79,15 +80,34 @@ public void Configure(IApplicationBuilder app, IApiVersionDescriptionProvider pr
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
app.UseSwagger();
_ = app.UseSwaggerUI(
options =>
// _ = app.UseSwaggerUI(
// options =>
// {
// // build a swagger endpoint for each discovered API version
// foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
// {
// options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
// }
// });
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger";
foreach (var description in provider.ApiVersionDescriptions)
{
c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", $"Version {description.GroupName}");
}
});
Comment on lines +92 to +99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Swagger JSON files not found for all API versions.

The search identified Swagger JSON files in:

  • demo/PetstoreService/wwwroot/swagger.json
  • tests/MMLib.SwaggerForOcelot.BenchmarkTests/Resources/Swagger.json

Ensure that Swagger JSON files are generated for each API version as per the PR objectives.

🔗 Analysis chain

Verify the Swagger UI configuration for multiple API versions

The new configuration sets the RoutePrefix to "swagger" and adds Swagger endpoints for all API versions. Ensure that this meets the requirement of providing a clear structure for versioned endpoints as outlined in the PR objectives.

To confirm that the Swagger endpoints are correctly configured for all API versions, you can check for the presence of generated Swagger JSON files for each version.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that Swagger JSON files exist for each API version.

# Test: List all swagger.json files. Expect: Files for each API version.
fd --extension json 'swagger' ./wwwroot/swagger

Length of output: 152


Script:

#!/bin/bash
# Description: Search for all swagger.json files in the project to verify Swagger endpoints for all API versions.

fd --extension json 'swagger\.json'

Length of output: 146


// Separate endpoints that contain only one version
foreach (var description in provider.ApiVersionDescriptions)
{
app.UseSwaggerUI(c =>
{
// build a swagger endpoint for each discovered API version
foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
c.RoutePrefix = $"swagger/{description.GroupName}";
c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", $"Version {description.GroupName}");
});
}

Comment on lines +101 to +110
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid multiple calls to app.UseSwaggerUI; consolidate configurations

Calling app.UseSwaggerUI multiple times inside a loop (lines 102-109) may lead to unexpected behavior, as it's typically intended to be called once during application startup. Consider consolidating the configuration into a single app.UseSwaggerUI call to ensure proper behavior.

Suggested Refactor:

You can merge the configurations to handle multiple API versions within a single app.UseSwaggerUI call. Here's how you might adjust the code:

 app.UseSwaggerUI(c =>
 {
     c.RoutePrefix = "swagger";
     foreach (var description in provider.ApiVersionDescriptions)
     {
         c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", $"Version {description.GroupName}");
+        // Optionally, add additional settings per version here if needed
     }
+    // Remove the loop that calls app.UseSwaggerUI multiple times
 });
 
-// Separate endpoints that contain only one version
-foreach (var description in provider.ApiVersionDescriptions)
-{
-    app.UseSwaggerUI(c =>
-    {
-        c.RoutePrefix = $"swagger/{description.GroupName}";
-        c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", $"Version {description.GroupName}");
-    });
-}

This adjustment ensures that app.UseSwaggerUI is called only once, and all API versions are configured within it.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Separate endpoints that contain only one version
foreach (var description in provider.ApiVersionDescriptions)
{
app.UseSwaggerUI(c =>
{
// build a swagger endpoint for each discovered API version
foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
c.RoutePrefix = $"swagger/{description.GroupName}";
c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", $"Version {description.GroupName}");
});
}
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger";
foreach (var description in provider.ApiVersionDescriptions)
{
c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", $"Version {description.GroupName}");
// Optionally, add additional settings per version here if needed
}
// Remove the loop that calls app.UseSwaggerUI multiple times
});

}

static string XmlCommentsFilePath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.SwaggerUI;

namespace MMLib.ServiceDiscovery.Consul.DependencyInjection;

/// <summary>
///
/// </summary>
public static class ConfigureExtensions
{
/// <summary>
///
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseSwaggerForOcelotUI(this WebApplication builder)
{
builder.ConfigureConsulConnection();
builder.MapHealthChecks("/api/health");

return builder;
}
Comment on lines +17 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Revise method name and implementation to match functionality

The UseSwaggerForOcelotUI method name is misleading as it doesn't set up Swagger UI. Instead, it configures Consul connection and health checks. Consider the following improvements:

  1. Rename the method to better reflect its functionality, e.g., ConfigureConsulAndHealthChecks.
  2. Add Swagger UI setup logic if it's intended to be part of this method.
  3. Update the return type to match the parameter type (WebApplication).

Here's a suggested implementation:

public static WebApplication ConfigureConsulAndHealthChecks(this WebApplication app)
{
    app.ConfigureConsulConnection();
    app.MapHealthChecks("/api/health");

    return app;
}

If Swagger UI setup is needed, consider adding it to this method or creating a separate method for it.


/// <summary>
///
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder ConfigureConsulConnection(this IApplicationBuilder builder)
{
var consul = builder.ApplicationServices.GetService<IConsulConnectionService>();
consul?.Start();

return builder;
}
}
Loading
Loading