diff --git a/Endpoint/Common/Services/UrlHelper.cs b/Endpoint/Common/Services/UrlHelper.cs index 9769a25..3de08b9 100644 --- a/Endpoint/Common/Services/UrlHelper.cs +++ b/Endpoint/Common/Services/UrlHelper.cs @@ -6,6 +6,9 @@ namespace Mirea.Api.Endpoint.Common.Services; public static class UrlHelper { + public static string GetCurrentScheme(this HttpContext context) => + context.Request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? context.Request.Scheme; + public static string GetCurrentDomain(this HttpContext context) => context.Request.Headers["X-Forwarded-Host"].FirstOrDefault() ?? context.Request.Host.Host; @@ -38,4 +41,15 @@ public static class UrlHelper } public static string GetSubPathSwagger => CreateSubPath(Environment.GetEnvironmentVariable("SWAGGER_SUB_PATH")); + + public static string GetApiUrl(this HttpContext context, string apiPath = "") + { + var scheme = GetCurrentScheme(context); + var domain = GetCurrentDomain(context).TrimEnd('/'); + + var port = context.Request.Host.Port; + var portString = port.HasValue && port != 80 && port != 443 ? $":{port}" : string.Empty; + + return $"{scheme}://{domain}{portString}{GetSubPathWithoutFirstApiName}{apiPath.Trim('/')}"; + } } \ No newline at end of file