-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgraded HostedContent to .NET 8 WebApplication (#35)
- Loading branch information
Showing
3 changed files
with
34 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,58 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.Server.Features; | ||
using Microsoft.AspNetCore.StaticFiles; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
|
||
namespace SharpWebview.Content | ||
{ | ||
public sealed class HostedContent : IWebviewContent | ||
{ | ||
private readonly IWebHost _webHost; | ||
private readonly WebApplication _webApp; | ||
|
||
public HostedContent(int port = 0, bool activateLog = false, IDictionary<string, string> additionalMimeTypes = null) | ||
{ | ||
_webHost = WebHost.CreateDefaultBuilder() | ||
.ConfigureServices(s => s.AddSingleton(x => new StartupParameters() { AdditionalMimeTypes = additionalMimeTypes })) | ||
.UseStartup<Startup>() | ||
.UseKestrel(options => options.Listen(IPAddress.Loopback, port)) | ||
.ConfigureLogging((logger) => { if(!activateLog) logger.ClearProviders(); }) | ||
.Build(); | ||
_webHost.Start(); | ||
} | ||
WebApplicationBuilder builder = WebApplication.CreateBuilder(); | ||
builder.WebHost.UseKestrel(options => options.Listen(IPAddress.Loopback, port)); | ||
|
||
public string ToWebviewUrl() | ||
{ | ||
return _webHost.ServerFeatures | ||
.Get<IServerAddressesFeature>() | ||
.Addresses | ||
.First(); | ||
} | ||
} | ||
if (!activateLog) | ||
{ | ||
builder.Logging.ClearProviders(); | ||
} | ||
|
||
internal sealed class Startup | ||
{ | ||
private StartupParameters startupParameters; | ||
public Startup(StartupParameters startupParameters) | ||
{ | ||
this.startupParameters = startupParameters; | ||
} | ||
public void ConfigureServices(IServiceCollection services) { } | ||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
var fileServerOptions = new FileServerOptions | ||
_webApp = builder.Build(); | ||
|
||
FileServerOptions fileServerOptions = new FileServerOptions | ||
{ | ||
FileProvider = new PhysicalFileProvider(Path.Combine(System.Environment.CurrentDirectory, "app")), | ||
RequestPath = "", | ||
EnableDirectoryBrowsing = true, | ||
}; | ||
if(startupParameters.AdditionalMimeTypes != null) | ||
|
||
if (additionalMimeTypes != null) | ||
{ | ||
var extensionProvider = new FileExtensionContentTypeProvider(); | ||
foreach (var mimeType in startupParameters.AdditionalMimeTypes) | ||
FileExtensionContentTypeProvider extensionProvider = new FileExtensionContentTypeProvider(); | ||
|
||
foreach (var mimeType in additionalMimeTypes) | ||
{ | ||
extensionProvider.Mappings.Add(mimeType); | ||
} | ||
|
||
fileServerOptions.StaticFileOptions.ContentTypeProvider = extensionProvider; | ||
} | ||
app.UseFileServer(fileServerOptions); | ||
|
||
_webApp.UseFileServer(fileServerOptions); | ||
_webApp.Start(); | ||
} | ||
|
||
public string ToWebviewUrl() | ||
{ | ||
return _webApp.Urls.First(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.