Esempio n. 1
0
        /// <summary>
        /// Expands versions from one endpoint to more ReRoute options.
        /// </summary>
        /// <param name="reRoutes">The re routes.</param>
        /// <param name="endPoint">The end point.</param>
        public static IEnumerable <ReRouteOptions> ExpandConfig(
            this IEnumerable <ReRouteOptions> reRoutes,
            SwaggerEndPointOptions endPoint)
        {
            var reRouteOptions = reRoutes.Where(p => p.SwaggerKey == endPoint.Key).ToList();

            if (string.IsNullOrWhiteSpace(endPoint.VersionPlaceholder))
            {
                return(reRouteOptions);
            }

            var versionReRouteOptions = reRouteOptions.Where(x =>
                                                             x.DownstreamPathTemplate.Contains(endPoint.VersionPlaceholder) ||
                                                             x.UpstreamPathTemplate.Contains(endPoint.VersionPlaceholder)).ToList();

            versionReRouteOptions.ForEach(o => reRouteOptions.Remove(o));
            foreach (ReRouteOptions reRouteOption in versionReRouteOptions)
            {
                IEnumerable <ReRouteOptions> versionMappedReRouteOptions = endPoint.Config.Select(c => new ReRouteOptions()
                {
                    SwaggerKey             = reRouteOption.SwaggerKey,
                    DownstreamPathTemplate =
                        reRouteOption.DownstreamPathTemplate.Replace(endPoint.VersionPlaceholder,
                                                                     c.Version),
                    UpstreamHttpMethod   = reRouteOption.UpstreamHttpMethod,
                    UpstreamPathTemplate =
                        reRouteOption.UpstreamPathTemplate.Replace(endPoint.VersionPlaceholder,
                                                                   c.Version),
                    VirtualDirectory = reRouteOption.VirtualDirectory
                });
                reRouteOptions.AddRange(versionMappedReRouteOptions);
            }

            return(reRouteOptions);
        }
Esempio n. 2
0
        /// <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 (string Url, SwaggerEndPointOptions EndPoint) GetEndPoint(string path)
        {
            (string Version, string Key)endPointInfo = GetEndPointInfo(path);
            SwaggerEndPointOptions endPoint = _swaggerEndPoints.Value[$"/{endPointInfo.Key}"];
            string url = endPoint.Config.FirstOrDefault(x => x.Version == endPointInfo.Version)?.Url;

            return(url, endPoint);
        }
Esempio n. 3
0
        private (string version, SwaggerEndPointOptions endpoint) GetEndPoint(
            string path,
            ISwaggerEndPointProvider swaggerEndPointRepository)
        {
            (string Version, string Key)endPointInfo = GetEndPointInfo(path);
            SwaggerEndPointOptions endPoint = swaggerEndPointRepository.GetByKey(endPointInfo.Key);

            return(endPointInfo.Version, endPoint);
        }
Esempio n. 4
0
        private JObject GetServiceDocs(RouteOptions route)
        => _memoryCache.GetOrCreate(route.SwaggerKey, (e) =>
        {
            SwaggerEndPointOptions endpoint = _swaggerEndPointRepository.GetByKey(route.SwaggerKey);
            string docs = _downstreamSwaggerDocs.GetSwaggerJsonAsync(route, endpoint).Result;
            e.SetSlidingExpiration(TimeSpan.FromMinutes(5));

            return(JObject.Parse(docs));
        });
        public ManageSwaggerEndpointData GetSwaggerEndpoint(SwaggerEndPointOptions endPoint, string version)
        {
            var lookupKey    = $"{endPoint.Key}_{version}";
            var endpointData = new ManageSwaggerEndpointData();

            if (_endpointDatas.ContainsKey(lookupKey))
            {
                endpointData = _endpointDatas[lookupKey];
            }

            return(endpointData);
        }
        /// <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);
        }
        /// <inheritdoc />
        public async Task <string> GetSwaggerJsonAsync(
            RouteOptions route,
            SwaggerEndPointOptions endPoint,
            string docsVersion = null)
        {
            string url = await GetUrlAsync(route, endPoint, docsVersion);

            HttpClient httpClient = _httpClientFactory.CreateClient();

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

            return(await httpClient.GetStringAsync(url));
        }
Esempio n. 9
0
        private string GetServerName(HttpContext context, SwaggerEndPointOptions endPoint)
        {
            string serverName;

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

            return(serverName);
        }
        public bool DoDownstreamSwaggerEndpoint(HttpContext httpContext, string version, SwaggerEndPointOptions endPoint)
        {
            var myEndpointConfiguration = _endpointConfigurationRepository.GetSwaggerEndpoint(endPoint, version);

            if (!myEndpointConfiguration.IsPublished)
            {
                httpContext.Response.StatusCode = 404;
                httpContext.Response.WriteAsync("This enpoint is under development, please come back later.");
            }

            return(myEndpointConfiguration.IsPublished);
        }