feat: Integrated SPA resources with ASP.NET Core.

This commit is contained in:
real-zony
2022-10-05 21:10:15 +08:00
parent f57afd4238
commit bccfaaaa5b
5 changed files with 43 additions and 24 deletions

View File

@@ -1,3 +1,4 @@
using System.Diagnostics;
using ZonyLrcTools.LocalServer;
var app = RegisterAndConfigureServices();
@@ -12,25 +13,42 @@ async Task ListenServices()
WebApplication? RegisterAndConfigureServices()
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.WebHost.ConfigureKestrel(k => k.ListenAnyIP(50002));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
var insideApp = builder.Build();
insideApp.UseSpaStaticFiles(new StaticFileOptions
{
app.UseSwagger();
app.UseSwaggerUI();
RequestPath = "",
FileProvider = new Microsoft.Extensions.FileProviders
.ManifestEmbeddedFileProvider(
typeof(Program).Assembly, "UiStaticResources"
)
});
insideApp.UseAuthorization();
insideApp.MapControllers();
insideApp.Lifetime.ApplicationStarted.Register(OpenBrowser);
return insideApp;
}
void OpenBrowser()
{
const string url = "http://localhost:50002/index.html";
if (OperatingSystem.IsWindows())
{
Process.Start("explorer.exe", url);
}
else if (OperatingSystem.IsMacOS())
{
Process.Start("open", url);
}
else if (OperatingSystem.IsLinux())
{
Process.Start("xdg-open", url);
}
app.UseAuthorization();
app.MapControllers();
return app;
}

View File

@@ -5,7 +5,6 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:50002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"

View File

@@ -4,23 +4,26 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.9" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="6.0.9" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="SuperSocket.WebSocket" Version="2.0.0-beta.11" />
<PackageReference Include="SuperSocket.WebSocket.Server" Version="2.0.0-beta.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers" />
<Folder Include="Controllers" />
<EmbeddedResource Include="UiStaticResources\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZonyLrcTools.Cli\ZonyLrcTools.Cli.csproj" />
</ItemGroup>
<!-- <ItemGroup>-->
<!-- <ProjectReference Include="..\ZonyLrcTools.Cli\ZonyLrcTools.Cli.csproj" />-->
<!-- </ItemGroup>-->
</Project>