From ab64a843fcdea651ff7ceb54540a2e1f89efb683 Mon Sep 17 00:00:00 2001 From: Meleki Date: Sun, 15 Mar 2026 10:57:14 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20AppPaths.WwwRoot=20=E2=80=93=20zent?= =?UTF-8?q?rale=20wwwroot-Pfadaufl=C3=B6sung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Neue statische Klasse AppPaths mit WwwRoot-Property - Dev-Modus: ../../../wwwroot (Quell-Verzeichnis) - Publish-Modus: Fallback auf BaseDirectory/wwwroot - Program.cs + RemoteEndpoint.cs nutzen AppPaths statt eigener Logik --- Endpoints/RemoteEndpoint.cs | 2 +- Program.cs | 6 +++--- Services/AppPaths.cs | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 Services/AppPaths.cs diff --git a/Endpoints/RemoteEndpoint.cs b/Endpoints/RemoteEndpoint.cs index 7c90d2d..992ceee 100644 --- a/Endpoints/RemoteEndpoint.cs +++ b/Endpoints/RemoteEndpoint.cs @@ -241,7 +241,7 @@ public static class RemoteEndpoint private static async Task ServeScript(HttpContext ctx) { - var scriptPath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "remote.ps1"); + var scriptPath = Path.Combine(AppPaths.WwwRoot, "remote.ps1"); if (!File.Exists(scriptPath)) return Results.NotFound("Script nicht gefunden"); diff --git a/Program.cs b/Program.cs index 0da72a5..a0f705c 100644 --- a/Program.cs +++ b/Program.cs @@ -138,10 +138,10 @@ await app.Services.GetRequiredService().LoadModelsAsync(); app.UseWebSockets(); // wwwroot: Quell-Verzeichnis bevorzugen (Live-Änderungen ohne Publish) -var devWwwroot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "wwwroot")); -if (Directory.Exists(devWwwroot)) +AppPaths.Initialize(); +if (AppPaths.WwwRoot != Path.Combine(AppContext.BaseDirectory, "wwwroot")) { - var fileProvider = new PhysicalFileProvider(devWwwroot); + var fileProvider = new PhysicalFileProvider(AppPaths.WwwRoot); app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = fileProvider }); app.UseStaticFiles(new StaticFileOptions { FileProvider = fileProvider }); } diff --git a/Services/AppPaths.cs b/Services/AppPaths.cs new file mode 100644 index 0000000..ec29a78 --- /dev/null +++ b/Services/AppPaths.cs @@ -0,0 +1,28 @@ +using System; +using System.IO; + +namespace Gott.Services; + +/// +/// Zentrale Pfade der Anwendung. Werden beim Start in Program.cs gesetzt. +/// +public static class AppPaths +{ + /// + /// Absoluter Pfad zum wwwroot-Verzeichnis. + /// Im Dev-Modus das Quell-Verzeichnis (../../../wwwroot), + /// im Publish-Modus das Verzeichnis neben der Executable. + /// + public static string WwwRoot { get; private set; } + + /// + /// Initialisiert die Pfade. Wird einmal beim Start aufgerufen. + /// + public static void Initialize() + { + var devPath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "wwwroot")); + WwwRoot = Directory.Exists(devPath) + ? devPath + : Path.Combine(AppContext.BaseDirectory, "wwwroot"); + } +}