mirror of
https://github.com/bitwarden/server.git
synced 2026-02-08 09:53:14 +08:00
Update to IHostBuilder style
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Bit.Server.IntegrationTest;
|
||||
|
||||
@@ -12,34 +12,27 @@ public class Server : WebApplicationFactory<Program>
|
||||
public bool? WebVault { get; set; }
|
||||
public string? AppIdLocation { get; set; }
|
||||
|
||||
protected override IWebHostBuilder? CreateWebHostBuilder()
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
var args = new List<string>
|
||||
base.ConfigureWebHost(builder);
|
||||
|
||||
var config = new Dictionary<string, string?>
|
||||
{
|
||||
"/contentRoot",
|
||||
ContentRoot ?? "",
|
||||
"/webRoot",
|
||||
WebRoot ?? "",
|
||||
"/serveUnknown",
|
||||
ServeUnknown.ToString().ToLowerInvariant(),
|
||||
{"contentRoot", ContentRoot},
|
||||
{"webRoot", WebRoot},
|
||||
{"serveUnknown", ServeUnknown.ToString().ToLowerInvariant()},
|
||||
};
|
||||
|
||||
if (WebVault.HasValue)
|
||||
{
|
||||
args.Add("/webVault");
|
||||
args.Add(WebVault.Value.ToString().ToLowerInvariant());
|
||||
config["webVault"] = WebVault.Value.ToString().ToLowerInvariant();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(AppIdLocation))
|
||||
{
|
||||
args.Add("/appIdLocation");
|
||||
args.Add(AppIdLocation);
|
||||
config["appIdLocation"] = AppIdLocation;
|
||||
}
|
||||
|
||||
var builder = WebHostBuilderFactory.CreateFromTypesAssemblyEntryPoint<Program>([.. args])
|
||||
?? throw new InvalidProgramException("Could not create builder from assembly.");
|
||||
|
||||
builder.UseSetting("TEST_CONTENTROOT_SERVER", ContentRoot);
|
||||
return builder;
|
||||
builder.UseConfiguration(new ConfigurationBuilder().AddInMemoryCollection(config).Build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,27 +6,30 @@ namespace Bit.Server;
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = CreateWebHostBuilder(args);
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
}
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddCommandLine(args)
|
||||
.Build();
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
.UseConfiguration(config)
|
||||
.UseKestrel()
|
||||
.UseStartup<Startup>()
|
||||
.ConfigureLogging((hostingContext, logging) =>
|
||||
var builder = new HostBuilder()
|
||||
.ConfigureWebHost(builder =>
|
||||
{
|
||||
logging.AddConsole().AddDebug();
|
||||
builder.UseConfiguration(config);
|
||||
builder.UseKestrel();
|
||||
builder.UseStartup<Startup>();
|
||||
builder.ConfigureKestrel((_,_) => {});
|
||||
|
||||
var webRoot = config.GetValue<string>("webRoot");
|
||||
if (string.IsNullOrWhiteSpace(webRoot))
|
||||
{
|
||||
builder.UseWebRoot(webRoot);
|
||||
}
|
||||
})
|
||||
.ConfigureKestrel((context, options) => { });
|
||||
.ConfigureLogging(logging =>
|
||||
{
|
||||
logging.AddConsole()
|
||||
.AddDebug();
|
||||
});
|
||||
|
||||
var contentRoot = config.GetValue<string>("contentRoot");
|
||||
if (!string.IsNullOrWhiteSpace(contentRoot))
|
||||
@@ -38,12 +41,7 @@ public class Program
|
||||
builder.UseContentRoot(Directory.GetCurrentDirectory());
|
||||
}
|
||||
|
||||
var webRoot = config.GetValue<string>("webRoot");
|
||||
if (string.IsNullOrWhiteSpace(webRoot))
|
||||
{
|
||||
builder.UseWebRoot(webRoot);
|
||||
}
|
||||
|
||||
return builder;
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user