diff --git a/test/SeederApi.IntegrationTest/SeederApiApplicationFactory.cs b/test/SeederApi.IntegrationTest/SeederApiApplicationFactory.cs index efd5bf3d1d..18d2099042 100644 --- a/test/SeederApi.IntegrationTest/SeederApiApplicationFactory.cs +++ b/test/SeederApi.IntegrationTest/SeederApiApplicationFactory.cs @@ -4,7 +4,7 @@ using Bit.IntegrationTestCommon.Factories; namespace Bit.SeederApi.IntegrationTest; -public class SeederApiApplicationFactory : WebApplicationFactoryBase +public class SeederApiApplicationFactory : WebApplicationFactoryBase { public SeederApiApplicationFactory() { diff --git a/util/SeederApi/Program.cs b/util/SeederApi/Program.cs index 96ef07d99d..59d343a5fe 100644 --- a/util/SeederApi/Program.cs +++ b/util/SeederApi/Program.cs @@ -1,50 +1,31 @@ -using Bit.Seeder; -using Bit.SeederApi.Extensions; -using Bit.SeederApi.Services; -using Bit.SharedWeb.Utilities; +using Bit.Core.Utilities; -var builder = WebApplication.CreateBuilder(args); +namespace Bit.SeederApi; -builder.Services.AddControllers(); -builder.Services.AddHttpContextAccessor(); - -var globalSettings = builder.Services.AddGlobalSettingsServices(builder.Configuration, builder.Environment); - -// Common services -builder.Services.AddCustomDataProtectionServices(builder.Environment, globalSettings); -builder.Services.AddTokenizers(); -builder.Services.AddDatabaseRepositories(globalSettings); - -builder.Services.AddScoped, Microsoft.AspNetCore.Identity.PasswordHasher>(); - -// Seeder services -builder.Services.AddSingleton(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(_ => new MangleId()); -builder.Services.AddScenes(); -builder.Services.AddQueries(); - -var app = builder.Build(); - -// Add PlayIdMiddleware services -if (globalSettings.TestPlayIdTrackingEnabled) +public class Program { - app.UseMiddleware(); + public static void Main(string[] args) + { + Host + .CreateDefaultBuilder(args) + .ConfigureCustomAppConfiguration(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureLogging((hostingContext, logging) => + logging.AddSerilog(hostingContext, (e, globalSettings) => + { + var context = e.Properties["SourceContext"].ToString(); + if (e.Properties.TryGetValue("RequestPath", out var requestPath) && + !string.IsNullOrWhiteSpace(requestPath?.ToString()) && + (context.Contains(".Server.Kestrel") || context.Contains(".Core.IISHttpServer"))) + { + return false; + } + return e.Level >= Serilog.Events.LogEventLevel.Information; + })); + }) + .Build() + .Run(); + } } - -// Configure the HTTP request pipeline. -if (!app.Environment.IsDevelopment()) -{ - app.UseExceptionHandler("/Home/Error"); -} - -app.UseRouting(); - -app.MapControllerRoute(name: "default", pattern: "{controller=Seed}/{action=Index}/{id?}"); - -app.Run(); - -// Make Program class accessible for integration tests -public partial class Program { } diff --git a/util/SeederApi/Startup.cs b/util/SeederApi/Startup.cs new file mode 100644 index 0000000000..ab28d10801 --- /dev/null +++ b/util/SeederApi/Startup.cs @@ -0,0 +1,86 @@ +using System.Globalization; +using Bit.Core.Settings; +using Bit.Core.Utilities; +using Bit.Seeder; +using Bit.Seeder.Factories; +using Bit.SeederApi.Extensions; +using Bit.SeederApi.Services; +using Bit.SharedWeb.Utilities; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Bit.SeederApi; + +public class Startup +{ + public Startup(IWebHostEnvironment env, IConfiguration configuration) + { + CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); + Configuration = configuration; + Environment = env; + } + + public IConfiguration Configuration { get; private set; } + public IWebHostEnvironment Environment { get; set; } + + public void ConfigureServices(IServiceCollection services) + { + // Options + services.AddOptions(); + + // Settings + var globalSettings = services.AddGlobalSettingsServices(Configuration, Environment); + + // Data Protection + services.AddCustomDataProtectionServices(Environment, globalSettings); + + // Repositories + services.AddTokenizers(); + services.AddDatabaseRepositories(globalSettings); + + // Context + services.TryAddSingleton(); + + // Identity + services.AddScoped, PasswordHasher>(); + + // Seeder services + services.AddSingleton(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(_ => new MangleId()); + services.AddScenes(); + services.AddQueries(); + + // MVC + services.AddControllers(); + } + + public void Configure( + IApplicationBuilder app, + IWebHostEnvironment env, + IHostApplicationLifetime appLifetime, + GlobalSettings globalSettings) + { + app.UseSerilog(env, appLifetime, globalSettings); + + // Add PlayIdMiddleware services + if (globalSettings.TestPlayIdTrackingEnabled) + { + app.UseMiddleware(); + } + + // Configure the HTTP request pipeline + if (!env.IsDevelopment()) + { + app.UseExceptionHandler("/Home/Error"); + } + + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute(name: "default", pattern: "{controller=Seed}/{action=Index}/{id?}"); + }); + } +}