public Response AddOrReplace(IInternalConfiguration internalConfiguration)
        {
            lock (LockObject)
            {
                _internalConfiguration = internalConfiguration;
            }

            _changeTokenSource.Activate();
            return(new OkResponse());
        }
        public Task <Response> Set(FileConfiguration fileConfiguration)
        {
            string jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration, Formatting.Indented);

            lock (_lock)
            {
                if (System.IO.File.Exists(_environmentFilePath))
                {
                    System.IO.File.Delete(_environmentFilePath);
                }

                System.IO.File.WriteAllText(_environmentFilePath, jsonConfiguration);

                if (System.IO.File.Exists(_ocelotFilePath))
                {
                    System.IO.File.Delete(_ocelotFilePath);
                }

                System.IO.File.WriteAllText(_ocelotFilePath, jsonConfiguration);
            }

            _changeTokenSource.Activate();
            return(Task.FromResult <Response>(new OkResponse()));
        }
Beispiel #3
0
 private void GivenIActivateTheChangeTokenSource()
 {
     _source.Activate();
 }
Beispiel #4
0
        public async Task <Response> Set(FileConfiguration fileConfiguration)
        {
            if (fileConfiguration == null)
            {
                throw new Exception("未监测到任何可用的配置信息");
            }
            if (string.IsNullOrEmpty(fileConfiguration.GlobalConfiguration.BaseUrl))
            {
                throw new Exception("BaseUrl不可为空");
            }

            var _gatewayDbContext = _serviceProvider.CreateScope().ServiceProvider.GetRequiredService <GatewayDbContext>();

            var dbGlobalConfigs = _gatewayDbContext.GlobalConfiguration.Where(x => x.Enable).ToList();

            dbGlobalConfigs.ForEach(x =>
            {
                x.Enable = false;
                x.ReRoutes.ForEach(m => m.Enable = false);
            });
            _gatewayDbContext.GlobalConfiguration.UpdateRange(dbGlobalConfigs);

            // Globalconfig
            var dbGlobalConfig = new GlobalConfiguration();

            dbGlobalConfig.Id                       = Guid.NewGuid().ToString("N");
            dbGlobalConfig.CreatedTime              = DateTime.Now;
            dbGlobalConfig.Enable                   = true;
            dbGlobalConfig.BaseUrl                  = fileConfiguration.GlobalConfiguration.BaseUrl;
            dbGlobalConfig.DownstreamScheme         = fileConfiguration.GlobalConfiguration.DownstreamScheme;
            dbGlobalConfig.RequestIdKey             = fileConfiguration.GlobalConfiguration.RequestIdKey;
            dbGlobalConfig.DownstreamHttpVersion    = fileConfiguration.GlobalConfiguration.DownstreamHttpVersion;
            dbGlobalConfig.HttpHandlerOptions       = JsonConvert.SerializeObject(fileConfiguration.GlobalConfiguration.HttpHandlerOptions);
            dbGlobalConfig.LoadBalancerOptions      = JsonConvert.SerializeObject(fileConfiguration.GlobalConfiguration.LoadBalancerOptions);
            dbGlobalConfig.QoSOptions               = JsonConvert.SerializeObject(fileConfiguration.GlobalConfiguration.QoSOptions);
            dbGlobalConfig.ServiceDiscoveryProvider = JsonConvert.SerializeObject(fileConfiguration.GlobalConfiguration.ServiceDiscoveryProvider);
            dbGlobalConfig.RateLimitOptions         = JsonConvert.SerializeObject(fileConfiguration.GlobalConfiguration.RateLimitOptions);
            dbGlobalConfig.ReRoutes                 = new List <ReRoute>();

            // Reroutes
            foreach (var model in fileConfiguration.ReRoutes)
            {
                var dbReroute = new ReRoute();
                dbReroute.Id = Guid.NewGuid().ToString("N");
                dbReroute.GlobalConfigurationId = dbGlobalConfig.Id;
                dbReroute.CreatedTime           = dbGlobalConfig.CreatedTime;
                dbReroute.Enable = true;
                dbReroute.AuthenticationOptions  = JsonConvert.SerializeObject(model.AuthenticationOptions);
                dbReroute.FileCacheOptions       = JsonConvert.SerializeObject(model.FileCacheOptions);
                dbReroute.DelegatingHandlers     = JsonConvert.SerializeObject(model.DelegatingHandlers);
                dbReroute.LoadBalancerOptions    = JsonConvert.SerializeObject(model.LoadBalancerOptions);
                dbReroute.QoSOptions             = JsonConvert.SerializeObject(model.QoSOptions);
                dbReroute.DownstreamHostAndPorts = JsonConvert.SerializeObject(model.DownstreamHostAndPorts);
                dbReroute.HttpHandlerOptions     = JsonConvert.SerializeObject(model.HttpHandlerOptions);
                dbReroute.RateLimitOptions       = JsonConvert.SerializeObject(model.RateLimitOptions);
                dbReroute.DownstreamPathTemplate = model.DownstreamPathTemplate;
                dbReroute.DownstreamScheme       = model.DownstreamScheme;
                dbReroute.Key                   = model.Key ?? "";
                dbReroute.Priority              = model.Priority;
                dbReroute.RequestIdKey          = model.RequestIdKey ?? "";
                dbReroute.ServiceName           = model.ServiceName ?? "";
                dbReroute.UpstreamHost          = model.UpstreamHost ?? "";
                dbReroute.UpstreamHttpMethod    = JsonConvert.SerializeObject(model.UpstreamHttpMethod);
                dbReroute.UpstreamPathTemplate  = model.UpstreamPathTemplate;
                dbReroute.DownstreamHttpVersion = model.DownstreamHttpVersion;
                dbGlobalConfig.ReRoutes.Add(dbReroute);
            }

            // Aggregates
            var dbAggregates = _gatewayDbContext.Aggregates.Where(x => x.Enable).ToList();

            dbAggregates.ForEach(x => x.Enable = false);
            _gatewayDbContext.Aggregates.UpdateRange(dbAggregates);
            dbAggregates = new List <Aggregates>();
            foreach (var aggregate in fileConfiguration.Aggregates)
            {
                var dbAggregate = new Aggregates();
                dbAggregate.Id                     = Guid.NewGuid().ToString("N");
                dbAggregate.CreatedTime            = dbGlobalConfig.CreatedTime;
                dbAggregate.Enable                 = true;
                dbAggregate.ReRouteKeys            = JsonConvert.SerializeObject(aggregate.ReRouteKeys);
                dbAggregate.ReRouteKeysConfig      = JsonConvert.SerializeObject(aggregate.ReRouteKeysConfig);
                dbAggregate.UpstreamPathTemplate   = aggregate.UpstreamPathTemplate;
                dbAggregate.UpstreamHost           = aggregate.UpstreamHost;
                dbAggregate.ReRouteIsCaseSensitive = aggregate.ReRouteIsCaseSensitive;
                dbAggregate.Aggregator             = aggregate.Aggregator;
                dbAggregate.Priority               = aggregate.Priority;
                dbAggregates.Add(dbAggregate);
            }
            await _gatewayDbContext.GlobalConfiguration.AddAsync(dbGlobalConfig);

            await _gatewayDbContext.Aggregates.AddRangeAsync(dbAggregates);

            var changeCount = await _gatewayDbContext.SaveChangesAsync();

            _changeTokenSource.Activate();
            return(await Task.FromResult(new OkResponse()));
        }