MireaBackend/Endpoint/Common/Services/UrlHelper.cs

46 lines
1.3 KiB
C#
Raw Permalink Normal View History

2024-07-05 01:35:19 +03:00
using Microsoft.AspNetCore.Http;
using System;
using System.Linq;
namespace Mirea.Api.Endpoint.Common.Services;
public static class UrlHelper
{
2024-08-10 23:11:43 +03:00
public static string GetCurrentDomain(this HttpContext context) =>
2024-07-05 01:58:14 +03:00
context.Request.Headers["X-Forwarded-Host"].FirstOrDefault() ?? context.Request.Host.Host;
2024-07-05 01:35:19 +03:00
private static string CreateSubPath(string? path)
{
if (string.IsNullOrEmpty(path))
return "/";
return "/" + path.Trim('/') + "/";
}
public static string GetSubPath => CreateSubPath(Environment.GetEnvironmentVariable("ACTUAL_SUB_PATH"));
2024-07-05 01:59:36 +03:00
public static string GetSubPathWithoutFirstApiName
{
get
{
var path = GetSubPath;
if (string.IsNullOrEmpty(path) || path == "/")
return CreateSubPath(null);
var parts = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
if (!parts[i].Equals("api", StringComparison.CurrentCultureIgnoreCase)) continue;
parts = parts.Take(i).Concat(parts.Skip(i + 1)).ToArray();
break;
}
return CreateSubPath(string.Join("/", parts));
}
}
2024-07-05 01:35:19 +03:00
public static string GetSubPathSwagger => CreateSubPath(Environment.GetEnvironmentVariable("SWAGGER_SUB_PATH"));
}