コード例 #1
0
        /// <inheritdoc />
        protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
        {
            var options = Options.Value;

            services.AddOptions <ScribanOptions>()
            .ValidateDataAnnotations();

            services.TryAddSingleton <ITemplateEngine, ScribanTemplateEngine>();
            services.AddSingleton <ITemplatesStore>(
                sp => new FileSystemTemplatesStore(options.Templates.Store.FileSystem.Path));

            if (options.Subscriptions.Store.UseInMemoryStore)
            {
                services.AddSingleton <ISubscriptionsStore, InMemorySubscriptionsStore>();
            }
            else if (options.Subscriptions.Store.Consul != null)
            {
                services.AddConsulSubscriptions(Configuration.CreateBinder <ConsulOptions>(
                                                    "Alerting:Subscriptions:Store:Consul", required: true));
            }
            else if (options.Subscriptions.Store.Faster != null)
            {
                services.AddFasterSubscriptions(Configuration.CreateBinder <FasterStoreOptions>(
                                                    "Alerting:Subscriptions:Store:Faster", required: true));
            }

            if (options.Channels.Store.Predefined != null)
            {
                services.AddSingleton <IChannelStore>(sp => new InMemoryChannelStore(
                                                          options.Channels.Store.Predefined));
            }
        }
コード例 #2
0
        /// <inheritdoc />
        protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
        {
            services.AddMetrics(s =>
            {
                s.Configuration.Configure(cfg =>
                {
                    cfg.AddEnvTag();
                    cfg.DefaultContextLabel = Options.Value.Prefix;
                });

                s.OutputMetrics.AsPrometheusPlainText();
            });

            services.AddMetricsEndpoints(o =>
            {
                o.MetricsTextEndpointEnabled = false;
                o.MetricsEndpointEnabled     = true;

                o.MetricsEndpointOutputFormatter = o.MetricsOutputFormatters
                                                   .OfType <MetricsPrometheusTextOutputFormatter>()
                                                   .First();
            });

            services.AddTransient <IMetricsCollector, ActiveSubscriptionsMetricsCollector>();
        }
コード例 #3
0
        public static IAppFeatureCollection AddWhen<TFeature, TOptions>(this IAppFeatureCollection builder,
            Func<bool> predicate, Action<TOptions> configure)
            where TFeature : class, IAppFeature<TOptions>
            where TOptions : class, new()
        {
            if (!predicate())
                return builder;

            var options = new AppFeatureOptions<TOptions>(configure);
            var feature = ActivatorUtilities.CreateInstance<TFeature>(builder.ServiceProvider, (IOptions<TOptions>) options);
            var enabled = feature.Configure(builder.Services, builder);

            if (enabled)
            {
                var subscriptions = builder.ServiceProvider.GetRequiredService<AppFeatureEventSubscriptions>();
                foreach (var subscription in subscriptions.Get<TFeature, TOptions>())
                {
                    subscription.Notify(options.Value);
                }
            }

            builder.Services.TryAddSingleton(feature);
            builder.Services.AddOptions<TOptions>()
                .Configure(configure)
                .ValidateDataAnnotations();

            var logger = builder.ServiceProvider.GetLogger<TFeature>();
            if (enabled)
                logger?.LogInformation($"Pipeline: Feature '{typeof(TFeature).Name}' registered successfully");

            return builder;
        }
コード例 #4
0
ファイル: BotFeature.cs プロジェクト: btshft/Zeus
        /// <inheritdoc />
        protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
        {
            const string clientName = "telegram.client";

            var options = Options.Value;

            services.AddHttpClient(clientName)
            .ConfigureHttpClient((sp, cl) => {})
            .ConfigurePrimaryHttpMessageHandler(sp =>
            {
                var proxy = options.Proxy?.CreateProxy();
                return(new HttpClientHandler
                {
                    UseProxy = proxy != null,
                    Proxy = proxy
                });
            });

            services.AddSingleton <IRequestHandlerFinder>(new RequestHandlerFinder(services));
            services.AddSingleton <ITelegramBotClient>(sp =>
            {
                var client = sp.GetHttpClient(clientName);
                return(new TelegramBotClient(options.Token, client));
            });

            services.AddHostedService <BotUpdatesPollingService>();

            services.AddTransient <IBotActionContextAccessor, BotActionContextAccessor>();
            services.AddTransient <IBotUserProvider, BotUserProvider>();
            services.AddSingleton <IBotPollingUpdatesReceiver, BotPollingUpdatesReceiver>();
        }
