Skip to content

Commit

Permalink
Upgraded HostedContent to .NET 8 WebApplication (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemonify authored Dec 6, 2023
1 parent ab4a519 commit 9540c6d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 54 deletions.
71 changes: 31 additions & 40 deletions src/SharpWebview/Content/HostedContent.cs
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();
}
}
}
6 changes: 3 additions & 3 deletions src/SharpWebview/SharpWebview.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>SharpWebview</AssemblyName>
<Authors>Gerrit 'Geaz' Gazic</Authors>
Expand Down Expand Up @@ -33,9 +33,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
11 changes: 0 additions & 11 deletions src/SharpWebview/StartupParameters.cs

This file was deleted.

0 comments on commit 9540c6d

Please sign in to comment.