Fix ASF trying to create www folder if it doesn't exist yet

It seems that ASP.NET is trying to create initialized WebRootPath if it doesn't exist yet. This might be unwanted, as user might want to explicitly disable www directory while still having interest in IPC. On top of that, ASF will outright crash if creating such directory will be impossible, e.g. because of insufficient permission.

It makes sense for us to check first if the directory exists - if not, we can omit it entirely, so ASP.NET will default to NullFileProvider and simply respond 404 to everything unhandled from the code perspective.

@SuperSandro2000 will resolve https://github.com/NixOS/nixpkgs/issues/312242 without a need of disabling IPC. In other words, you can use IPC with no www folder attached in order to still have ASF API and /swagger available. ASF will no longer crash in this scenario, it also won't try to create a directory on read-only filesystem.
This commit is contained in:
Łukasz Domeradzki
2024-07-10 00:18:44 +02:00
parent 7afcf82c32
commit b9beb6ec16

View File

@@ -435,15 +435,22 @@ internal static class ArchiKestrel {
} }
private static async Task<WebApplication> CreateWebApplication() { private static async Task<WebApplication> CreateWebApplication() {
string customDirectory = Path.Combine(Directory.GetCurrentDirectory(), SharedInfo.WebsiteDirectory); string? webRootPath = Path.Combine(Directory.GetCurrentDirectory(), SharedInfo.WebsiteDirectory);
string websiteDirectory = Directory.Exists(customDirectory) ? customDirectory : Path.Combine(AppContext.BaseDirectory, SharedInfo.WebsiteDirectory);
if (!Directory.Exists(webRootPath)) {
webRootPath = Path.Combine(AppContext.BaseDirectory, SharedInfo.WebsiteDirectory);
if (!Directory.Exists(webRootPath)) {
webRootPath = null;
}
}
// The order of dependency injection matters, pay attention to it // The order of dependency injection matters, pay attention to it
WebApplicationBuilder builder = WebApplication.CreateEmptyBuilder( WebApplicationBuilder builder = WebApplication.CreateEmptyBuilder(
new WebApplicationOptions { new WebApplicationOptions {
ApplicationName = SharedInfo.AssemblyName, ApplicationName = SharedInfo.AssemblyName,
ContentRootPath = SharedInfo.HomeDirectory, ContentRootPath = SharedInfo.HomeDirectory,
WebRootPath = websiteDirectory WebRootPath = webRootPath
} }
); );