public static OcelotConfiguration GetOcelotConfiguration(this IConfiguration configuration)
        {
            var ocelotConfiguration = new OcelotConfiguration();

            configuration.Bind(nameof(OcelotConfiguration), ocelotConfiguration);
            return(ocelotConfiguration);
        }
示例#2
0
        public void should_call_scoped_data_repository_correctly()
        {
            var config = new OcelotConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "");

            var downstreamReRoute = new DownstreamReRouteBuilder()
                                    .WithDownstreamPathTemplate("any old string")
                                    .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                                    .Build();

            this.Given(x => x.GivenTheDownStreamRouteFinderReturns(
                           new DownstreamRoute(
                               new List <PlaceholderNameAndValue>(),
                               new ReRouteBuilder()
                               .WithDownstreamReRoute(downstreamReRoute)
                               .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                               .Build())))
            .And(x => GivenTheFollowingConfig(config))
            .When(x => x.WhenICallTheMiddleware())
            .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
            .BDDfy();
        }
示例#3
0
        private async Task <Response <IOcelotConfiguration> > SetUpConfiguration(FileConfiguration fileConfiguration)
        {
            var response = await _configurationValidator.IsValid(fileConfiguration);

            if (response.Data.IsError)
            {
                return(new ErrorResponse <IOcelotConfiguration>(response.Data.Errors));
            }

            var reRoutes = new List <ReRoute>();

            foreach (var reRoute in fileConfiguration.ReRoutes)
            {
                var downstreamReRoute = SetUpDownstreamReRoute(reRoute, fileConfiguration.GlobalConfiguration);

                var ocelotReRoute = SetUpReRoute(reRoute, downstreamReRoute);

                reRoutes.Add(ocelotReRoute);
            }

            foreach (var aggregate in fileConfiguration.Aggregates)
            {
                var ocelotReRoute = SetUpAggregateReRoute(reRoutes, aggregate, fileConfiguration.GlobalConfiguration);
                reRoutes.Add(ocelotReRoute);
            }

            var serviceProviderConfiguration = _serviceProviderConfigCreator.Create(fileConfiguration.GlobalConfiguration);

            var config = new OcelotConfiguration(reRoutes, _adminPath.Path, serviceProviderConfiguration, fileConfiguration.GlobalConfiguration.RequestIdKey);

            return(new OkResponse <IOcelotConfiguration>(config));
        }
 public CustomTokenRefreshService(IServiceFinder <ConsulService> serviceFinder, ILoadBalancer loadBalancer, IRedisCache cache, OcelotConfiguration options, IConfiguration configuration)
 {
     _serviceFinder = serviceFinder;
     _loadBalancer  = loadBalancer ?? new WeightRoundBalancer();
     _options       = options;
     _cache         = cache;
     _conf          = configuration;
 }
示例#5
0
        private void GivenTheConfigReturnsError()
        {
            var config = new OcelotConfiguration(null, null, null, null);

            var response = new Ocelot.Responses.ErrorResponse <IOcelotConfiguration>(new FakeError());

            _provider
            .Setup(x => x.Get()).ReturnsAsync(response);
        }
示例#6
0
        public void DownstreamException()
        {
            var config = new OcelotConfiguration(null, null, null, null);

            this.Given(_ => GivenAnExceptionWillBeThrownDownstream())
            .And(_ => GivenTheConfigurationIs(config))
            .When(_ => WhenICallTheMiddleware())
            .Then(_ => ThenTheResponseIsError())
            .BDDfy();
        }
示例#7
0
        public void NoDownstreamException()
        {
            var config = new OcelotConfiguration(null, null, null, null);

            this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
            .And(_ => GivenTheConfigurationIs(config))
            .When(_ => WhenICallTheMiddleware())
            .Then(_ => ThenTheResponseIsOk())
            .And(_ => TheAspDotnetRequestIdIsSet())
            .BDDfy();
        }
示例#8
0
        public void ShouldNotSetRequestId()
        {
            var config = new OcelotConfiguration(null, null, null, null);

            this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
            .And(_ => GivenTheConfigurationIs(config))
            .When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
            .Then(_ => ThenTheResponseIsOk())
            .And(_ => TheRequestIdIsNotSet())
            .BDDfy();
        }
