Exemplo n.º 1
0
        private async Task <Uri> GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptions route)
        {
            var conf = _configurationCreator.Create(_options.CurrentValue.GlobalConfiguration);

            var downstreamRoute = new DownstreamRouteBuilder()
                                  .WithUseServiceDiscovery(true)
                                  .WithServiceName(endPoint.Service.Name)
                                  .WithServiceNamespace(route?.ServiceNamespace)
                                  .Build();

            Response <IServiceDiscoveryProvider> serviceProvider = _serviceDiscovery.Get(conf, downstreamRoute);

            if (serviceProvider.IsError)
            {
                throw new InvalidOperationException(GetErrorMessage(endPoint));
            }

            ServiceHostAndPort service = (await serviceProvider.Data.Get()).FirstOrDefault()?.HostAndPort;

            if (service is null)
            {
                throw new InvalidOperationException(GetErrorMessage(endPoint));
            }

            var builder = new UriBuilder(GetScheme(service, route), service.DownstreamHost, service.DownstreamPort)
            {
                Path = endPoint.Service.Path
            };

            return(builder.Uri);
        }
 /// <inheritdoc />
 public async Task <Uri> GetSwaggerUriAsync(SwaggerEndPointConfig endPoint, ReRouteOptions reRoute)
 {
     if (!endPoint.Url.IsNullOrEmpty())
     {
         return(new Uri(endPoint.Url));
     }
     else
     {
         return(await GetSwaggerUri(endPoint, reRoute));
     }
 }
        /// <summary>
        /// Get Url and Endpoint from path
        /// </summary>
        /// <param name="path"></param>
        /// <returns>
        /// The Url of a specific version and <see cref="SwaggerEndPointOptions"/>.
        /// </returns>
        private async Task <(string Url, SwaggerEndPointOptions EndPoint)> GetEndPoint(
            string path,
            ISwaggerServiceDiscoveryProvider discoveryProvider)
        {
            (string Version, string Key)endPointInfo = GetEndPointInfo(path);
            SwaggerEndPointOptions endPoint = _swaggerEndPoints.Value[$"/{endPointInfo.Key}"];
            SwaggerEndPointConfig  config   = endPoint.Config.FirstOrDefault(x => x.Version == endPointInfo.Version);

            string url = (await discoveryProvider
                          .GetSwaggerUriAsync(config, _routes.Value.FirstOrDefault(p => p.SwaggerKey == endPoint.Key)))
                         .AbsoluteUri;

            return(url, endPoint);
        }
        private async Task <string> GetUrlAsync(
            RouteOptions route,
            SwaggerEndPointOptions endPoint,
            string docsVersion)
        {
            SwaggerEndPointConfig config =
                string.IsNullOrEmpty(docsVersion)
                ? endPoint.Config.FirstOrDefault()
                : endPoint.Config.FirstOrDefault(x => x.Version == docsVersion);

            return((await _serviceDiscoveryProvider
                    .GetSwaggerUriAsync(config, route))
                   .AbsoluteUri);
        }
Exemplo n.º 5
0
 /// <inheritdoc />
 public async Task <Uri> GetSwaggerUriAsync(SwaggerEndPointConfig endPoint, RouteOptions route)
 {
     if (endPoint.IsGatewayItSelf)
     {
         return(GetGatewayItSelfSwaggerPath(endPoint));
     }
     else if (!endPoint.Url.IsNullOrEmpty())
     {
         return(new Uri(endPoint.Url));
     }
     else
     {
         return(await GetSwaggerUri(endPoint, route));
     }
 }
Exemplo n.º 6
0
        private Uri GetGatewayItSelfSwaggerPath(SwaggerEndPointConfig endPoint)
        {
            var builder = new UriBuilder(
                _httpContextAccessor.HttpContext.Request.Scheme,
                _httpContextAccessor.HttpContext.Request.Host.Host)
            {
                Path = _swaggerOptions.Value
                       .RouteTemplate.Replace("{documentName}", endPoint.Version).Replace("{json|yaml}", "json")
            };

            if (_httpContextAccessor.HttpContext.Request.Host.Port.HasValue)
            {
                builder.Port = _httpContextAccessor.HttpContext.Request.Host.Port.Value;
            }

            return(builder.Uri);
        }
Exemplo n.º 7
0
        private async Task <(SwaggerDescriptor, OpenApiDocument)> LoadSwaggerAsync(string key,
                                                                                   SwaggerEndPointConfig endpoint)
        {
            var descriptor = new SwaggerDescriptor(key, endpoint.Version);

            try
            {
                var swaggerJson = await _client.GetStringAsync(endpoint.Url);

                var openApi = new OpenApiStringReader().Read(swaggerJson, out _);
                return(descriptor, openApi);
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, $"Can't load downstream swagger {endpoint.Url}");
                return(descriptor, null);
            }
        }
 public Task <Uri> GetSwaggerUriAsync(SwaggerEndPointConfig endPoint, ReRouteOptions reRoute) =>
 Task.FromResult(new Uri(endPoint.Url));
 static string GetDescription(SwaggerEndPointConfig config)
 => config.IsGatewayItSelf ? config.Name : $"{config.Name} - {config.Version}";