Beispiel #1
0
        public static IConfigurationBuilder AddConsul(
            this IConfigurationBuilder builder,
            ConfigurationOptions options,
            Action <IConsulHandler> configure
            )
        {
            var handler = new ConsulHandler();

            configure.Invoke(handler);
            return(builder.AddConsul(options, handler));
        }
Beispiel #2
0
        public static IWebHostBuilder UseNethium(this IWebHostBuilder builder,
                                                 Action <NethiumConfig> configureNethiumOption)
        {
            var nethiumConfig = new NethiumConfig();

            configureNethiumOption(nethiumConfig);
            nethiumConfig.CancellationToken ??= builder.GetShutdownToken();
            var bootstrapConfig = nethiumConfig.BootstrapConfig;
            var logger          = nethiumConfig.LoggerFactory?.CreateLogger <NethiumConfig>();

            if (bootstrapConfig != null)
            {
                nethiumConfig.Prefix ??= bootstrapConfig["Nethium:Consul:Prefix"] ?? throw new ArgumentNullException("Nethium:Consul:Prefix", "Consul:Prefix not specified in config");
                nethiumConfig.ConfigurationPrefix ??= bootstrapConfig["Nethium:Consul:ConfigurationPrefix"] ??
                nethiumConfig.Prefix;
                nethiumConfig.Separator ??= bootstrapConfig["Nethium:Consul:Separator"];
                nethiumConfig.Watch ??= bootstrapConfig["Nethium:Consul:Watch"];
            }

            var configurationOptions = new ConfigurationOptions
            {
                Prefix = nethiumConfig.Prefix !,
                ConfigurationPrefix = nethiumConfig.ConfigurationPrefix,
                Separator           = nethiumConfig.Separator !,
                Watch      = nethiumConfig.Watch,
                AutoReload = nethiumConfig.AutoReload ?? false
            };

            var consulHandler = new ConsulHandler
            {
                ClientConfigurationOptions = ClientConfiguration(bootstrapConfig, nethiumConfig),
                CancellationToken          = nethiumConfig.CancellationToken
            };
            var consulBuilder             = new ConsulBuilder(consulHandler);
            var consulClientConfiguration = new ConsulClientConfiguration();

            consulHandler.ClientConfigurationOptions.Invoke(consulClientConfiguration);
            foreach (var pair in bootstrapConfig.AsEnumerable())
            {
                logger?.LogInformation(pair.Key + ": " + pair.Value);
            }
            logger?.LogInformation(bootstrapConfig?["Nethium:Consul:Watch"]);
            logger?.LogInformation($"Using consul at address {consulClientConfiguration.Address} " +
                                   $"{(string.IsNullOrEmpty(consulClientConfiguration.Datacenter) ? "" : "datacenter " + consulClientConfiguration.Datacenter)}");
            logger?.LogInformation($"Consul config prefix {configurationOptions.Prefix}" +
                                   $"{(string.IsNullOrEmpty(configurationOptions.ConfigurationPrefix) ? "" : " (" + configurationOptions.ConfigurationPrefix + ") ")}" +
                                   $"{(string.IsNullOrEmpty(configurationOptions.Watch) ? "" : "watching " + configurationOptions.Watch + (configurationOptions.AutoReload ? " auto-reload" : " no auto-reload"))}");
            if (nethiumConfig.HostConfiguration)
            {
                logger?.LogInformation("Injecting Nethium config provider to host configuration");
                var consulKvEndpoint = new ConsulKvEndpoint(consulBuilder,
                                                            nethiumConfig.LoggerFactory?.CreateLogger <ConsulKvEndpoint>())
                {
                    WatchExceptionHandler = WatchExceptionHandler
                };
                var configurationBuilder = new ConfigurationBuilder();
                if (bootstrapConfig != null)
                {
                    configurationBuilder.AddConfiguration(bootstrapConfig);
                }

                var configurationRoot = configurationBuilder
                                        .AddConsul(configurationOptions, consulHandler, consulKvEndpoint)
                                        .Build();

                builder
                .UseConfiguration(configurationRoot);
            }

            builder
            .ConfigureServices(ConfigureServices(nethiumConfig, consulHandler, consulBuilder))
            .ConfigureAppConfiguration(ConfigureAppConfiguration(configurationOptions, nethiumConfig, consulHandler,
                                                                 consulBuilder));
            return(builder);
        }