105040 Rename 中间件接口
This commit is contained in:
parent
e36444b9b8
commit
2a91923955
|
@ -1,9 +1,7 @@
|
||||||
using Microsoft.AspNetCore.Http;
|
namespace LFlow.Middleware
|
||||||
|
|
||||||
namespace LFlow.Middleware
|
|
||||||
{
|
{
|
||||||
|
|
||||||
public interface IMiddleware
|
public interface ILFlowMiddleware
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 优先级
|
/// 优先级
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,4 @@
|
||||||
using Microsoft.AspNetCore.Builder;
|
using System.Reflection;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace LFlow.Middleware.Register
|
namespace LFlow.Middleware.Register
|
||||||
{
|
{
|
||||||
|
@ -8,17 +6,26 @@ namespace LFlow.Middleware.Register
|
||||||
{
|
{
|
||||||
private static IServiceProvider? _serviceProvider;
|
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 = [];
|
private static List<Type> _middlewareTypes = [];
|
||||||
public static void RegisterMiddlewares(this IServiceCollection service, List<Assembly> assemblies)
|
public static void RegisterMiddlewares(this IServiceCollection service, List<Assembly> assemblies)
|
||||||
{
|
{
|
||||||
//var assembly = Assembly.getas()!;
|
if (assemblies == null || assemblies.Count == 0)
|
||||||
assemblies.ForEach(assembly =>
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 列入系统内置中间件
|
||||||
|
var allAssemblies = new List<Assembly>(assemblies)
|
||||||
|
{
|
||||||
|
Assembly.GetAssembly(typeof(ILFlowMiddleware))!
|
||||||
|
};
|
||||||
|
|
||||||
|
allAssemblies.ForEach(assembly =>
|
||||||
{
|
{
|
||||||
var types = assembly.GetTypes().ToList();
|
var types = assembly.GetTypes().ToList();
|
||||||
foreach (var type in types)
|
foreach (var type in types)
|
||||||
{
|
{
|
||||||
if (type.GetInterface(nameof(IMiddleware)) != null)
|
if (type.GetInterface(nameof(ILFlowMiddleware)) != null)
|
||||||
{
|
{
|
||||||
service.AddSingleton(type);
|
service.AddSingleton(type);
|
||||||
_middlewareTypes.Add(type);
|
_middlewareTypes.Add(type);
|
||||||
|
@ -32,7 +39,7 @@ namespace LFlow.Middleware.Register
|
||||||
_serviceProvider = application.ApplicationServices;
|
_serviceProvider = application.ApplicationServices;
|
||||||
_middlewareTypes.ForEach(type =>
|
_middlewareTypes.ForEach(type =>
|
||||||
{
|
{
|
||||||
if (_serviceProvider.GetService(type) is IMiddleware middleware)
|
if (_serviceProvider.GetService(type) is ILFlowMiddleware middleware)
|
||||||
_middlewares.Add(type, middleware);
|
_middlewares.Add(type, middleware);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -44,11 +51,15 @@ namespace LFlow.Middleware.Register
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task Handle(IApplicationBuilder application)
|
public static async Task Handle(IApplicationBuilder application)
|
||||||
{
|
{
|
||||||
|
//var logger = application.ApplicationServices.GetService<ILogger>();
|
||||||
var orderedMiddlewares = _middlewares.Values.OrderBy(m => m.Priority).ToList();
|
var orderedMiddlewares = _middlewares.Values.OrderBy(m => m.Priority).ToList();
|
||||||
foreach (var middleware in orderedMiddlewares)
|
foreach (var middleware in orderedMiddlewares)
|
||||||
{
|
{
|
||||||
await Task.FromResult(application.Use(middleware.RunAsync));
|
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
using Serilog;
|
||||||
|
|
||||||
namespace LFlow.UserManagement
|
namespace LFlow.UserManagement
|
||||||
{
|
{
|
||||||
public class UserMiddleware(ILogger logger) : IMiddleware
|
public class UserMiddleware(ILogger logger) : ILFlowMiddleware
|
||||||
{
|
{
|
||||||
public int Priority => 1;
|
public int Priority => 1;
|
||||||
public async Task RunAsync(Microsoft.AspNetCore.Http.HttpContext context, Func<Task> next)
|
public async Task RunAsync(Microsoft.AspNetCore.Http.HttpContext context, Func<Task> next)
|
||||||
{
|
{
|
||||||
// Do something before
|
// Do something before
|
||||||
var path = context.Request.Path;
|
var progController = context.GetRouteData()?.Values["controller"]?.ToString();
|
||||||
logger.Information(path);
|
var progAction = context.GetRouteData()?.Values["action"]?.ToString();
|
||||||
await next();
|
if (progAction != "ListAll")
|
||||||
|
await next();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await context.Response.WriteAsync(JsonConvert.SerializeObject(ApiResult<object>.FailResult("无权限!", 100501)));
|
||||||
|
}
|
||||||
// Do something after
|
// Do something after
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue