Exemple #1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var loggerFactory = new LoggerFactory()
                                .AddConsole(LogLevel.Error)
                                .AddDebug(LogLevel.Error);

            services.AddSingleton(loggerFactory);
            services.AddLogging();
            services.AddSingleton(Configuration);
            services.AddMvc()
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                options.SerializerSettings.Converters       = SerializerSettings.GetDefaultConverters();
            });

            services.AddSwaggerGen(options =>
            {
                options.DefaultLykkeConfiguration("v1", "MarginTrading_Api");
                options.OperationFilter <AddAuthorizationHeaderParameter>();
            });

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.SecurityTokenValidators.Clear();
                options.SecurityTokenValidators.Add(ApplicationContainer.Resolve <ISecurityTokenValidator>());
            });

            var builder = new ContainerBuilder();


            var appSettings = Configuration.LoadSettings <ApplicationSettings>()
                              .Nested(s =>
            {
                if (!string.IsNullOrEmpty(Configuration["Env"]))
                {
                    s.MtFrontend.MarginTradingFront.Env = Configuration["Env"];
                }

                return(s);
            });

            var settings = appSettings.Nested(s => s.MtFrontend);


            Console.WriteLine($"Env: {settings.CurrentValue.MarginTradingFront.Env}");

            SetupLoggers(services, appSettings);

            RegisterModules(builder, appSettings);

            builder.Populate(services);

            ApplicationContainer = builder.Build();

            SetSubscribers(settings.CurrentValue);

            return(new AutofacServiceProvider(ApplicationContainer));
        }
Exemple #2
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var loggerFactory = new LoggerFactory()
                                .AddConsole(LogLevel.Error)
                                .AddDebug(LogLevel.Error);

            services.AddSingleton(loggerFactory);
            services.AddLogging();
            services.AddSingleton(Configuration);
            services.AddMvc(options => options.Filters.Add(typeof(MarginTradingEnabledFilter)))
            .AddJsonOptions(
                options => { options.SerializerSettings.Converters = SerializerSettings.GetDefaultConverters(); });
            services.AddAuthentication(KeyAuthOptions.AuthenticationScheme)
            .AddScheme <KeyAuthOptions, KeyAuthHandler>(KeyAuthOptions.AuthenticationScheme, "", options => { });

            var isLive = Configuration.IsLive();

            services.AddSwaggerGen(options =>
            {
                options.DefaultLykkeConfiguration("v1", $"MarginTrading_Api_{Configuration.ServerType()}");
                options.OperationFilter <ApiKeyHeaderOperationFilter>();
            });

            var builder = new ContainerBuilder();

            var envSuffix  = !string.IsNullOrEmpty(Configuration["Env"]) ? "." + Configuration["Env"] : "";
            var mtSettings = Configuration.LoadSettings <MtBackendSettings>()
                             .Nested(s =>
            {
                var inner    = isLive ? s.MtBackend.MarginTradingLive : s.MtBackend.MarginTradingDemo;
                inner.IsLive = isLive;
                inner.Env    = Configuration.ServerType() + envSuffix;
                return(s);
            });

            var settings =
                mtSettings.Nested(s => isLive ? s.MtBackend.MarginTradingLive : s.MtBackend.MarginTradingDemo);
            var riskInformingSettings =
                mtSettings.Nested(s => isLive ? s.RiskInformingSettings : s.RiskInformingSettingsDemo);

            Console.WriteLine($"IsLive: {settings.CurrentValue.IsLive}");

            SetupLoggers(services, mtSettings, settings);

            RegisterModules(builder, mtSettings, settings, Environment, riskInformingSettings);

            builder.Populate(services);

            ApplicationContainer = builder.Build();

            MtServiceLocator.FplService            = ApplicationContainer.Resolve <IFplService>();
            MtServiceLocator.AccountUpdateService  = ApplicationContainer.Resolve <IAccountUpdateService>();
            MtServiceLocator.AccountsCacheService  = ApplicationContainer.Resolve <IAccountsCacheService>();
            MtServiceLocator.SwapCommissionService = ApplicationContainer.Resolve <ICommissionService>();
            MtServiceLocator.OvernightSwapService  = ApplicationContainer.Resolve <IOvernightSwapService>();

            return(new AutofacServiceProvider(ApplicationContainer));
        }
