using System; using System.Globalization; using Bit.Core; using Bit.Core.Identity; using Bit.Core.Utilities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Stripe; namespace Bit.Admin { public class Startup { public Startup(IHostingEnvironment env, IConfiguration configuration) { CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); Configuration = configuration; Environment = env; } public IConfiguration Configuration { get; private set; } public IHostingEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { // Options services.AddOptions(); // Settings var globalSettings = services.AddGlobalSettingsServices(Configuration); services.Configure(Configuration.GetSection("AdminSettings")); // Data Protection services.AddCustomDataProtectionServices(Environment, globalSettings); // Stripe Billing StripeConfiguration.ApiKey = globalSettings.StripeApiKey; // Repositories services.AddSqlServerRepositories(globalSettings); // Context services.AddScoped(); // Identity services.AddPasswordlessIdentityServices(globalSettings); services.Configure(options => { options.ValidationInterval = TimeSpan.FromMinutes(5); }); if(globalSettings.SelfHosted) { services.ConfigureApplicationCookie(options => { options.Cookie.Path = "/admin"; }); } // Services services.AddBaseServices(); services.AddDefaultServices(globalSettings); // Mvc services.AddMvc(config => { config.Filters.Add(new LoggingExceptionHandlerFilterAttribute()); }); services.Configure(options => options.LowercaseUrls = true); // Jobs service Jobs.JobsHostedService.AddJobsServices(services, globalSettings.SelfHosted); services.AddHostedService(); if(globalSettings.SelfHosted) { services.AddHostedService(); } else { if(CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString)) { services.AddHostedService(); } else if(CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret)) { services.AddHostedService(); } } } public void Configure( IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime, GlobalSettings globalSettings) { app.UseSerilog(env, appLifetime, globalSettings); if(globalSettings.SelfHosted) { app.UsePathBase("/admin"); app.UseForwardedHeaders(globalSettings); } if(env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseAuthentication(); app.UseStaticFiles(); app.UseMvcWithDefaultRoute(); } } }