Auto-set app-base meta from BasePath config at startup
This commit is contained in:
32
Program.cs
32
Program.cs
@@ -75,6 +75,7 @@ var basePath = builder.Configuration["BasePath"];
|
||||
if (!string.IsNullOrWhiteSpace(basePath))
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
UpdateIndexMetaBase(app.Environment, basePath);
|
||||
}
|
||||
|
||||
app.UseGlobalExceptionLogging();
|
||||
@@ -100,3 +101,34 @@ app.MapResultsEndpoints();
|
||||
app.MapAdminEndpoints();
|
||||
|
||||
app.Run();
|
||||
|
||||
static void UpdateIndexMetaBase(IWebHostEnvironment env, string basePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var indexPath = Path.Combine(env.WebRootPath, "index.html");
|
||||
if (!File.Exists(indexPath)) return;
|
||||
|
||||
var text = File.ReadAllText(indexPath);
|
||||
var marker = "name=\"app-base\"";
|
||||
var contentKey = "content=\"";
|
||||
var markerIndex = text.IndexOf(marker, StringComparison.OrdinalIgnoreCase);
|
||||
if (markerIndex < 0) return;
|
||||
var contentIndex = text.IndexOf(contentKey, markerIndex, StringComparison.OrdinalIgnoreCase);
|
||||
if (contentIndex < 0) return;
|
||||
var valueStart = contentIndex + contentKey.Length;
|
||||
var valueEnd = text.IndexOf('"', valueStart);
|
||||
if (valueEnd < 0) return;
|
||||
|
||||
var current = text[valueStart..valueEnd];
|
||||
var normalized = basePath.EndsWith('/') ? basePath.TrimEnd('/') : basePath;
|
||||
if (current == normalized) return;
|
||||
|
||||
var updated = text[..valueStart] + normalized + text[valueEnd..];
|
||||
File.WriteAllText(indexPath, updated);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If we can't rewrite, continue; frontend can still be set manually.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user