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

implemented a workaround for Conul service discovary support. #288

Merged
merged 3 commits into from
Mar 27, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Kros.Utils;
using Kros.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using MMLib.SwaggerForOcelot.Configuration;
Expand All @@ -14,6 +14,7 @@
using Microsoft.OpenApi.Writers;
using System.Text;
using Microsoft.OpenApi.Models;
using MMLib.SwaggerForOcelot.ServiceDiscovery;

namespace MMLib.SwaggerForOcelot.Middleware
{
Expand Down Expand Up @@ -91,15 +92,14 @@ public async Task Invoke(HttpContext context,
RouteOptions route = routeOptions.FirstOrDefault(r => r.SwaggerKey == endPoint.Key);

string content = await downstreamSwaggerDocs.GetSwaggerJsonAsync(route, endPoint, version);

if (endPoint.TransformByOcelotConfig)
if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul")
{
content = _transformer.Transform(
content,
routeOptions,
GetServerName(context, endPoint),
endPoint);
if (endPoint.TransformByOcelotConfig)
{
content = _transformer.Transform(content, routeOptions, GetServerName(context, endPoint), endPoint);
}
}

content = await ReconfigureUpstreamSwagger(context, content);

await context.Response.WriteAsync(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public async Task<string> GetSwaggerJsonAsync(
var clientName = _options.Value.HttpClientName ?? ((route?.DangerousAcceptAnyServerCertificateValidator ?? false) ? ServiceCollectionExtensions.IgnoreSslCertificate : string.Empty);
var httpClient = _httpClientFactory.CreateClient(clientName);

if (!(url.StartsWith("http://",StringComparison.InvariantCultureIgnoreCase) || url.StartsWith("https://",StringComparison.InvariantCultureIgnoreCase)))
{
url = "https://" + url;
}

SetHttpVersion(httpClient, route);
AddHeaders(httpClient);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IOptions<SwaggerOptions> _swaggerOptions;

public SwaggerServiceDiscoveryProvider(

Check warning on line 31 in src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Missing XML comment for publicly visible type or member 'SwaggerServiceDiscoveryProvider.SwaggerServiceDiscoveryProvider(IServiceDiscoveryProviderFactory, IServiceProviderConfigurationCreator, IOptionsMonitor<FileConfiguration>, IHttpContextAccessor, IOptions<SwaggerOptions>)'
IServiceDiscoveryProviderFactory serviceDiscovery,
IServiceProviderConfigurationCreator configurationCreator,
IOptionsMonitor<FileConfiguration> options,
Expand Down Expand Up @@ -81,6 +81,8 @@
{
var conf = _configurationCreator.Create(_options.CurrentValue.GlobalConfiguration);

ServiceProviderType = conf?.Type;

var downstreamRoute = new DownstreamRouteBuilder()
.WithUseServiceDiscovery(true)
.WithServiceName(endPoint.Service.Name)
Expand Down Expand Up @@ -125,6 +127,8 @@
_ => string.Empty,
};

public static string? ServiceProviderType { get; set; }

Check warning on line 130 in src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Missing XML comment for publicly visible type or member 'SwaggerServiceDiscoveryProvider.ServiceProviderType'

private static string GetErrorMessage(SwaggerEndPointConfig endPoint) => $"Service with swagger documentation '{endPoint.Service.Name}' cann't be discovered";
}
}
Loading