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

2
.gitignore vendored
View File

@ -450,3 +450,5 @@ fabric.properties
.idea/ .idea/
/.idea /.idea
/src/ZonyLrcTools.Cli/TempFiles/ /src/ZonyLrcTools.Cli/TempFiles/
src/ZonyLrcTools.LocalServer/UiStaticResources/*

View File

@ -6,9 +6,6 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C29FB05C-54B1-4020-94D2-87E192EB2F98}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C29FB05C-54B1-4020-94D2-87E192EB2F98}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AF8ADB2F-E46C-4DEE-8316-652D9FE1A69B}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AF8ADB2F-E46C-4DEE-8316-652D9FE1A69B}"
ProjectSection(SolutionItems) = preProject
tests\.gitignore = tests\.gitignore
EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Cli", "src\ZonyLrcTools.Cli\ZonyLrcTools.Cli.csproj", "{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Cli", "src\ZonyLrcTools.Cli\ZonyLrcTools.Cli.csproj", "{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}"
EndProject EndProject

View File

@ -1,3 +1,4 @@
using System.Diagnostics;
using ZonyLrcTools.LocalServer; using ZonyLrcTools.LocalServer;
var app = RegisterAndConfigureServices(); var app = RegisterAndConfigureServices();
@ -12,25 +13,42 @@ async Task ListenServices()
WebApplication? RegisterAndConfigureServices() WebApplication? RegisterAndConfigureServices()
{ {
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(k => k.ListenAnyIP(50002));
// Add services to the container.
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build(); var insideApp = builder.Build();
insideApp.UseSpaStaticFiles(new StaticFileOptions
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); RequestPath = "",
app.UseSwaggerUI(); FileProvider = new Microsoft.Extensions.FileProviders
} .ManifestEmbeddedFileProvider(
typeof(Program).Assembly, "UiStaticResources"
)
});
app.UseAuthorization(); insideApp.UseAuthorization();
app.MapControllers(); insideApp.MapControllers();
insideApp.Lifetime.ApplicationStarted.Register(OpenBrowser);
return app; 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);
}
} }

View File

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

View File

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