last ...
This commit is contained in:
@@ -21,6 +21,7 @@ using System.Security.Cryptography;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.RegularExpressions;
|
||||
using xCommons.Configurations;
|
||||
|
||||
namespace xCommons.Extensions
|
||||
{
|
||||
@@ -1820,6 +1821,65 @@ namespace xCommons.Extensions
|
||||
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 ...
|
||||
public static bool IsValidIntId(this int source)
|
||||
@@ -1925,6 +1985,39 @@ namespace xCommons.Extensions
|
||||
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
|
||||
|
||||
//
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 ();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user