From 713bbfa16f497e507f89fb1e89a667d76545dc5b Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 4 Nov 2024 02:35:43 +0300 Subject: [PATCH] feat: add calculate correct api url --- Endpoint/Common/Services/UrlHelper.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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