From 2dcd495da7c95f0555dbde60e692ec404f8c06ad Mon Sep 17 00:00:00 2001 From: Flaminel Date: Fri, 20 Jun 2025 14:57:20 +0300 Subject: [PATCH] fixed default base path --- .../DependencyInjection/ApiDI.cs | 41 ++++++++++++++++++- .../app/core/services/base-path.service.ts | 9 +++- code/frontend/src/index.html | 17 ++++++-- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs b/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs index 6fb5c203..ac4d9bba 100644 --- a/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs +++ b/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs @@ -5,6 +5,7 @@ using Cleanuparr.Infrastructure.Hubs; using Cleanuparr.Infrastructure.Logging; using Microsoft.AspNetCore.Http.Json; using Microsoft.OpenApi.Models; +using System.Text; namespace Cleanuparr.Api.DependencyInjection; @@ -100,8 +101,44 @@ public static class ApiDI app.UseAuthorization(); app.MapControllers(); - // SPA fallback - must be last - app.MapFallbackToFile("index.html"); + // Custom SPA fallback to inject base path + app.MapFallback(async context => + { + var basePath = app.Configuration.GetValue("BASE_PATH") ?? "/"; + + // Normalize the base path (remove trailing slash if not root) + if (basePath != "/" && basePath.EndsWith("/")) + { + basePath = basePath.TrimEnd('/'); + } + + var webRoot = app.Environment.WebRootPath ?? Path.Combine(app.Environment.ContentRootPath, "wwwroot"); + var indexPath = Path.Combine(webRoot, "index.html"); + + if (!File.Exists(indexPath)) + { + context.Response.StatusCode = 404; + await context.Response.WriteAsync("index.html not found"); + return; + } + + var indexContent = await File.ReadAllTextAsync(indexPath); + + // Inject the base path into the HTML + var scriptInjection = $@" + "; + + // Insert the script right before the existing script tag + indexContent = indexContent.Replace( + "