feat: automatically run vite dev server when in Debugger mode. feat: supports hotreload on both js frontend and .net backend.
91 lines
2.6 KiB
C#
91 lines
2.6 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.ResponseCompression;
|
|
using System.Diagnostics;
|
|
using System.IO.Compression;
|
|
using System.Text.Json.Serialization;
|
|
using WebApplication1.Controllers;
|
|
|
|
namespace WebApplication1;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
#if DEBUG
|
|
if (Debugger.IsAttached)
|
|
{
|
|
var sourcePath = Path.Combine(AppContext.BaseDirectory, "../../../Source");
|
|
|
|
Process.Start(new ProcessStartInfo
|
|
{
|
|
FileName = "cmd.exe",
|
|
Arguments = $"/k \"cd /d \"{sourcePath}\" && npm run dev:vite\"",
|
|
UseShellExecute = true
|
|
});
|
|
Thread.Sleep(1000);
|
|
}
|
|
#endif
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddSingleton<ViteHelper>();
|
|
|
|
builder.Services
|
|
.AddControllers((options) =>
|
|
{
|
|
options.CacheProfiles.Add("Default", new CacheProfile
|
|
{
|
|
Duration = 2592000,
|
|
Location = ResponseCacheLocation.Any,
|
|
NoStore = false
|
|
});
|
|
options.CacheProfiles.Add("NoCache", new CacheProfile
|
|
{
|
|
Location = ResponseCacheLocation.None,
|
|
NoStore = true
|
|
});
|
|
if (!builder.Environment.IsDevelopment())
|
|
{
|
|
options.Filters.Add<PageExceptionFilterAttribute>();
|
|
}
|
|
})
|
|
.AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|
});
|
|
|
|
builder.Services
|
|
.AddResponseCompression(options =>
|
|
{
|
|
options.Providers.Add<BrotliCompressionProvider>();
|
|
options.Providers.Add<GzipCompressionProvider>();
|
|
options.MimeTypes = ["application/json"];
|
|
options.EnableForHttps = true;
|
|
})
|
|
.Configure<BrotliCompressionProviderOptions>(options =>
|
|
{
|
|
options.Level = CompressionLevel.Optimal;
|
|
})
|
|
.AddResponseCaching();
|
|
|
|
builder.WebHost.UseIIS();
|
|
|
|
var app = builder.Build();
|
|
app.UseStaticFiles();
|
|
|
|
#if DEBUG
|
|
app.UseDeveloperExceptionPage();
|
|
#endif
|
|
|
|
// Configure the HTTP request pipeline.
|
|
app.UseRouting();
|
|
app.UseAuthorization();
|
|
app.UseResponseCompression();
|
|
app.UseResponseCaching();
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
}
|