예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.RegisterAddonServices(Configuration);
            services.Decorate <IContextDriver>((driver, provider) =>
                                               new TimedContextDriver(driver, provider.GetService <IMetrics>()));
            services.Decorate <IContextDriver>((driver, provider) => CreateInputValidationDriverDecorator(provider, driver));
            services.Decorate <IRulesDriver>((driver, provider) => new TimedRulesDriver(driver, provider.GetService <IMetrics>));

            services.AddSingleton(CreateParserResolver());

            var rulesetVersionProvider = Configuration.GetValue("RulesetVersionProvider", "SampleVersionProvider");

            services.AddSingleton <IRulesetVersionProvider>(provider =>
            {
                switch (rulesetVersionProvider)
                {
                case "NatsVersionProvider":
                    return(new NatsVersionProvider(provider.GetService <IRulesDriver>(),
                                                   Configuration.GetValue <string>("Rules:Nats:Endpoint"),
                                                   provider.GetService <ILoggerFactory>().CreateLogger("NatsVersionProvider")));

                default:
                    return(new SampleVersionProvider(provider.GetService <IRulesDriver>(),
                                                     TimeSpan.FromMilliseconds(Configuration.GetValue("Rules:SampleIntervalInMs", 30000))));
                }
            });

            var failureDelayInMs   = Configuration.GetValue("Rules:FailureDelayInMs", 60000);
            var versionTimeoudInMs = Configuration.GetValue("Rules:VersionTimeoutInMs", failureDelayInMs * 3);

            services.AddSingleton <IRulesRepository>(provider => new RulesRepository(provider.GetService <IRulesDriver>(),
                                                                                     provider.GetService <IRulesetVersionProvider>(),
                                                                                     TimeSpan.FromMilliseconds(failureDelayInMs),
                                                                                     TimeSpan.FromMilliseconds(versionTimeoudInMs),
                                                                                     provider.GetService <ILoggerFactory>().CreateLogger("RulesRepository")));
            services.AddSingleton(provider =>
            {
                var parserResolver = provider.GetService <GetRuleParser>();
                var rulesDriver    = provider.GetService <IRulesRepository>();
                return(Task.Run(async() => await Engine.Tweek.Create(rulesDriver, parserResolver)).Result);
            });
            services.AddSingleton(provider =>
            {
                var rulesDriver = provider.GetService <IRulesRepository>();
                return(Task.Run(async() => await TweekIdentityProvider.Create(rulesDriver)).Result);
            });
            services.AddSingleton(provider => Authorization.CreateReadConfigurationAccessChecker(provider.GetService <ITweek>(), provider.GetService <TweekIdentityProvider>()));
            services.AddSingleton(provider => Authorization.CreateWriteContextAccessChecker(provider.GetService <ITweek>(), provider.GetService <TweekIdentityProvider>()));
            services.AddSingleton(provider => Validator.GetValidationDelegate(provider.GetService <GetRuleParser>()));

            var tweekContactResolver = new TweekContractResolver();
            var jsonSerializer       = new JsonSerializer()
            {
                ContractResolver = tweekContactResolver
            };

            services.AddSingleton(jsonSerializer);
            services.AddMvc()
            .AddMetrics()
            .AddJsonOptions(opt =>
            {
                opt.SerializerSettings.ContractResolver = tweekContactResolver;
            });

            services.SetupCors(Configuration);

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("api", new Info
                {
                    Title   = "Tweek Api",
                    License = new License {
                        Name = "MIT", Url = "https://github.com/Soluto/tweek/blob/master/LICENSE"
                    },
                    Version = Assembly.GetEntryAssembly()
                              .GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                              .InformationalVersion
                });
                // Generate Dictionary<string,JsonValue> as JSON object in Swagger
                options.MapType(typeof(Dictionary <string, JsonValue>), () => new Schema {
                    Type = "object"
                });

                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var xmlPath  = Path.Combine(basePath, "Tweek.ApiService.xml");
                options.IncludeXmlComments(xmlPath);
            });
            services.ConfigureAuthenticationProviders(Configuration, loggerFactory.CreateLogger("AuthenticationProviders"));
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.RegisterAddonServices(Configuration);

            services.Decorate <IContextDriver>((driver, provider) => new TimedContextDriver(driver, provider.GetService <IMetrics>()));

            services.AddSingleton <IDiagnosticsProvider>(ctx => new RulesDriverDiagnosticsProvider(ctx.GetServices <IRulesDriver>().Single()));
            services.AddSingleton <IDiagnosticsProvider>(new EnvironmentDiagnosticsProvider());

            services.AddSingleton(CreateParserResolver());
            services.AddSingleton(provider =>
            {
                var parserResolver = provider.GetService <GetRuleParser>();
                var rulesDriver    = provider.GetService <IRulesDriver>();
                return(Task.Run(async() => await Engine.Tweek.Create(rulesDriver, parserResolver)).Result);
            });
            services.AddSingleton(provider =>
            {
                var rulesDriver = provider.GetService <IRulesDriver>();
                return(Task.Run(async() => await TweekIdentityProvider.Create(rulesDriver)).Result);
            });
            services.AddSingleton(provider => Authorization.CreateReadConfigurationAccessChecker(provider.GetService <ITweek>(), provider.GetService <TweekIdentityProvider>()));
            services.AddSingleton(provider => Authorization.CreateWriteContextAccessChecker(provider.GetService <ITweek>(), provider.GetService <TweekIdentityProvider>()));
            services.AddSingleton(provider => Validator.GetValidationDelegate(provider.GetService <GetRuleParser>()));

            var tweekContactResolver = new TweekContractResolver();
            var jsonSerializer       = new JsonSerializer()
            {
                ContractResolver = tweekContactResolver
            };

            services.AddSingleton(jsonSerializer);
            services.AddMvc(options => options.AddMetricsResourceFilter())
            .AddJsonOptions(opt =>
            {
                opt.SerializerSettings.ContractResolver = tweekContactResolver;
            });

            services.SetupCors(Configuration);

            RegisterMetrics(services);
            services.AdaptSingletons <IDiagnosticsProvider, HealthCheck>(inner => new DiagnosticsProviderDecorator(inner));
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("api", new Info
                {
                    Title   = "Tweek Api",
                    License = new License {
                        Name = "MIT", Url = "https://github.com/Soluto/tweek/blob/master/LICENSE"
                    },
                    Version = Assembly.GetEntryAssembly()
                              .GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                              .InformationalVersion
                });
                // Generate Dictionary<string,JsonValue> as JSON object in Swagger
                options.MapType(typeof(Dictionary <string, JsonValue>), () => new Schema {
                    Type = "object"
                });

                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var xmlPath  = Path.Combine(basePath, "Tweek.ApiService.NetCore.xml");
                options.IncludeXmlComments(xmlPath);
            });
            services.ConfigureAuthenticationProviders(Configuration, loggerFactory.CreateLogger("AuthenticationProviders"));
        }