Example #1
0
 public ConsulConfigurationSource(ConfigurationOptions options, IConsulHandler consulHandler,
                                  IConsulKvEndpoint consulKvEndpoint)
 {
     Options          = options;
     ConsulKvEndpoint = consulKvEndpoint;
     ConsulHandler    = consulHandler;
 }
Example #2
0
        public static IConfigurationBuilder AddConsul(
            this IConfigurationBuilder builder,
            ConfigurationOptions configurationOptions,
            IConsulHandler consulHandler,
            IConsulKvEndpoint consulKvEndpoint
            )
        {
            var consulConfigurationSource =
                new ConsulConfigurationSource(configurationOptions, consulHandler, consulKvEndpoint);

            return(builder.Add(consulConfigurationSource));
        }
Example #3
0
        private static Action <WebHostBuilderContext, IServiceCollection> ConfigureServices(
            NethiumConfig nethiumConfig, IConsulHandler consulHandler, IConsulBuilder consulBuilder
            ) =>
        (context, collection) =>
        {
            var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(context.Configuration["Nethium:ServerSecretKey"] ?? throw new ArgumentNullException("Nethium:ServerSecretKey", "ServerSecretKey not specified in config")));

            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = signingKey,

                RequireExpirationTime = true,
                ValidateLifetime      = true,

                ValidateAudience = false,
                ValidateActor    = false,
                ValidateIssuer   = false
            };

            collection.AddSingleton(
                new JwtHeader(new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256)));

            collection.AddHttpContextAccessor();
            collection.AddHttpClient();
            collection.Configure <ServerIdentifier>(context.Configuration.GetSection("Nethium"));
            collection.Configure <ConfigurationOptions>(o =>
            {
                o.Prefix = nethiumConfig.Prefix !;
                o.ConfigurationPrefix = nethiumConfig.ConfigurationPrefix;
                o.Separator           = nethiumConfig.Separator !;
                o.Watch      = nethiumConfig.Watch;
                o.AutoReload = nethiumConfig.AutoReload ?? false;
            });
            collection.AddSingleton(consulHandler);
            collection.AddSingleton(consulBuilder);
            collection.AddScoped <IAuthHandler, StubAuthHandler>();
            collection.AddScoped <ISwaggerStubHandler, SwaggerStubHandler>();
            collection.AddScoped <IConsulKvEndpoint, ConsulKvEndpoint>();
            collection.AddScoped <IConsulServiceEndpoint, ConsulServiceEndpoint>();
            collection.AddScoped <IServicesLocator, ConsulServicesLocator>();
            collection.AddScoped(typeof(IServiceFactory <>), typeof(SwaggerServiceFactory <>));
            collection.AddScoped(typeof(IServiceChooseStrategy <>), typeof(NearestStrategy <>));
            collection.AddHostedService <NethiumHostedService>();
            collection.AddOpenApiDocument();
            collection
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => { options.TokenValidationParameters = tokenValidationParameters; });
        };
Example #4
0
 private static Action <WebHostBuilderContext, IConfigurationBuilder> ConfigureAppConfiguration(
     ConfigurationOptions configurationOptions,
     NethiumConfig nethiumConfig,
     IConsulHandler consulHandler,
     IConsulBuilder consulBuilder
     ) =>
 (context, configurationBuilder) =>
 {
     var consulKvEndpoint = new ConsulKvEndpoint(consulBuilder,
                                                 nethiumConfig.LoggerFactory?.CreateLogger <ConsulKvEndpoint>())
     {
         WatchExceptionHandler = WatchExceptionHandler
     };
     configurationBuilder
     .AddJsonFile("nethium.json", true, true)
     .AddEnvironmentVariables("NETHIUM_")
     .Add(new ConsulConfigurationSource(configurationOptions, consulHandler, consulKvEndpoint));
 };
Example #5
0
        public static IConfigurationBuilder AddConsul(
            this IConfigurationBuilder builder,
            ConfigurationOptions configurationOptions,
            IConsulHandler consulHandler,
            ILoggerFactory?loggerFactory
            )
        {
            var consulBuilder  = new ConsulBuilder(consulHandler);
            var consulEndpoint = new ConsulKvEndpoint(consulBuilder, loggerFactory.CreateLogger <ConsulKvEndpoint>())
            {
                WatchExceptionHandler = async(kv, e, key, logger, ct) =>
                {
                    await Task.Delay(10000, ct);

                    logger.LogWarning($"Restart watching {key}");
                    return(true);
                }
            };

            return(builder.AddConsul(configurationOptions, consulHandler, consulEndpoint));
        }
        public ConsulConfigurationProvider(ConfigurationOptions options, IConsulHandler consulHandler,
                                           IConsulKvEndpoint consulKvEndpoint)
        {
            _options          = options;
            _consulKvEndpoint = consulKvEndpoint;
            _consulHandler    = consulHandler;
            AutoReload        = _options.AutoReload;
            if (options.Watch != null)
            {
                ChangeToken.OnChange(
                    () => _consulKvEndpoint.Watch(_options.Watch !, _consulHandler.CancellationToken),
                    async() =>
                {
                    if (!AutoReload)
                    {
                        return;
                    }

                    await LoadAsync();
                    OnReload();
                });
            }
        }
Example #7
0
 public static IConfigurationBuilder AddConsul(
     this IConfigurationBuilder builder,
     ConfigurationOptions configurationOptions,
     IConsulHandler consulHandler
     ) =>
 builder.AddConsul(configurationOptions, consulHandler, (ILoggerFactory?)null);
Example #8
0
 public ConsulBuilder(IConsulHandler consulHandler) => ConsulHandler = consulHandler;