mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-06 03:48:40 +00:00
Automatically warmup website hot reload when starting up debug builds
This commit is contained in:
parent
c5cc8a8782
commit
fb0b108c21
2 changed files with 59 additions and 0 deletions
52
ProjectLighthouse/Startup/DebugWarmupLifetime.cs
Normal file
52
ProjectLighthouse/Startup/DebugWarmupLifetime.cs
Normal file
|
@ -0,0 +1,52 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Kettu;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Hosting.Internal;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Startup;
|
||||
|
||||
public class DebugWarmupLifetime : IHostLifetime
|
||||
{
|
||||
public IHostApplicationLifetime ApplicationLifetime { get; }
|
||||
|
||||
private CancellationTokenRegistration applicationStartedRegistration;
|
||||
|
||||
private readonly ConsoleLifetime consoleLifetime;
|
||||
|
||||
public DebugWarmupLifetime
|
||||
(
|
||||
IOptions<ConsoleLifetimeOptions> options,
|
||||
IHostEnvironment environment,
|
||||
IHostApplicationLifetime applicationLifetime,
|
||||
IOptions<HostOptions> hostOptions,
|
||||
ILoggerFactory loggerFactory
|
||||
)
|
||||
{
|
||||
this.consoleLifetime = new ConsoleLifetime(options, environment, applicationLifetime, hostOptions, loggerFactory);
|
||||
this.ApplicationLifetime = applicationLifetime;
|
||||
}
|
||||
|
||||
public static void OnApplicationStarted()
|
||||
{
|
||||
using HttpClient client = new();
|
||||
|
||||
Logger.Log("Warming up Hot Reload...", LoggerLevelStartup.Instance);
|
||||
client.GetAsync("http://localhost:10060/").Wait();
|
||||
Logger.Log("Hot Reload is ready to go!", LoggerLevelStartup.Instance);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => this.consoleLifetime.StopAsync(cancellationToken);
|
||||
|
||||
public Task WaitForStartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
this.applicationStartedRegistration = this.ApplicationLifetime.ApplicationStarted.Register((Action<object>)(_ => OnApplicationStarted()), (object)this);
|
||||
|
||||
return this.consoleLifetime.WaitForStartAsync(cancellationToken);
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Http;
|
|||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Startup;
|
||||
|
@ -53,6 +54,12 @@ public class Startup
|
|||
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
||||
}
|
||||
);
|
||||
|
||||
#if DEBUG
|
||||
services.AddSingleton<IHostLifetime, DebugWarmupLifetime>();
|
||||
#else
|
||||
services.AddSingleton<IHostLifetime, ConsoleLifetime>();
|
||||
#endif
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue