Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddLogging();

            // Authentication
            var authOptions = Configuration.GetSection("Authentication").Get <AuthOptions>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(
                JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.Authority = authOptions.Authority;
                options.TokenValidationParameters =
                    new TokenValidationParameters {
                    ValidateAudience = false, ValidIssuer = authOptions.Issuer
                };

                options.RequireHttpsMetadata = !authOptions.NoSslRequired;

                options.AcceptTokenFromQuery();
            });
            services.AddSingleton <IAuthorizationHandler, UserIsModeratorOfConferenceHandler>();

            var sfuOptions = Configuration.GetSection("SFU").Get <SfuOptions>();
            var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(sfuOptions.TokenSecret ??
                                                                              throw new ArgumentException(
                                                                                  "SFU token secret not set")));

            services.AddSingleton <IOptions <SfuJwtOptions> >(new OptionsWrapper <SfuJwtOptions>(new SfuJwtOptions
            {
                Audience           = sfuOptions.TokenAudience,
                Issuer             = sfuOptions.TokenIssuer,
                SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
                ValidFor           = sfuOptions.TokenExpiration,
            }));

            services.AddSingleton <IOptions <SfuConnectionOptions> >(new OptionsWrapper <SfuConnectionOptions>(
                                                                         new SfuConnectionOptions(sfuOptions.UrlTemplate ??
                                                                                                  throw new ArgumentException("SFU url template not set."))));

            // SignalR
            services.AddSignalR().AddNewtonsoftJsonProtocol(options =>
            {
                JsonConfig.Apply(options.PayloadSerializerSettings);
            });

            services.AddMvc().ConfigureApiBehaviorOptions(options => options.UseInvalidModelStateToError())
            .AddFluentValidation(fv =>
                                 fv.RegisterValidatorsFromAssemblyContaining <Startup>()
                                 .RegisterValidatorsFromAssemblyContaining <CoreModule>()).AddNewtonsoftJson(options =>
            {
                JsonConfig.Apply(options.SerializerSettings);
            });

            services.AddAutoMapper(Assembly.GetExecutingAssembly(), typeof(CoreModule).Assembly);

            var healthChecks = services.AddHealthChecks();

            // KeyValueDatabase
            var keyValueOptions = Configuration.GetSection("KeyValueDatabase").Get <KeyValueDatabaseConfig>();

            if (keyValueOptions.UseInMemory)
            {
                services.AddSingleton <IKeyValueDatabase, InMemoryKeyValueDatabase>(services =>
                                                                                    new InMemoryKeyValueDatabase(new InMemoryKeyValueData(),
                                                                                                                 services.GetRequiredService <IOptions <KeyValueDatabaseOptions> >()));
            }
            else
            {
                var config = keyValueOptions.Redis ?? new RedisConfiguration();
                services.AddStackExchangeRedisExtensions <NewtonsoftSerializer>(config);
                services.AddSingleton(s => s.GetRequiredService <IRedisDatabase>().Database);
                services.AddSingleton <IKeyValueDatabase, RedisKeyValueDatabase>();

                healthChecks.AddRedis(config.ConnectionString);
            }

            // MongoDb
            services.Configure <MongoDbOptions>(Configuration.GetSection("MongoDb"));
            services.AddHostedService <MongoDbBuilder>();

            var mongoOptions = Configuration.GetSection("MongoDb").Get <MongoDbOptions>();

            healthChecks.AddMongoDb(mongoOptions.ConnectionString);

            services.Configure <HealthCheckPublisherOptions>(options =>
            {
                options.Predicate = check => check.Tags.Contains("ready");
            });

            // Masstransit / RabbitMQ
            services.Configure <SfuOptions>(Configuration.GetSection("SFU"));
            services.Configure <RabbitMqOptions>(Configuration.GetSection("RabbitMq"));

            var rabbitMqOptions = Configuration.GetSection("RabbitMq").Get <RabbitMqOptions>();

            services.AddMassTransit(config =>
            {
                //x.AddSignalRHub<CoreHub>();
                config.AddConsumersFromNamespaceContaining <ParticipantKickedConsumer>();
                config.AddConsumer <MediatrNotificationConsumer>();

                if (rabbitMqOptions.UseInMemory)
                {
                    Uri schedulerEndpoint = new("queue:scheduler");
                    config.AddMessageScheduler(schedulerEndpoint);
                    config.UsingInMemory((context, configurator) =>
                    {
                        configurator.UseInMemoryScheduler("scheduler");
                        configurator.ConfigureEndpoints(context);

                        ScheduledMediator.Configure(configurator, context);
                    });
                }
                else
                {
                    config.AddDelayedMessageScheduler();
                    config.UsingRabbitMq((context, configurator) =>
                    {
                        if (rabbitMqOptions.RabbitMq != null)
                        {
                            configurator.ConfigureOptions(rabbitMqOptions.RabbitMq);
                        }

                        configurator.UseHealthCheck(context);
                        configurator.UseDelayedMessageScheduler();

                        configurator.ConfigureEndpoints(context);

                        ScheduledMediator.Configure(configurator, context);

                        configurator.ConfigurePublishMessage <MediaStateChanged>(sfuOptions);
                        configurator.ConfigurePublishMessage <ChangeParticipantProducer>(sfuOptions);
                        configurator.ConfigurePublishMessage <ParticipantLeft>(sfuOptions);

                        configurator.ReceiveEndpoint(sfuOptions.ReceiveQueue, e =>
                        {
                            e.Durable = false;

                            e.Consumer <StreamsUpdatedConsumer>(context);
                            e.Consumer <NotifyConnectionConsumer>(context);
                        });

                        configurator.ConfigureJsonSerializer(jsonConfig =>
                        {
                            jsonConfig.DefaultValueHandling = DefaultValueHandling.Include;
                            JsonConfig.Apply(jsonConfig);
                            return(jsonConfig);
                        });
                    });
                }
            });
            services.AddMassTransitHostedService();
            services.AddMediator();

            // Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Strive API", Version = "v1"
                });

                var scheme = new OpenApiSecurityScheme
                {
                    In          = ParameterLocation.Header,
                    Description = "Please insert JWT with Bearer into field",
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.ApiKey,
                };

                // Swagger 2.+ support
                c.AddSecurityDefinition("Bearer", scheme);
                c.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    { scheme, new List <string>() }
                });
            });

            services.AddMediatR(typeof(Startup), typeof(CoreModule));

            if (Environment.IsDevelopment())
            {
                services.AddCors(options =>
                {
                    options.AddPolicy("AllowAll",
                                      builder =>
                    {
                        builder.WithOrigins("http://localhost:55103").AllowAnyMethod().AllowAnyHeader()
                        .AllowCredentials();
                    });
                });
            }

            services.Configure <ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });

            // Now register our services with Autofac container.
            var builder = new ContainerBuilder();

            builder.RegisterModule(new CoreModule());
            builder.RegisterModule(new InfrastructureModule());
            builder.RegisterModule(new PresentationModule());

            if (Environment.IsDevelopment())
            {
                builder.RegisterGeneric(typeof(LoggingBehavior <,>)).As(typeof(IPipelineBehavior <,>));
            }

            builder.Populate(services);
            var container = builder.Build();

            // Create the IServiceProvider based on the container.
            return(new AutofacServiceProvider(container));
        }
Пример #2
0
 protected HubConnection CreateHubConnection(string url)
 {
     return(new HubConnectionBuilder()
            .WithUrl(url, o => o.HttpMessageHandlerFactory = _ => Factory.Server.CreateHandler())
            .AddNewtonsoftJsonProtocol(settings => JsonConfig.Apply(settings.PayloadSerializerSettings)).Build());
 }