Skip to content

Commit

Permalink
implemented a workaround for Conul service discovary support. (#288)
Browse files Browse the repository at this point in the history
* implemented a workaround for Conul service discovary. all thought the solution in this PR works  (please take it as  just guidance on how to fix it) , i didnt had time to get fully familiar with your code base and just aplied a hot fix

* added changes requested to, accept PR

* updated the ServiceProviderType to support null since the conf is null when unit testing

---------

Co-authored-by: Radoslav Radev <[email protected]>
  • Loading branch information
raga70 and Radoslav Radev authored Mar 27, 2024
1 parent ccf6bd3 commit c7f6602
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
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 @@ -81,6 +81,8 @@ private async Task<Uri> GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio
{
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 @@ private string GetScheme(ServiceHostAndPort service, RouteOptions route)
_ => 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 / deploy

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 / deploy

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";
}
}

0 comments on commit c7f6602

Please sign in to comment.