Add BASE_PATH environment variable for the base path setting.
This commit is contained in:
parent
388411748a
commit
dfd97b2c3c
2 changed files with 11 additions and 10 deletions
|
|
@ -1,19 +1,20 @@
|
|||
using System.Text.RegularExpressions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using RdtClient.Data.Models.Internal;
|
||||
|
||||
namespace RdtClient.Service.Middleware;
|
||||
|
||||
public class BaseHrefMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly String _basePath;
|
||||
|
||||
public BaseHrefMiddleware(RequestDelegate next)
|
||||
public BaseHrefMiddleware(RequestDelegate next, String basePath)
|
||||
{
|
||||
_next = next;
|
||||
_basePath = $"/{basePath.TrimStart('/').TrimEnd('/')}/";
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context, AppSettings appSettings)
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var originalBody = context.Response.Body;
|
||||
|
||||
|
|
@ -32,12 +33,10 @@ public class BaseHrefMiddleware
|
|||
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
||||
if (context.Response.ContentType?.Contains("text/html") == true)
|
||||
{
|
||||
var basePath = $"/{appSettings.BasePath!.TrimStart('/').TrimEnd('/')}/";
|
||||
responseBody = Regex.Replace(responseBody, @"<base href=""/""", @$"<base href=""{_basePath}""");
|
||||
|
||||
responseBody = Regex.Replace(responseBody, @"<base href=""/""", @$"<base href=""{basePath}""");
|
||||
|
||||
responseBody = Regex.Replace(responseBody, "(<script.*?src=\")(.*?)(\".*?</script>)", $"$1{basePath}$2$3");
|
||||
responseBody = Regex.Replace(responseBody, "(<link.*?href=\")(.*?)(\".*?>)", $"$1{basePath}$2$3");
|
||||
responseBody = Regex.Replace(responseBody, "(<script.*?src=\")(.*?)(\".*?</script>)", $"$1{_basePath}$2$3");
|
||||
responseBody = Regex.Replace(responseBody, "(<link.*?href=\")(.*?)(\".*?>)", $"$1{_basePath}$2$3");
|
||||
|
||||
context.Response.Headers.Remove("Content-Length");
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
|
|
|
|||
|
|
@ -164,9 +164,11 @@ try
|
|||
}
|
||||
});
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(appSettings.BasePath))
|
||||
var basePath = !String.IsNullOrWhiteSpace(appSettings.BasePath) ? appSettings.BasePath : !String.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("BASE_PATH")) ? Environment.GetEnvironmentVariable("BASE_PATH") : null;
|
||||
|
||||
if (basePath != null)
|
||||
{
|
||||
app.UseMiddleware<BaseHrefMiddleware>();
|
||||
app.UseMiddleware<BaseHrefMiddleware>(basePath);
|
||||
}
|
||||
|
||||
app.UseMiddleware<RequestLoggingMiddleware>();
|
||||
|
|
|
|||
Loading…
Reference in a new issue