コード例 #1
0
ファイル: Startup.cs プロジェクト: waffle-iron/tweek
        // 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"));
        }