Exemple #3
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
        {
            app.UseGlobalErrorHandler();
            app.UseOptions();

            var settings = ApplicationContainer.Resolve <MtFrontSettings>();

            if (settings.CorsSettings.Enabled)
            {
                app.UseCors(builder =>
                {
                    builder.WithOrigins(settings.CorsSettings.AllowOrigins)
                    .WithHeaders(settings.CorsSettings.AllowHeaders)
                    .WithMethods(settings.CorsSettings.AllowMethods);

                    if (settings.CorsSettings.AllowCredentials)
                    {
                        builder.AllowCredentials();
                    }
                });
            }


            var host             = ApplicationContainer.Resolve <IWampHost>();
            var realm            = ApplicationContainer.Resolve <IWampHostedRealm>();
            var realmMetaService = realm.HostMetaApiService();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(name: "Default", template: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseSwagger();
            app.UseSwaggerUi();
            app.UseStaticFiles();

            app.Map("/ws", builder =>
            {
                builder.UseWebSockets(new WebSocketOptions {
                    KeepAliveInterval = TimeSpan.FromMinutes(1)
                });

                var jsonSettings =
                    new JsonSerializerSettings()
                {
                    Converters = SerializerSettings.GetDefaultConverters()
                };
                var jsonSerializer = JsonSerializer.Create(jsonSettings);

                host.RegisterTransport(new AspNetCoreWebSocketTransport(builder),
                                       new JTokenJsonBinding(jsonSerializer),
                                       new JTokenMsgpackBinding(jsonSerializer));
            });

            appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());

            var application = app.ApplicationServices.GetService <Application>();

            appLifetime.ApplicationStarted.Register(() =>
            {
                if (!string.IsNullOrEmpty(settings.ApplicationInsightsKey))
                {
                    Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey =
                        settings.ApplicationInsightsKey;
                }

                application.StartAsync().Wait();

                LogLocator.CommonLog?.WriteMonitorAsync("", "", settings.Env + " Started");
            });

            appLifetime.ApplicationStopping.Register(() =>
            {
                LogLocator.CommonLog?.WriteMonitorAsync("", "", settings.Env + " Terminating");
                realmMetaService.Dispose();
                application.Stop();
            }
                                                     );

            host.Open();
        }
Exemple #4
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var appSettings = Configuration.LoadSettings <ApplicationSettings>()
                              .Nested(s =>
            {
                if (!string.IsNullOrEmpty(Configuration["Env"]))
                {
                    s.MtFrontend.MarginTradingFront.Env = Configuration["Env"];
                }

                return(s);
            });

            var loggerFactory = new LoggerFactory()
                                .AddConsole(LogLevel.Error)
                                .AddDebug(LogLevel.Error);

            services.AddDistributedMemoryCache();
            services.AddSingleton(loggerFactory);
            services.AddLogging();
            services.AddSingleton(Configuration);
            services.AddMvc()
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                options.SerializerSettings.Converters       = SerializerSettings.GetDefaultConverters();
            });

            services.AddSwaggerGen(options =>
            {
                options.DefaultLykkeConfiguration("v1", "MarginTrading_Api");
                options.OperationFilter <AddAuthorizationHeaderParameter>();
            });

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = AuthConsts.LykkeBearerScheme;
                options.DefaultChallengeScheme    = AuthConsts.LykkeBearerScheme;
            })
            .AddOAuth2Introspection(AuthConsts.OAuthBearerScheme, options =>
            {
                var oauthSettings     = appSettings.Nested(s => s.MtFrontend.MarginTradingFront.OAuthSettings).CurrentValue;
                options.Authority     = oauthSettings.Authority;
                options.ClientId      = oauthSettings.ClientId;
                options.ClientSecret  = oauthSettings.ClientSecret;
                options.CacheDuration = TimeSpan.FromMinutes(1);
                options.EnableCaching = true;
            })
            .AddJwtBearer(AuthConsts.LykkeBearerScheme, options =>
            {
                options.SecurityTokenValidators.Clear();
                options.SecurityTokenValidators.Add(ApplicationContainer.Resolve <ISecurityTokenValidator>());
                options.ForwardDefaultSelector = ctx =>
                {
                    var token = TokenRetrieval.FromAuthorizationHeader()(ctx.Request);
                    if (string.IsNullOrWhiteSpace(token) || token.Length == AuthConsts.LykkeTokenLength)
                    {
                        return(AuthConsts.LykkeBearerScheme);
                    }

                    return(AuthConsts.OAuthBearerScheme);
                };
            });


            var builder = new ContainerBuilder();



            var settings = appSettings.Nested(s => s.MtFrontend);


            Console.WriteLine($"Env: {settings.CurrentValue.MarginTradingFront.Env}");

            SetupLoggers(services, appSettings);

            RegisterModules(builder, appSettings);

            builder.Populate(services);

            ApplicationContainer = builder.Build();

            SetSubscribers(settings.CurrentValue);

            return(new AutofacServiceProvider(ApplicationContainer));
        }