Beispiel #1
0
        /// <summary>
        /// 查询网关配置数据
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <returns></returns>
        private async Task <FileConfiguration> GetGatewayData(int gatewayId)
        {
            var fileConfig = new FileConfiguration();
            var configInfo = await _configDbRepository.GetFirstAsync(it => it.GatewayId == gatewayId);

            if (configInfo != null)
            {
                // config
                var fgc = new FileGlobalConfiguration
                {
                    BaseUrl          = configInfo.BaseUrl,
                    DownstreamScheme = configInfo.DownstreamScheme,
                    RequestIdKey     = configInfo.RequestIdKey,
                };
                if (!string.IsNullOrWhiteSpace(configInfo.HttpHandlerOptions))
                {
                    fgc.HttpHandlerOptions = Json.ToObject <FileHttpHandlerOptions>(configInfo.HttpHandlerOptions);
                }
                if (!string.IsNullOrWhiteSpace(configInfo.LoadBalancerOptions))
                {
                    fgc.LoadBalancerOptions = Json.ToObject <FileLoadBalancerOptions>(configInfo.LoadBalancerOptions);
                }
                if (!string.IsNullOrWhiteSpace(configInfo.QoSOptions))
                {
                    fgc.QoSOptions = Json.ToObject <FileQoSOptions>(configInfo.QoSOptions);
                }
                if (!string.IsNullOrWhiteSpace(configInfo.RateLimitOptions))
                {
                    fgc.RateLimitOptions = Json.ToObject <FileRateLimitOptions>(configInfo.RateLimitOptions);
                }
                if (!string.IsNullOrWhiteSpace(configInfo.ServiceDiscoveryProvider))
                {
                    fgc.ServiceDiscoveryProvider = Json.ToObject <FileServiceDiscoveryProvider>(configInfo.ServiceDiscoveryProvider);
                }
                fileConfig.GlobalConfiguration = fgc;
                // reroutes
                var reRouteResult = await _routeDbRepository.GetListAsync(it => it.GatewayId == configInfo.GatewayId && it.State == 1);

                if (reRouteResult.Count > 0)
                {
                    var reroutelist = new List <FileReRoute>();
                    foreach (var model in reRouteResult)
                    {
                        var m = new FileReRoute()
                        {
                            UpstreamHost           = model.UpstreamHost,
                            UpstreamPathTemplate   = model.UpstreamPathTemplate,
                            DownstreamPathTemplate = model.DownstreamPathTemplate,
                            DownstreamScheme       = model.DownstreamScheme,
                            ServiceName            = model.ServiceName,
                            Priority     = model.Priority,
                            RequestIdKey = model.RequestIdKey,
                            Key          = model.Key,
                            Timeout      = model.Timeout,
                        };
                        if (!string.IsNullOrWhiteSpace(model.UpstreamHttpMethod))
                        {
                            m.UpstreamHttpMethod = Json.ToObject <List <string> >(model.UpstreamHttpMethod);
                        }
                        if (!string.IsNullOrWhiteSpace(model.DownstreamHostAndPorts))
                        {
                            m.DownstreamHostAndPorts = Json.ToObject <List <FileHostAndPort> >(model.DownstreamHostAndPorts);
                        }
                        if (!string.IsNullOrWhiteSpace(model.SecurityOptions))
                        {
                            m.SecurityOptions = Json.ToObject <FileSecurityOptions>(model.SecurityOptions);
                        }
                        if (!string.IsNullOrWhiteSpace(model.CacheOptions))
                        {
                            m.FileCacheOptions = Json.ToObject <FileCacheOptions>(model.CacheOptions);
                        }
                        if (!string.IsNullOrWhiteSpace(model.HttpHandlerOptions))
                        {
                            m.HttpHandlerOptions = Json.ToObject <FileHttpHandlerOptions>(model.HttpHandlerOptions);
                        }
                        if (!string.IsNullOrWhiteSpace(model.AuthenticationOptions))
                        {
                            m.AuthenticationOptions = Json.ToObject <FileAuthenticationOptions>(model.AuthenticationOptions);
                        }
                        if (!string.IsNullOrWhiteSpace(model.RateLimitOptions))
                        {
                            m.RateLimitOptions = Json.ToObject <FileRateLimitRule>(model.RateLimitOptions);
                        }
                        if (!string.IsNullOrWhiteSpace(model.LoadBalancerOptions))
                        {
                            m.LoadBalancerOptions = Json.ToObject <FileLoadBalancerOptions>(model.LoadBalancerOptions);
                        }
                        if (!string.IsNullOrWhiteSpace(model.QoSOptions))
                        {
                            m.QoSOptions = Json.ToObject <FileQoSOptions>(model.QoSOptions);
                        }
                        if (!string.IsNullOrWhiteSpace(model.DelegatingHandlers))
                        {
                            m.DelegatingHandlers = Json.ToObject <List <string> >(model.DelegatingHandlers);
                        }
                        reroutelist.Add(m);
                    }
                    fileConfig.ReRoutes = reroutelist;
                }
            }
            return(fileConfig);
        }