Skip to content

Commit

Permalink
added changes requested to, accept PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Radoslav Radev committed Mar 15, 2024
1 parent 1234320 commit ebf8afc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
40 changes: 22 additions & 18 deletions src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs
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 Down Expand Up @@ -43,9 +43,13 @@ public class SwaggerForOcelotMiddleware
/// <param name="transformer">The SwaggerJsonTransformer</param>
/// <param name="swaggerProvider">Swagger provider.</param>
/// <param name="downstreamInterceptor">Downstream interceptor.</param>
public SwaggerForOcelotMiddleware(RequestDelegate next, SwaggerForOcelotUIOptions options,
IOptionsMonitor<List<RouteOptions>> routes, ISwaggerJsonTransformer transformer,
ISwaggerProvider swaggerProvider, ISwaggerDownstreamInterceptor downstreamInterceptor = null)
public SwaggerForOcelotMiddleware(
RequestDelegate next,
SwaggerForOcelotUIOptions options,
IOptionsMonitor<List<RouteOptions>> routes,
ISwaggerJsonTransformer transformer,
ISwaggerProvider swaggerProvider,
ISwaggerDownstreamInterceptor downstreamInterceptor = null)
{
_transformer = Check.NotNull(transformer, nameof(transformer));
_next = Check.NotNull(next, nameof(next));
Expand All @@ -61,11 +65,11 @@ public SwaggerForOcelotMiddleware(RequestDelegate next, SwaggerForOcelotUIOption
/// <param name="context">The context.</param>
/// <param name="swaggerEndPointRepository">Swagger endpoint repository.</param>
/// <param name="downstreamSwaggerDocs">Repository for obtaining downstream swagger docs.</param>
public async Task Invoke(HttpContext context, ISwaggerEndPointProvider swaggerEndPointRepository,
public async Task Invoke(HttpContext context,
ISwaggerEndPointProvider swaggerEndPointRepository,
IDownstreamSwaggerDocsRepository downstreamSwaggerDocs)
{
(string version, SwaggerEndPointOptions endPoint) =
GetEndPoint(context.Request.Path, swaggerEndPointRepository);
(string version, SwaggerEndPointOptions endPoint) = GetEndPoint(context.Request.Path, swaggerEndPointRepository);

if (_downstreamInterceptor != null &&
!_downstreamInterceptor.DoDownstreamSwaggerEndpoint(context, version, endPoint))
Expand All @@ -81,21 +85,20 @@ public async Task Invoke(HttpContext context, ISwaggerEndPointProvider swaggerEn
return;
}

IEnumerable<RouteOptions> routeOptions = _routes.CurrentValue.ExpandConfig(endPoint).GroupByPaths();
IEnumerable<RouteOptions> routeOptions = _routes.CurrentValue
.ExpandConfig(endPoint)
.GroupByPaths();

RouteOptions route = routeOptions.FirstOrDefault(r => r.SwaggerKey == endPoint.Key);

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

if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul") // ignore if Consul , this will just replace the freshly fetched swagger json from the service with an empty one
if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul")
{
if (endPoint.TransformByOcelotConfig)
{
content = _transformer.Transform(
content,
routeOptions,
GetServerName(context, endPoint),
endPoint);
content = _transformer.Transform(content, routeOptions, GetServerName(context, endPoint), endPoint);
}
}

content = await ReconfigureUpstreamSwagger(context, content);

Expand All @@ -121,8 +124,8 @@ private string GetServerName(HttpContext context, SwaggerEndPointOptions endPoin
string serverName;
if (string.IsNullOrWhiteSpace(_options.ServerOcelot))
{
serverName = endPoint.HostOverride ??
$"{context.Request.Scheme}://{context.Request.Host.Value.RemoveSlashFromEnd()}";
serverName = endPoint.HostOverride
?? $"{context.Request.Scheme}://{context.Request.Host.Value.RemoveSlashFromEnd()}";
}
else
{
Expand Down Expand Up @@ -153,7 +156,8 @@ private async Task<string> ReconfigureUpstreamSwagger(HttpContext context, strin
return swaggerJson;
}

private (string version, SwaggerEndPointOptions endpoint) GetEndPoint(string path,
private (string version, SwaggerEndPointOptions endpoint) GetEndPoint(
string path,
ISwaggerEndPointProvider swaggerEndPointRepository)
{
(string Version, string Key) endPointInfo = GetEndPointInfo(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +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://") || url.StartsWith("https://"))) //the url that gets constructed when using consul misses the http schema , and trows an exception
if (!(url.StartsWith("http://",StringComparison.InvariantCultureIgnoreCase) || url.StartsWith("https://",StringComparison.InvariantCultureIgnoreCase)))
{
url = "https://" + url; //appending https because that`s what i use (there is no real way to figure out the protocol from Consul)
url = "https://" + url;
}


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ private async Task<Uri> GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio
{
var conf = _configurationCreator.Create(_options.CurrentValue.GlobalConfiguration);

ServiceProviderType = conf.Type; // get a way to detect who the service provider is

ServiceProviderType = conf.Type;

var downstreamRoute = new DownstreamRouteBuilder()
.WithUseServiceDiscovery(true)
Expand Down Expand Up @@ -128,7 +127,6 @@ 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 / 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";
Expand Down

0 comments on commit ebf8afc

Please sign in to comment.