-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f52ed8
commit 66a042a
Showing
22 changed files
with
216 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,4 +224,5 @@ coverage.opencover.xml | |
*.pdb | ||
|
||
# Run Tests | ||
BenchmarkDotNet.Artifacts/ | ||
BenchmarkDotNet.Artifacts/ | ||
TestResults/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;</TargetFrameworks> | ||
<TargetFrameworks>net6.0;net7.0;net8.0;</TargetFrameworks> | ||
<MicrosoftPackageVersion Condition="'$(TargetFramework)' == 'net6.0'">6.0.0</MicrosoftPackageVersion> | ||
<MicrosoftPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">7.0.0</MicrosoftPackageVersion> | ||
<MicrosoftPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">8.0.0</MicrosoftPackageVersion> | ||
<MasaFrameworkPackageVersion>1.2.0-preview.5</MasaFrameworkPackageVersion> | ||
|
||
<MSTestPackageVersion Condition="'$(TargetFramework)' == 'net6.0'">2.2.3</MSTestPackageVersion> | ||
<MSTestPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">2.2.3</MSTestPackageVersion> | ||
<MSTestPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">3.6.2</MSTestPackageVersion> | ||
|
||
<MicrosoftTeskSdkPackageVersion Condition="'$(TargetFramework)' == 'net6.0'">16.9.4</MicrosoftTeskSdkPackageVersion> | ||
<MicrosoftTeskSdkPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">16.9.4</MicrosoftTeskSdkPackageVersion> | ||
<MicrosoftTeskSdkPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">17.12.0</MicrosoftTeskSdkPackageVersion> | ||
|
||
<CoverletPackageVersion Condition="'$(TargetFramework)' == 'net6.0'">3.0.2</CoverletPackageVersion> | ||
<CoverletPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">3.0.2</CoverletPackageVersion> | ||
<CoverletPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">3.1.2</CoverletPackageVersion> | ||
|
||
<MasaFrameworkPackageVersion>1.2.0-preview.5</MasaFrameworkPackageVersion> | ||
<OpenTelemetryVersion>1.9.0</OpenTelemetryVersion> | ||
<OpenTelemetryContribVersion>1.9.0</OpenTelemetryContribVersion> | ||
|
||
<FluentValidationPackageVersion>11.1.0</FluentValidationPackageVersion> | ||
<IdentityModelPackageVersion>6.0.0</IdentityModelPackageVersion> | ||
|
||
<NESTPackageVersion>7.17.4</NESTPackageVersion> | ||
<MoqPackageVersion>4.16.1</MoqPackageVersion> | ||
<MSTestPackageVersion>2.2.3</MSTestPackageVersion> | ||
<CoverletPackageVersion>3.0.2</CoverletPackageVersion> | ||
<MicrosoftTeskSdkPackageVersion>16.9.4</MicrosoftTeskSdkPackageVersion> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Blazor; | ||
|
||
internal class BlazorRouteData | ||
{ | ||
public string Template { get; set; } = default!; | ||
|
||
public Type Type { get; set; } = default!; | ||
|
||
public List<BlazorRouteParamKey> RouteKeies { get; set; } = new(); | ||
|
||
public int MinCount => RouteKeies.Count(k => !k.CanNull); | ||
|
||
public int MaxCount => RouteKeies.Count; | ||
|
||
public bool IsMatch(string url) | ||
{ | ||
if (string.IsNullOrEmpty(url)) | ||
return false; | ||
var paths = url[1..].Split('/'); | ||
if (!(MinCount - paths.Length == 0 || MaxCount - paths.Length == 0)) | ||
return false; | ||
var index = 0; | ||
foreach (var param in paths) | ||
{ | ||
if (RouteKeies[index].IsParamter) | ||
continue; | ||
if (!string.Equals(RouteKeies[index].Value, param, StringComparison.CurrentCultureIgnoreCase)) | ||
return false; | ||
index++; | ||
} | ||
return true; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Blazor; | ||
|
||
public sealed class BlazorRouteManager | ||
{ | ||
private BlazorRouteManager() { } | ||
|
||
private static Dictionary<Type, List<BlazorRouteData>> _routes = new(); | ||
|
||
internal static bool EnableBlazorRoute { get; private set; } | ||
|
||
public static void InitRoute(params Assembly[] assemblies) | ||
{ | ||
if (assemblies == null || assemblies.Length == 0) | ||
throw new ArgumentNullException(nameof(assemblies)); | ||
|
||
EnableBlazorRoute = true; | ||
foreach (var assembly in assemblies) | ||
{ | ||
InitRoute(assembly); | ||
} | ||
} | ||
|
||
private static void InitRoute(Assembly assembly) | ||
{ | ||
if (assembly == null) | ||
return; | ||
|
||
var typeRoutes = assembly.GetTypes().Select(type => new { type, routes = type.GetCustomAttributes<RouteAttribute>().ToList() }).Where(item => item.routes.Count > 0).ToList(); | ||
|
||
foreach (var item in typeRoutes) | ||
{ | ||
if (_routes.ContainsKey(item.type)) | ||
continue; | ||
var routeDatas = item.routes.Select(route => | ||
{ | ||
var values = (route.Template.StartsWith('/') ? route.Template[1..] : route.Template).Split('/'); | ||
return new BlazorRouteData | ||
{ | ||
Template = route.Template, | ||
Type = item.type, | ||
RouteKeies = values.Select((value, index) => new BlazorRouteParamKey | ||
{ | ||
Value = value, | ||
Index = index, | ||
IsParamter = IsParamter(value), | ||
CanNull = CanNull(value, index == values.Length - 1) | ||
} | ||
).ToList() | ||
}; | ||
}).ToList(); | ||
_routes.Add(item.type, routeDatas); | ||
} | ||
} | ||
|
||
internal static bool TryGetUrlRoute(string url, out BlazorRouteData? route) | ||
{ | ||
route = default; | ||
foreach (var type in _routes.Keys) | ||
{ | ||
var routes = _routes[type]; | ||
var matches = routes.Where(route => route.IsMatch(url)).OrderByDescending(item => item.MaxCount); | ||
if (matches.Any()) | ||
{ | ||
route = matches.First(); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static bool IsParamter(string value) | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
throw new ArgumentNullException(nameof(value)); | ||
return Regex.IsMatch(value, @"^\{\w+:?\w+\??\}$"); | ||
} | ||
|
||
private static bool CanNull(string value, bool isLast) | ||
{ | ||
return isLast && value.Contains('?'); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteParamKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Blazor; | ||
|
||
internal class BlazorRouteParamKey | ||
{ | ||
public string Value { get; set; } = default!; | ||
|
||
public int Index { get; set; } | ||
|
||
public bool IsParamter { get; set; } | ||
|
||
public bool CanNull { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.