1
0
mirror of https://github.com/bitwarden/server synced 2025-12-16 16:23:31 +00:00

Prefer startup pattern to program pattern

This commit is contained in:
Matt Gibson
2025-11-19 16:41:14 -08:00
parent 811a234c65
commit 1e7e3db807
3 changed files with 114 additions and 47 deletions

View File

@@ -4,7 +4,7 @@ using Bit.IntegrationTestCommon.Factories;
namespace Bit.SeederApi.IntegrationTest; namespace Bit.SeederApi.IntegrationTest;
public class SeederApiApplicationFactory : WebApplicationFactoryBase<Program> public class SeederApiApplicationFactory : WebApplicationFactoryBase<Startup>
{ {
public SeederApiApplicationFactory() public SeederApiApplicationFactory()
{ {

View File

@@ -1,50 +1,31 @@
using Bit.Seeder; using Bit.Core.Utilities;
using Bit.SeederApi.Extensions;
using Bit.SeederApi.Services;
using Bit.SharedWeb.Utilities;
var builder = WebApplication.CreateBuilder(args); namespace Bit.SeederApi;
builder.Services.AddControllers(); public class Program
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.IPasswordHasher<Bit.Core.Entities.User>, Microsoft.AspNetCore.Identity.PasswordHasher<Bit.Core.Entities.User>>();
// Seeder services
builder.Services.AddSingleton<Bit.RustSDK.RustSdkService>();
builder.Services.AddScoped<Bit.Seeder.Factories.UserSeeder>();
builder.Services.AddScoped<ISceneService, SceneService>();
builder.Services.AddScoped<IQueryService, QueryService>();
builder.Services.AddScoped<MangleId>(_ => new MangleId());
builder.Services.AddScenes();
builder.Services.AddQueries();
var app = builder.Build();
// Add PlayIdMiddleware services
if (globalSettings.TestPlayIdTrackingEnabled)
{ {
app.UseMiddleware<PlayIdMiddleware>(); public static void Main(string[] args)
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{ {
app.UseExceptionHandler("/Home/Error"); Host
.CreateDefaultBuilder(args)
.ConfigureCustomAppConfiguration(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
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();
}
} }
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 { }

86
util/SeederApi/Startup.cs Normal file
View File

@@ -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<IHttpContextAccessor, HttpContextAccessor>();
// Identity
services.AddScoped<IPasswordHasher<Core.Entities.User>, PasswordHasher<Core.Entities.User>>();
// Seeder services
services.AddSingleton<RustSDK.RustSdkService>();
services.AddScoped<UserSeeder>();
services.AddScoped<ISceneService, SceneService>();
services.AddScoped<IQueryService, QueryService>();
services.AddScoped<MangleId>(_ => 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<PlayIdMiddleware>();
}
// 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?}");
});
}
}