/// <summary>
        /// Invokes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="discoveryProvider">The discovery provider.</param>
        public async Task Invoke(HttpContext context, ISwaggerServiceDiscoveryProvider discoveryProvider)
        {
            (string Url, SwaggerEndPointOptions EndPoint) = await GetEndPoint(context.Request.Path, discoveryProvider);

            IEnumerable <RouteOptions> routeOptions = _routes.Value
                                                      .ExpandConfig(EndPoint)
                                                      .GroupByPaths();

            HttpClient httpClient = _httpClientFactory.CreateClient("SwaggerForOcelot");

            SetHttpVersion(httpClient, routeOptions);
            AddHeaders(httpClient);
            string content = await httpClient.GetStringAsync(Url);

            string serverName;

            if (string.IsNullOrWhiteSpace(_options.ServerOcelot))
            {
                serverName = EndPoint.HostOverride
                             ?? $"{context.Request.Scheme}://{context.Request.Host.Value.RemoveSlashFromEnd()}";
            }
            else
            {
                serverName = _options.ServerOcelot;
            }

            if (EndPoint.TransformByOcelotConfig)
            {
                content = _transformer.Transform(content, routeOptions, serverName);
            }
            content = await ReconfigureUpstreamSwagger(context, content);

            await context.Response.WriteAsync(content);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DownstreamSwaggerDocsRepository"/> class.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <param name="httpClientFactory">The HTTP client factory.</param>
 /// <param name="serviceDiscoveryProvider">The service discovery provider.</param>
 public DownstreamSwaggerDocsRepository(
     IOptions <SwaggerForOcelotUIOptions> options,
     IHttpClientFactory httpClientFactory,
     ISwaggerServiceDiscoveryProvider serviceDiscoveryProvider)
 {
     _options                  = options;
     _httpClientFactory        = httpClientFactory;
     _serviceDiscoveryProvider = serviceDiscoveryProvider;
 }
        /// <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);
        }
Example #4
0
        /// <summary>
        /// Invokes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="discoveryProvider">The discovery provider.</param>
        public async Task Invoke(HttpContext context, ISwaggerServiceDiscoveryProvider discoveryProvider)
        {
            (string Url, SwaggerEndPointOptions EndPoint) = await GetEndPoint(context.Request.Path, discoveryProvider);

            HttpClient httpClient = _httpClientFactory.CreateClient();

            AddHeaders(httpClient);
            string content = await httpClient.GetStringAsync(Url);

            string hostName = EndPoint.HostOverride ?? context.Request.Host.Value.RemoveSlashFromEnd();
            IEnumerable <ReRouteOptions> reRouteOptions = _reRoutes.Value
                                                          .ExpandConfig(EndPoint)
                                                          .GroupByPaths();

            content = _transformer.Transform(content, reRouteOptions, hostName);
            content = await ReconfigureUpstreamSwagger(context, content);

            await context.Response.WriteAsync(content);
        }