Skip to content

Commit

Permalink
feat: add blazor server route match
Browse files Browse the repository at this point in the history
  • Loading branch information
Qinyouzeng committed Dec 24, 2024
1 parent 5f52ed8 commit 66a042a
Show file tree
Hide file tree
Showing 22 changed files with 216 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,5 @@ coverage.opencover.xml
*.pdb

# Run Tests
BenchmarkDotNet.Artifacts/
BenchmarkDotNet.Artifacts/
TestResults/
21 changes: 15 additions & 6 deletions Directory.Build.props
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>
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ namespace Masa.BuildingBlocks.StackSdks.Auth.Contracts.Model;

public class ThirdPartyIdpModel
{
public string ClientId { get; set; }
public string ClientId { get; set; } = default!;

public string ClientSecret { get; set; }
public string ClientSecret { get; set; } = default!;

public string Url { get; set; }
public string Url { get; set; } = default!;

public AuthenticationTypes VerifyType { get; set; }

public string Name { get; set; }
public string Name { get; set; } = default!;

public string DisplayName { get; set; }
public string DisplayName { get; set; } = default!;

public string Icon { get; set; }
public string Icon { get; set; } = default!;

public ThirdPartyIdpTypes ThirdPartyIdpType { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Masa.BuildingBlocks.StackSdks.Config.Models;

public class DbModel
{
public string Server { get; set; }
public string Server { get; set; } = default!;

public int Port { get; set; } = 1433;

public string Database { get; set; }
public string Database { get; set; } = default!;

public string UserId { get; set; }
public string UserId { get; set; } = default!;

public string Password { get; set; }
public string Password { get; set; } = default!;

public string? DbType { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Masa.BuildingBlocks.StackSdks.Config.Models;

public class ElasticModel
{
public List<string> Nodes { get; set; }
public List<string> Nodes { get; set; } = default!;

public string Index { get; set; }
public string Index { get; set; } = default!;

public string UserName { get; set; }
public string UserName { get; set; } = default!;

public string Password { get; set; }
public string Password { get; set; } = default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Masa.BuildingBlocks.StackSdks.Config.Models;

public class RedisModel
{
public string RedisHost { get; set; }
public string RedisHost { get; set; } = default!;

public int RedisPort { get; set; }

public int RedisDb { get; set; }

public string RedisPassword { get; set; }
public string RedisPassword { get; set; } = default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Masa.BuildingBlocks.StackSdks.Scheduler.Request;

public class GetSchedulerJobByIdentityRequest
{
public string JobIdentity { get; set; }
public string JobIdentity { get; set; } = default!;

public string ProjectIdentity { get; set; }
public string ProjectIdentity { get; set; } = default!;
}
2 changes: 1 addition & 1 deletion src/Contrib/Masa.Contrib.StackSdks.Auth/AuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public AuthClient(ICaller caller, IMultiEnvironmentUserContext userContext, IMul

public IThirdPartyIdpService ThirdPartyIdpService { get; }

public IOssService OssService { get; }
public IOssService OssService { get; } = default!;
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services,
{
var redisOptions = configuration.GetSection("$public.RedisConfig").Get<RedisConfigurationOptions>();
authServiceBaseAddress ??= configuration.GetValue<string>("$public.AppSettings:AuthClient:Url");
services.AddAuthClient(authServiceBaseAddress, redisOptions);
services.AddAuthClient(authServiceBaseAddress!, redisOptions!);

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,25 @@ private static Dictionary<string, string> GetConfigMap(IServiceCollection servic
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();

string environment = configuration.GetValue<string>(MasaStackConfigConstant.ENVIRONMENT);
environment = string.IsNullOrWhiteSpace(environment) ? configuration["ASPNETCORE_ENVIRONMENT"] : environment;
string environment = configuration.GetValue<string>(MasaStackConfigConstant.ENVIRONMENT)!;
environment = string.IsNullOrWhiteSpace(environment) ? configuration["ASPNETCORE_ENVIRONMENT"]! : environment;

var configs = new Dictionary<string, string>()
{
{ MasaStackConfigConstant.VERSION, configuration.GetValue<string>(MasaStackConfigConstant.VERSION) },
{ MasaStackConfigConstant.VERSION, configuration.GetValue<string>(MasaStackConfigConstant.VERSION)! },
{ MasaStackConfigConstant.IS_DEMO, configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() },
{ MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue<string>(MasaStackConfigConstant.DOMAIN_NAME) },
{ MasaStackConfigConstant.NAMESPACE, configuration.GetValue<string>(MasaStackConfigConstant.NAMESPACE) },
{ MasaStackConfigConstant.CLUSTER, configuration.GetValue<string>(MasaStackConfigConstant.CLUSTER) },
{ MasaStackConfigConstant.OTLP_URL, configuration.GetValue<string>(MasaStackConfigConstant.OTLP_URL) },
{ MasaStackConfigConstant.REDIS, configuration.GetValue<string>(MasaStackConfigConstant.REDIS) },
{ MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue<string>(MasaStackConfigConstant.CONNECTIONSTRING) },
{ MasaStackConfigConstant.MASA_STACK, configuration.GetValue<string>(MasaStackConfigConstant.MASA_STACK) },
{ MasaStackConfigConstant.ELASTIC, configuration.GetValue<string>(MasaStackConfigConstant.ELASTIC) },
{ MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue<string>(MasaStackConfigConstant.DOMAIN_NAME)! },
{ MasaStackConfigConstant.NAMESPACE, configuration.GetValue<string>(MasaStackConfigConstant.NAMESPACE)! },
{ MasaStackConfigConstant.CLUSTER, configuration.GetValue<string>(MasaStackConfigConstant.CLUSTER)! },
{ MasaStackConfigConstant.OTLP_URL, configuration.GetValue < string >(MasaStackConfigConstant.OTLP_URL)! },
{ MasaStackConfigConstant.REDIS, configuration.GetValue<string>(MasaStackConfigConstant.REDIS)! },
{ MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue<string>(MasaStackConfigConstant.CONNECTIONSTRING)! },
{ MasaStackConfigConstant.MASA_STACK, configuration.GetValue<string>(MasaStackConfigConstant.MASA_STACK)! },
{ MasaStackConfigConstant.ELASTIC, configuration.GetValue<string>(MasaStackConfigConstant.ELASTIC)! },
{ MasaStackConfigConstant.ENVIRONMENT, environment },
{ MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue<string>(MasaStackConfigConstant.ADMIN_PWD) },
{ MasaStackConfigConstant.DCC_SECRET, configuration.GetValue<string>(MasaStackConfigConstant.DCC_SECRET) },
{ MasaStackConfigConstant.SUFFIX_IDENTITY, configuration.GetValue<string>(MasaStackConfigConstant.SUFFIX_IDENTITY) }
{ MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue<string>(MasaStackConfigConstant.ADMIN_PWD)! },
{ MasaStackConfigConstant.DCC_SECRET, configuration.GetValue<string>(MasaStackConfigConstant.DCC_SECRET)! },
{ MasaStackConfigConstant.SUFFIX_IDENTITY, configuration.GetValue<string>(MasaStackConfigConstant.SUFFIX_IDENTITY)! }
};

return configs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Masa.Contrib.StackSdks.Isolation;

internal class EnvironmentModel : IEnvironmentModel
{
public string Environment { get; set; }
public string Environment { get; set; } = default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static IMasaConfigurationBuilder UseDccIsolation(this IMasaConfigurationB
configurationApiClient, loggerFactory));

var configurationBuilder = serviceProvider.GetService<IConfiguration>() as IConfigurationBuilder;
configurationBuilder.AddConfiguration(builder.Add(source).Build());
configurationBuilder!.AddConfiguration(builder.Add(source).Build());
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void SetData()
{
Data = _data.SelectMany(kv =>
kv.Value.GetPropertyNames().Select(key =>
new KeyValuePair<string, string>($"{kv.Key}{ConfigurationPath.KeyDelimiter}{key}", kv.Value.GetProperty(key)!)
new KeyValuePair<string, string?>($"{kv.Key}{ConfigurationPath.KeyDelimiter}{key}", kv.Value.GetProperty(key)!)
)
).ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase);
}
Expand Down
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;
}
}
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('?');
}
}
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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static IServiceCollection AddObservable(this IServiceCollection services,
string? otlpUrl = null,
bool isBlazor = false,
bool isInterruptSignalRTracing = true,
IEnumerable<string>? activitySources = default)
IEnumerable<string>? activitySources = default,
IEnumerable<Assembly>? blazorRouteAssemblies = default)
{
ArgumentNullException.ThrowIfNull(option);

Expand All @@ -69,7 +70,10 @@ public static IServiceCollection AddObservable(this IServiceCollection services,
builder.SetResourceBuilder(resources);
builder.AddOtlpExporter(otlp => otlp.Endpoint = uri!);
});

if (blazorRouteAssemblies != null && blazorRouteAssemblies.Any())
{
BlazorRouteManager.InitRoute(blazorRouteAssemblies.ToArray());
}
return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task InvokeAsync(HttpContext httpContext)
}
else
{
OpenTelemetryInstrumentationOptions.Logger.LogInformation("response length: {Length}, context: {Context}", responseResult.Length, responseResult);
OpenTelemetryInstrumentationOptions.Logger?.LogInformation("response length: {Length}, context: {Context}", responseResult.Length, responseResult);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Masa.Contrib.StackSdks.Tsc;

public class MasaObservableOptions
{
public string ProjectName { get; set; }
public string ProjectName { get; set; } = default!;

public string ServiceName { get; set; }
public string ServiceName { get; set; } = default!;

public string ServiceNameSpace { get; set; }
public string ServiceNameSpace { get; set; } = default!;

public string ServiceInstanceId { get; set; }
public string ServiceInstanceId { get; set; } = default!;

public string ServiceVersion { get; set; }
public string ServiceVersion { get; set; } = default!;

public string Layer { get; set; }
public string Layer { get; set; } = default!;
}
Loading

0 comments on commit 66a042a

Please sign in to comment.