This commit is contained in:
2026-11-13 13:48:10 +03:30
parent 5e31f8f99a
commit 61d8ad831e
3 changed files with 93 additions and 80 deletions
+93
View File
@@ -21,6 +21,7 @@ using System.Security.Cryptography;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using xCommons.Configurations;
namespace xCommons.Extensions namespace xCommons.Extensions
{ {
@@ -1820,6 +1821,65 @@ namespace xCommons.Extensions
return result; return result;
} }
/// <summary>
/// Extract RateLimit Rule based on given Path ...
/// </summary>
/// <param name="source"></param>
/// <param name="path"></param>
/// <returns></returns>
public static XRateLimitRule GetMatchingRule(
this XRateLimitConfiguration source,
string path
)
{
//
if (!source.Enabled)
{
return null;
}
//
var normalizedPath = path.IsNullOrEmpty()
? "/"
: path.ToNormalString();
//
var rule = source.Rules.IsNull()
? null
: source.Rules
.Where(r =>
!r.IsNull() &&
r.Enabled &&
!r.PathPrefix.IsNullOrEmpty() &&
normalizedPath.StartsWith(r.PathPrefix.ToNormalString()))
.OrderByDescending(r => r.PathPrefix.Length)
.FirstOrDefault();
//
if (!rule.IsNull())
{
return rule;
}
//
if (source.ApplyToAllRoutes)
{
//
return new XRateLimitRule
{
Enabled = true,
Name = "Default",
PathPrefix = "/",
Limit = source.DefaultLimit,
KeyMode = source.DefaultKeyMode,
PeriodSeconds = source.DefaultPeriodSeconds,
};
}
//
return null;
}
// //
#region Int Id Extensions ... #region Int Id Extensions ...
public static bool IsValidIntId(this int source) public static bool IsValidIntId(this int source)
@@ -1925,6 +1985,39 @@ namespace xCommons.Extensions
return Convert.ToBase64String(hash); return Convert.ToBase64String(hash);
} }
} }
/// <summary>
/// Apply Templates on provided String ...
/// </summary>
/// <param name="source"></param>
/// <param name="templates"></param>
/// <returns></returns>
public static string ApplyTemplates(
this string source,
IDictionary<string, string> templates = null
)
{
//
// Validate ...
if (source.IsNullOrEmpty() ||
!templates.HasChild())
{
return source;
}
//
var existsTemplates = templates.Where(
x => source.Contains(x.Key)
);
var result = source;
foreach (var template in existsTemplates)
{
result = result.Replace(template.Key, template.Value);
}
//
return result;
}
#endregion #endregion
// //
-67
View File
@@ -1,67 +0,0 @@
using System.Linq;
using xCommons.Configurations;
namespace xCommons.Extensions
{
public static class XCommonsExtensions
{
/// <summary>
/// Extract RateLimit Rule based on given Path ...
/// </summary>
/// <param name="source"></param>
/// <param name="path"></param>
/// <returns></returns>
public static XRateLimitRule GetMatchingRule(
this XRateLimitConfiguration source,
string path
)
{
//
if (!source.Enabled)
{
return null;
}
//
var normalizedPath = path.IsNullOrEmpty()
? "/"
: path.ToNormalString();
//
var rule = source.Rules.IsNull()
? null
: source.Rules
.Where(r =>
!r.IsNull() &&
r.Enabled &&
!r.PathPrefix.IsNullOrEmpty() &&
normalizedPath.StartsWith(r.PathPrefix.ToNormalString()))
.OrderByDescending(r => r.PathPrefix.Length)
.FirstOrDefault();
//
if (!rule.IsNull())
{
return rule;
}
//
if (source.ApplyToAllRoutes)
{
//
return new XRateLimitRule
{
Enabled = true,
Name = "Default",
PathPrefix = "/",
Limit = source.DefaultLimit,
KeyMode = source.DefaultKeyMode,
PeriodSeconds = source.DefaultPeriodSeconds,
};
}
//
return null;
}
}
}
-13
View File
@@ -1,13 +0,0 @@
using System;
namespace xCommons.Helpers {
public partial class XDateHelper {
/// <summary>
/// retrieve utc now timestam unix like representation ...
/// </summary>
/// <returns></returns>
public static long UtcNowTimespan () {
return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds ();
}
}
}