105040 Rename 中间件接口

This commit is contained in:
lihanbo 2024-11-01 16:45:39 +08:00
parent e36444b9b8
commit 2a91923955
4 changed files with 63 additions and 17 deletions

View File

@ -1,9 +1,7 @@
using Microsoft.AspNetCore.Http;
namespace LFlow.Middleware
namespace LFlow.Middleware
{
public interface IMiddleware
public interface ILFlowMiddleware
{
/// <summary>
/// 优先级

View File

@ -0,0 +1,28 @@
namespace LFlow.Middleware.Middlewares
{
public class LoggingMiddleware : ILFlowMiddleware
{
private readonly ILogger<LoggingMiddleware> _logger;
public LoggingMiddleware(ILogger<LoggingMiddleware> logger)
{
_logger = logger;
}
public int Priority => 1;
public async Task RunAsync(HttpContext context, Func<Task> next)
{
var controllerName = context.GetRouteData()?.Values["controller"]?.ToString();
var actionName = context.GetRouteData()?.Values["action"]?.ToString();
using (_logger?.BeginScope("LoggingMiddleware"))
{
//_logger?.LogInformation($"Controller: {controllerName} / Action : {actionName}");
//_logger?.LogInformation("Router Data {@routerData}", context.GetRouteData());
await next();
}
}
}
}

View File

@ -1,6 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using System.Reflection;
namespace LFlow.Middleware.Register
{
@ -8,17 +6,26 @@ namespace LFlow.Middleware.Register
{
private static IServiceProvider? _serviceProvider;
private static readonly IDictionary<Type, IMiddleware> _middlewares = new Dictionary<Type, IMiddleware>();
private static readonly IDictionary<Type, ILFlowMiddleware> _middlewares = new Dictionary<Type, ILFlowMiddleware>();
private static List<Type> _middlewareTypes = [];
public static void RegisterMiddlewares(this IServiceCollection service, List<Assembly> assemblies)
{
//var assembly = Assembly.getas()!;
assemblies.ForEach(assembly =>
if (assemblies == null || assemblies.Count == 0)
{
return;
}
// 列入系统内置中间件
var allAssemblies = new List<Assembly>(assemblies)
{
Assembly.GetAssembly(typeof(ILFlowMiddleware))!
};
allAssemblies.ForEach(assembly =>
{
var types = assembly.GetTypes().ToList();
foreach (var type in types)
{
if (type.GetInterface(nameof(IMiddleware)) != null)
if (type.GetInterface(nameof(ILFlowMiddleware)) != null)
{
service.AddSingleton(type);
_middlewareTypes.Add(type);
@ -32,7 +39,7 @@ namespace LFlow.Middleware.Register
_serviceProvider = application.ApplicationServices;
_middlewareTypes.ForEach(type =>
{
if (_serviceProvider.GetService(type) is IMiddleware middleware)
if (_serviceProvider.GetService(type) is ILFlowMiddleware middleware)
_middlewares.Add(type, middleware);
});
}
@ -44,11 +51,15 @@ namespace LFlow.Middleware.Register
/// <returns></returns>
public static async Task Handle(IApplicationBuilder application)
{
//var logger = application.ApplicationServices.GetService<ILogger>();
var orderedMiddlewares = _middlewares.Values.OrderBy(m => m.Priority).ToList();
foreach (var middleware in orderedMiddlewares)
{
await Task.FromResult(application.Use(middleware.RunAsync));
//logger?.LogInformation("Middleware {MiddlewareName} is registered.", middleware.GetType().Name);
Console.WriteLine($"[{DateTime.Now:hh:mm:ss} INF] Middleware {middleware.GetType().Name} is registered.");
}
}
}
}

View File

@ -1,17 +1,26 @@
using LFlow.Middleware;
using LFlow.Base.Utils;
using LFlow.Middleware;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
using Serilog;
namespace LFlow.UserManagement
{
public class UserMiddleware(ILogger logger) : IMiddleware
public class UserMiddleware(ILogger logger) : ILFlowMiddleware
{
public int Priority => 1;
public async Task RunAsync(Microsoft.AspNetCore.Http.HttpContext context, Func<Task> next)
{
// Do something before
var path = context.Request.Path;
logger.Information(path);
await next();
var progController = context.GetRouteData()?.Values["controller"]?.ToString();
var progAction = context.GetRouteData()?.Values["action"]?.ToString();
if (progAction != "ListAll")
await next();
else
{
await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501)));
}
// Do something after
}
}