コード例 #5
0
ファイル: HealthChecksFeature.cs プロジェクト: btshft/Zeus
        /// <inheritdoc />
        protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
        {
            var options = Options.Value;

            services.TryAddSingleton <IHealthCheckResponseWriter, SystemJsonHealthCheckResponseWriter>();

            var builder = services.AddHealthChecks();

            features
            .WhenConfigured <AlertingFeature, AlertingFeatureOptions>(o =>
            {
                var consulOptions = o.Subscriptions.Store.Consul;
                if (consulOptions != null)
                {
                    var urlInfo = new UriBuilder(consulOptions.Address);

                    builder.AddConsul(co =>
                    {
                        co.HostName = urlInfo.Host;
                        co.Port     = urlInfo.Port;
                    }, name: "consul",
                                      options.Consul?.FailureStatus,
                                      timeout: options.Consul?.Timeout);
                }
            })
            .WhenConfigured <BotFeature, BotFeatureOptions>(o =>
            {
                var registration = new HealthCheckRegistration(
                    name: "telegram",
                    factory: sp => new TelegramHealthCheck(sp.GetRequiredService <ITelegramBotClient>()),
                    failureStatus: options.Telegram.FailureStatus,
                    tags: default,
コード例 #6
0
ファイル: SwaggerFeature.cs プロジェクト: btshft/Zeus
        /// <inheritdoc />
        protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
        {
            services.AddTransient <IConfigureOptions <SwaggerGenOptions>, ConfigureSwaggerGenOptions>();
            services.AddSwaggerGen(s =>
            {
                s.OperationFilter <SwaggerDefaultValues>();

                var includeDocsTypesMarkers = new[]
                {
                    typeof(BotController),
                    typeof(AlertManagerWebhookUpdate),
                    typeof(Update)
                };

                foreach (var marker in includeDocsTypesMarkers)
                {
                    var xmlName = $"{marker.Assembly.GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlName);

                    if (File.Exists(xmlPath))
                    {
                        s.IncludeXmlComments(xmlPath);
                    }
                }
            });
        }
コード例 #7
0
        public static IAppFeatureCollection WhenConfigured<TFeature, TOptions>(this IAppFeatureCollection features,
            Action<TOptions> callback)
            where TFeature : class, IAppFeature<TOptions>
            where TOptions : class, new()
        {
            var subscriptions = features.ServiceProvider.GetRequiredService<AppFeatureEventSubscriptions>();
            subscriptions.Add(new AppFeatureSubscription<TFeature, TOptions>(callback));

            return features;
        }
コード例 #8
0
ファイル: OptionalFeature.cs プロジェクト: btshft/Zeus
        /// <inheritdoc />
        public bool Configure(IServiceCollection services, IAppFeatureCollection features)
        {
            if (Options.Value.Enabled)
            {
                ConfigureFeature(services, features);
            }

            var isOverriden = _methodHolder.ConfigureFeature.IsOverriden();

            return(Options.Value.Enabled && isOverriden);
        }
コード例 #9
0
 /// <inheritdoc />
 protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
 {
     if (Options.Value.OrphanNotifications != null)
     {
         services.AddCronJob <OrphanSubscriptionsCleanupJob>(o =>
         {
             o.UseLocalTimeZone = false;
             o.Schedule         = Options.Value.OrphanNotifications.Schedule;
         });
     }
 }
コード例 #10
0
ファイル: ProfilingFeature.cs プロジェクト: btshft/Zeus
        /// <inheritdoc />
        protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
        {
            var options = Options.Value;

            services.AddMemoryCache();
            services.AddMiniProfiler(o =>
            {
                o.RouteBasePath = options.Path;
            });

            services.AddDiagnosticObserver <HttpClientProfilingDiagnosticObserver>();
            services.Decorate <IStringLocalizerFactory, ProfilingStringLocalizerFactory>();
        }
コード例 #11
0
        public static IAppFeatureCollection Add<TFeature>(this IAppFeatureCollection builder)
            where TFeature : class, IAppFeature
        {
            var feature = ActivatorUtilities.CreateInstance<TFeature>(builder.ServiceProvider);

            var enabled = feature.Configure(builder.Services, builder); 
            builder.Services.TryAddSingleton(feature);

            var logger = builder.ServiceProvider.GetLogger<TFeature>();

            if (enabled)
                logger?.LogInformation($"Services: Feature '{typeof(TFeature).Name}' registered successfully");

            return builder;
        }
コード例 #12
0
ファイル: ApiFeature.cs プロジェクト: btshft/Zeus
        /// <inheritdoc />
        protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
        {
            services
            .AddApiVersioning(o =>
            {
                o.ReportApiVersions = true;
            })
            .AddVersionedApiExplorer();

            services.AddHttpContextAccessor();
            services.AddControllers()
            .AddJsonOptions(o =>
            {
                o.JsonSerializerOptions.IgnoreNullValues = true;
                o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });
        }
コード例 #13
0
        public static IAppFeatureCollection AddFromConfiguration <TFeature, TOptions>(this IAppFeatureCollection builder, string sectionName,
                                                                                      bool required = true, bool addAlways = false)
            where TFeature : class, IAppFeature <TOptions>
            where TOptions : class, new()
        {
            var configuration = builder.ServiceProvider.GetRequiredService <IConfiguration>();

            return(required
                ? builder.Add <TFeature, TOptions>(
                       configuration.CreateBinder <TOptions>(sectionName, required: true))
                : builder.AddWhen <TFeature, TOptions>(
                       () => configuration.SectionExists(sectionName) || addAlways,
                       configuration.CreateBinder <TOptions>(sectionName, required: false)));
        }
コード例 #14
0
ファイル: TransportFeature.cs プロジェクト: btshft/Zeus
 /// <inheritdoc />
 protected override void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
 {
     void RegisterKafkaTransport <TMessage, TConsumer>(IKafkaTransportBuilder builder,
                                                       TransportFeatureOptions.KafkaTransportOptions.TransportOptions transportOptions)
         where TConsumer : class, ITransportConsumer <TMessage>
コード例 #15
0
ファイル: AppFeature.cs プロジェクト: btshft/Zeus
 protected virtual void ConfigureFeature(IServiceCollection services, IAppFeatureCollection features)
 {
 }
コード例 #16
0
ファイル: AppFeature.cs プロジェクト: btshft/Zeus
 /// <inheritdoc />
 public bool Configure(IServiceCollection services, IAppFeatureCollection features)
 {
     ConfigureFeature(services, features);
     return(_methodHolder.ConfigureFeature.IsOverriden());
 }
コード例 #17
0
 public static bool IsEnabled<TFeature>(this IAppFeatureCollection builder) 
     where TFeature : IAppFeature
 {
     return builder.Services.Any(s => s.ServiceType == typeof(TFeature));
 }