示例#9
0
        public async Task <IActionResult> Get()
        {
            try
            {
                if (_executor.Initialized())
                {
                    return(Ok("The is already initialized."));
                }

                _configuration.ValidateConfigurationFiles();

                var configuration = await _configuration.GetMicroservicesConfiguration();

                foreach (var gateway in configuration.Gateways)
                {
                    var ocelot = new OcelotConfiguration
                    {
                        //GlobalConfiguration = new GlobalConfiguration
                        //{
                        //    RequestIdKey = "OcRequestId",
                        //    AdministrationPath = "/administration"
                        //},
                        ReRoutes = new List <Reroute>()
                    };

                    var fileName = $"{gateway.Name}.json";

                    await _configuration.WriteOcelotConfigurationFile(fileName, ocelot);

                    _executor.Launch(gateway, fileName);

                    var microservices = configuration.Microservices.Where(x => x.Gateway == gateway.Name).ToList();

                    var tasks = new List <Task>();

                    foreach (var microservice in microservices)
                    {
                        _executor.Launch(microservice);

                        tasks.Add(_configuration.UpdateOcelotConfiguration(ocelot, microservice));
                    }

                    await Task.WhenAll(tasks);

                    await _configuration.WriteOcelotConfigurationFile(fileName, ocelot);
                }

                return(Ok("Services launched successfully."));
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
示例#10
0
        public void should_set_configuration()
        {
            var fileConfig = new FileConfiguration();
            var config     = new OcelotConfiguration(new List <ReRoute>(), string.Empty);

            this.Given(x => GivenTheFollowingConfiguration(fileConfig))
            .And(x => GivenTheRepoReturns(new OkResponse()))
            .And(x => GivenTheCreatorReturns(new OkResponse <IOcelotConfiguration>(config)))
            .When(x => WhenISetTheConfiguration())
            .Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly())
            .BDDfy();
        }
示例#11
0
        public void should_fix_issue_142()
        {
            var consulPort    = 8500;
            var configuration = new FileConfiguration
            {
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
                    {
                        Host = "localhost",
                        Port = consulPort
                    }
                }
            };

            var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";

            var serviceProviderConfig = new ServiceProviderConfigurationBuilder()
                                        .WithServiceDiscoveryProviderHost("localhost")
                                        .WithServiceDiscoveryProviderPort(consulPort)
                                        .Build();

            var reRoute = new ReRouteBuilder()
                          .WithDownstreamPathTemplate("/status")
                          .WithUpstreamTemplatePattern("^(?i)/cs/status/$")
                          .WithDownstreamScheme("http")
                          .WithDownstreamHost("localhost")
                          .WithDownstreamPort(51779)
                          .WithUpstreamPathTemplate("/cs/status")
                          .WithUpstreamHttpMethod(new List <string> {
                "Get"
            })
                          .WithReRouteKey("/cs/status|Get")
                          .WithHttpHandlerOptions(new HttpHandlerOptions(true, false))
                          .Build();

            var reRoutes = new List <ReRoute> {
                reRoute
            };

            var config = new OcelotConfiguration(reRoutes, null, serviceProviderConfig);

            this.Given(x => GivenTheConsulConfigurationIs(config))
            .And(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
            .And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "/status", 200, "Hello from Laura"))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/cs/status"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
            .BDDfy();
        }
        public static IApplicationBuilder UseGspGateway(
            this IApplicationBuilder applicationBuilder,
            OcelotConfiguration ocelotConfiguration)
        {
            if (ocelotConfiguration.IsOcelotSwaggerEnabled)
            {
                applicationBuilder.UseSwaggerForOcelotUI();
            }

            applicationBuilder.UseAuthentication();
            applicationBuilder.UseStaticFiles();
            applicationBuilder.UseOcelot().Wait();

            return(applicationBuilder);
        }
        public static IServiceCollection AddGspGateway(
            this IServiceCollection services,
            IConfiguration configuration,
            OcelotConfiguration ocelotConfiguration)
        {
            services.AddOcelot()
            .AddAppConfiguration();

            if (ocelotConfiguration.IsOcelotSwaggerEnabled)
            {
                services.AddSwaggerForOcelot(configuration);
            }

            services.AddJwtBearerAuthentication(configuration);

            return(services);
        }
 public static IOcelotBuilder WithConfigurationRepository(this IOcelotBuilder builder, string configKey = "Ocelot.Extensions:Configuration")
 {
     if (!_withConfigurationRepository)
     {
         builder.Services.AddHttpClient();
         builder.WithRouteExtensions();
         var config = new OcelotConfiguration();
         builder.Configuration.Bind(configKey, config);
         builder.Services.AddSingleton(config);
         builder.Services.AddSingleton <IFileConfigurationRepositoryExtended>(
             sp =>
             new SnapshotConfigurationRepositoryExtended(
                 new DiskFileConfigurationRepositoryExtended(sp.GetRequiredService <IHostingEnvironment>())));
         builder.Services.AddHostedService <OcelotConfigurationSyncAgent>();
         _withConfigurationRepository = true;
     }
     return(builder);
 }
示例#15
0
 private void GivenTheConsulConfigurationIs(OcelotConfiguration config)
 {
     _config = config;
 }
示例#16
0
 public Startup(IConfiguration configuration)
 {
     Configuration        = configuration;
     _ocelotConfiguration = configuration.GetOcelotConfiguration();
 }
 private void GivenTheConfigurationIs(List<ReRoute> reRoutesConfig, string adminPath, ServiceProviderConfiguration serviceProviderConfig)
 {
     _reRoutesConfig = reRoutesConfig;
     _config = new OcelotConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig);
 }