fixed default base path

This commit is contained in:
Flaminel
2025-06-20 14:57:20 +03:00
parent 38b7d1d4bb
commit 2dcd495da7
3 changed files with 60 additions and 7 deletions

View File

@@ -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<string>("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 = $@"
<script>
window['_server_base_path'] = '{basePath}';
</script>";
// Insert the script right before the existing script tag
indexContent = indexContent.Replace(
" <script>",
scriptInjection + "\n <script>"
);
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(indexContent, Encoding.UTF8);
});
// Map SignalR hubs
app.MapHub<HealthStatusHub>("/api/hubs/health");

View File

@@ -14,8 +14,8 @@ export class BasePathService {
return `http://localhost:11011`;
}
// Fallback to window value if environment hasn't been updated yet
return (window as any)['_app_base'] || '/api';
// Use the server-injected base path or fallback to root
return (window as any)['_server_base_path'] || '/';
}
/**
@@ -35,6 +35,11 @@ export class BasePathService {
const basePath = this.getBasePath();
const cleanApiPath = apiPath.startsWith('/') ? apiPath : '/' + apiPath;
// In development mode, return full URL directly
if (isDevMode()) {
return basePath + '/api' + cleanApiPath;
}
return basePath === '/' ? '/api' + cleanApiPath : basePath + '/api' + cleanApiPath;
}
}

View File

@@ -5,9 +5,20 @@
<title>UI</title>
<script>
(function() {
// Extract base path from URL (everything before the Angular app)
const pathSegments = window.location.pathname.split('/').filter(segment => segment);
const basePath = pathSegments.length > 0 ? '/' + pathSegments[0] : '/';
// Check if we're running with a custom base path
// This should be injected by the server when serving the index.html
// For now, we'll detect if we're at root level or have a configured base path
let basePath = '/';
// If the server has injected a base path, use it
if (window['_server_base_path']) {
basePath = window['_server_base_path'];
} else {
// For development or when no base path is configured, always use root
// Don't try to extract base path from current URL as that breaks SPA routing
basePath = '/';
}
// Store for Angular to use
window['_app_base'] = basePath;