聚合 API 网关
This commit is contained in:
parent
f13e775ca1
commit
b7caaa2d0c
@ -0,0 +1,18 @@
|
||||
using HelloShop.ApiService.Services;
|
||||
using HelloShop.ServiceDefaults.Permissions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HelloShop.ApiService.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PermissionsController(IPermissionService permissionService) : ControllerBase
|
||||
{
|
||||
[HttpGet("Definitions")]
|
||||
public async Task<ActionResult<IReadOnlyList<PermissionGroupDefinitionResponse>>> GetAllPermissionDefinitions()
|
||||
{
|
||||
IReadOnlyList<PermissionGroupDefinitionResponse> permissionGroupDefinitions = await permissionService.GetAllPermissionGorupDefinitionsAsync(HttpContext.RequestAborted);
|
||||
|
||||
return permissionGroupDefinitions.Any() ? Ok(permissionGroupDefinitions) : NoContent();
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ using HelloShop.ServiceDefaults.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using HelloShop.ApiService.Extensions;
|
||||
using HelloShop.ApiService.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -22,6 +23,7 @@ builder.Services.AddReverseProxy()
|
||||
builder.Services.AddSingleton<IConfiguredServiceEndPointResolver, ConfiguredServiceEndPointResolver>();
|
||||
builder.Services.AddSingleton<IReverseProxyConfigProvider, CustomReverseProxyConfigProvider>();
|
||||
builder.Services.AddTransient<IConfigureOptions<SwaggerUIOptions>, OpenApiConfigureOptions>();
|
||||
builder.Services.AddTransient<IPermissionService, PermissionService>();
|
||||
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
|
8
src/HelloShop.ApiService/Services/IPermissionService.cs
Normal file
8
src/HelloShop.ApiService/Services/IPermissionService.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using HelloShop.ServiceDefaults.Permissions;
|
||||
|
||||
namespace HelloShop.ApiService.Services;
|
||||
|
||||
public interface IPermissionService
|
||||
{
|
||||
Task<IReadOnlyList<PermissionGroupDefinitionResponse>> GetAllPermissionGorupDefinitionsAsync(CancellationToken cancellationToken = default);
|
||||
}
|
35
src/HelloShop.ApiService/Services/PermissionService.cs
Normal file
35
src/HelloShop.ApiService/Services/PermissionService.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using HelloShop.ApiService.Infrastructure;
|
||||
using HelloShop.ServiceDefaults.Permissions;
|
||||
|
||||
namespace HelloShop.ApiService.Services;
|
||||
|
||||
public class PermissionService(HttpClient httpClient, IConfiguredServiceEndPointResolver serviceEndPointResolver) : IPermissionService
|
||||
{
|
||||
public async Task<IReadOnlyList<PermissionGroupDefinitionResponse>> GetAllPermissionGorupDefinitionsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
List<PermissionGroupDefinitionResponse> result = [];
|
||||
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
IReadOnlyCollection<ConfiguredServiceEndPoint> serviceEndPoints = await serviceEndPointResolver.GetConfiguredServiceEndpointsAsync(cancellationToken);
|
||||
|
||||
await Parallel.ForEachAsync(serviceEndPoints, new ParallelOptions{ CancellationToken= cancellationToken}, async (serviceEndPoint, cancelToken) =>
|
||||
{
|
||||
UriBuilder uriBuilder = new(serviceEndPoint.ServiceName) { Path = "api/Permissions/PermissionDefinitions" };
|
||||
|
||||
HttpResponseMessage response = await httpClient.GetAsync(uriBuilder.Uri, cancelToken);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
List<PermissionGroupDefinitionResponse>? permissionGroupDefinition = await response.Content.ReadFromJsonAsync<List<PermissionGroupDefinitionResponse>>(cancelToken);
|
||||
|
||||
if (permissionGroupDefinition != null)
|
||||
{
|
||||
result.AddRange(permissionGroupDefinition);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return result.DistinctBy(x => x.GroupName).ToList();
|
||||
}
|
||||
}
|
@ -38,7 +38,7 @@ public static class PermissionExtensions
|
||||
{
|
||||
PermissionGroupDefinitionResponse permissionGroupDefinition = new()
|
||||
{
|
||||
Name = permissionGroup.Name,
|
||||
GroupName = permissionGroup.Name,
|
||||
DisplayName = permissionGroup.DisplayName,
|
||||
Permissions = []
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
public class PermissionGroupDefinitionResponse
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
public required string GroupName { get; init; }
|
||||
|
||||
public string? DisplayName { get; set; }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user