From cd1b0a7401bad9c2b5bdb7d47662db33a92b953b Mon Sep 17 00:00:00 2001
From: fengjiayi <12821976+ning_xi@user.noreply.gitee.com>
Date: Fri, 20 Sep 2024 17:30:38 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E8=87=AA=E8=BF=B0?=
=?UTF-8?q?=E6=96=87=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Library.Core/Http/Attribute.cs | 116 ---
Library.Core/Http/ControllerBase.cs | 17 -
Library.Core/Http/Router.cs | 762 -------------------
Library.Core/Http/WebAPIAttribute.cs | 187 -----
Library.Core/NodeFlow/DynamicNodeCoreType.cs | 38 -
Library.Core/NodeFlow/FlowStateType.cs | 25 -
Library.Core/NodeFlow/Tool/Attribute.cs | 35 -
Library.Core/NodeFlow/Tool/DynamicTool.cs | 202 -----
README.md | 20 +-
9 files changed, 12 insertions(+), 1390 deletions(-)
delete mode 100644 Library.Core/Http/Attribute.cs
delete mode 100644 Library.Core/Http/ControllerBase.cs
delete mode 100644 Library.Core/Http/Router.cs
delete mode 100644 Library.Core/Http/WebAPIAttribute.cs
delete mode 100644 Library.Core/NodeFlow/DynamicNodeCoreType.cs
delete mode 100644 Library.Core/NodeFlow/FlowStateType.cs
delete mode 100644 Library.Core/NodeFlow/Tool/Attribute.cs
delete mode 100644 Library.Core/NodeFlow/Tool/DynamicTool.cs
diff --git a/Library.Core/Http/Attribute.cs b/Library.Core/Http/Attribute.cs
deleted file mode 100644
index 670a840..0000000
--- a/Library.Core/Http/Attribute.cs
+++ /dev/null
@@ -1,116 +0,0 @@
-using System;
-
-namespace Serein.Library.Core.Http
-{
- ///
- /// 表示参数为url中的数据(Get请求中不需要显式标注)
- ///
- [AttributeUsage(AttributeTargets.Parameter)]
- public sealed class IsUrlDataAttribute : Attribute
- {
-
- }
-
- ///
- /// 表示入参参数为整个boby的数据
- ///
- /// 例如:User类型含有int id、string name字段
- ///
- /// ① Add(User user)
- /// 请求需要传入的json为
- /// {"user":{
- /// "id":2,
- /// "name":"李志忠"}}
- ///
- /// ② Add([Boby]User user)
- /// 请求需要传入的json为
- /// {"id":2,"name":"李志忠"}
- ///
- ///
- [AttributeUsage(AttributeTargets.Parameter)]
- public sealed class IsBobyDataAttribute : Attribute
- {
-
- }
-
- ///
- /// 表示该控制器会被自动注册(与程序集同一命名空间,暂时不支持运行时自动加载DLL,需要手动注册)
- ///
- [AttributeUsage(AttributeTargets.Class)]
- public sealed class AutoHostingAttribute(string url = "") : Attribute
- {
- public string Url { get; } = url;
- }
- ///
- /// 表示该属性为自动注入依赖项
- ///
- [AttributeUsage(AttributeTargets.Property)]
- public sealed class AutoInjectionAttribute : Attribute
- {
- }
-
-
- ///
- /// 方法的接口类型与附加URL
- ///
- ///
- /// 假设UserController.Add()的WebAPI特性中
- /// http是HTTP.POST
- /// url被显示标明“temp”
- /// 那么请求的接口是POST,URL是
- /// [http://localhost:8080]/user/add/temp
- ///
- ///
- ///
- [AttributeUsage(AttributeTargets.Method)]
-
- public sealed class WebApiAttribute() : Attribute
-
- {
- public API Type ;
- public string Url ;
- ///
- /// 方法名称不作为url的部分
- ///
- public bool IsUrl;
- }
- [AttributeUsage(AttributeTargets.Method)]
-
- public sealed class ApiPostAttribute() : Attribute
-
- {
- public string Url;
- ///
- /// 方法名称不作为url的部分
- ///
- public bool IsUrl = true;
- }
- [AttributeUsage(AttributeTargets.Method)]
-
- public sealed class ApiGetAttribute() : Attribute
-
- {
- public string Url;
- ///
- /// 方法名称不作为url的部分
- ///
- public bool IsUrl = true;
- }
-
- /*public sealed class WebApiAttribute(API http, bool isUrl = true, string url = "") : Attribute
- {
- public API Http { get; } = http;
- public string Url { get; } = url;
- ///
- /// 方法名称不作为url的部分
- ///
- public bool IsUrl { get; } = isUrl;
- }*/
- public enum API
- {
- POST,
- GET,
- //PUT,
- //DELETE
- }
-}
diff --git a/Library.Core/Http/ControllerBase.cs b/Library.Core/Http/ControllerBase.cs
deleted file mode 100644
index 714c1a0..0000000
--- a/Library.Core/Http/ControllerBase.cs
+++ /dev/null
@@ -1,17 +0,0 @@
-namespace Serein.Library.Core.Http
-{
- public class ControllerBase
- {
-
- public string Url { get; set; }
-
- public string BobyData { get; set; }
-
- public string GetLog(Exception ex)
- {
- return "Url : " + Url + Environment.NewLine +
- "Ex : " + ex.Message + Environment.NewLine +
- "Data : " + BobyData + Environment.NewLine;
- }
- }
-}
diff --git a/Library.Core/Http/Router.cs b/Library.Core/Http/Router.cs
deleted file mode 100644
index 1272568..0000000
--- a/Library.Core/Http/Router.cs
+++ /dev/null
@@ -1,762 +0,0 @@
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
-using Serein.Library.Api.Api;
-using Serein.Library.Core.IOC;
-using Serein.Tool;
-using System.Collections;
-using System.Collections.Concurrent;
-using System.Net;
-using System.Reflection;
-using System.Text;
-using System.Web;
-using Enum = System.Enum;
-using Type = System.Type;
-
-namespace Serein.Library.Core.Http
-{
- /*
- Router类负责解析请求的url,url参数,boby参数
- 根据url
-
- web server 监听类,监听外部的请求
- router 选择对应的控制器
- agent 负责传入对应的参数,注入依赖
-
-
-
- */
-
-
- ///
- /// 路由注册与解析
- ///
- public class Router
- {
- private readonly ConcurrentDictionary _controllerAutoHosting; // 存储是否实例化
- private readonly ConcurrentDictionary _controllerTypes; // 存储控制器类型
- private readonly ConcurrentDictionary _controllerInstances; // 存储控制器实例对象
- private readonly ConcurrentDictionary> _routes; // 用于存储路由信息
-
- private readonly ISereinIoc serviceRegistry; // 用于存储路由信息
-
- //private Type PostRequest;
-
- public Router(ISereinIoc serviceRegistry) // 构造函数,初始化 Router 类的新实例
- {
- this.serviceRegistry = serviceRegistry;
-
- _routes = new ConcurrentDictionary>(); // 初始化路由字典
-
- _controllerAutoHosting = new ConcurrentDictionary(); // 初始化控制器实例对象字典
- _controllerTypes = new ConcurrentDictionary(); // 初始化控制器实例对象字典
- _controllerInstances = new ConcurrentDictionary(); // 初始化控制器实例对象字典
-
- foreach (API method in Enum.GetValues(typeof(API))) // 遍历 HTTP 枚举类型的所有值
- {
- _routes.TryAdd(method.ToString(), new ConcurrentDictionary()); // 初始化每种 HTTP 方法对应的路由字典
- }
-
- // 获取当前程序集
- Assembly assembly = Assembly.GetExecutingAssembly();
-
- // 获取包含“Controller”名称的类型
- var controllerTypes = assembly.GetTypes()
- .Where(t => t.Name.Contains("Controller"));
-
- Type baseAttribute = typeof(AutoHostingAttribute);
- Type baseController = typeof(ControllerBase);
- foreach (var controllerType in controllerTypes)
- {
- if (controllerType.IsSubclassOf(baseController) && controllerType.IsDefined(baseAttribute))
- {
-
- // 如果属于控制器,并标记了AutoHosting特性,进行自动注册
- AutoRegisterAutoController(controllerType);
- }
- else
- {
- continue;
- }
- }
- }
-
-
- ///
- /// 自动注册 自动实例化控制器 类型
- ///
- ///
- public void AutoRegisterAutoController(Type controllerType) // 方法声明,用于注册并实例化控制器类型
- {
- if (!controllerType.IsClass || controllerType.IsAbstract) return; // 如果不是类或者是抽象类,则直接返回
-
- var autoHostingAttribute = controllerType.GetCustomAttribute();
- if (autoHostingAttribute != null) {
- foreach (var method in controllerType.GetMethods()) // 遍历控制器类型的所有方法
- {
- var apiGetAttribute = method.GetCustomAttribute();
- var apiPostAttribute = method.GetCustomAttribute();
- if( apiGetAttribute == null && apiPostAttribute == null )
- {
- continue;
- }
-
-
-
- WebApiAttribute webApiAttribute = new WebApiAttribute()
- {
- Type = apiGetAttribute != null ? API.GET : API.POST,
- Url = apiGetAttribute != null ? apiGetAttribute.Url : apiPostAttribute.Url,
- IsUrl = apiGetAttribute != null ? apiGetAttribute.IsUrl : apiPostAttribute.IsUrl,
- };
-
-
-
- if (apiPostAttribute != null) // 如果存在 WebAPIAttribute 属性
- {
- var url = AddRoutesUrl(autoHostingAttribute,
- webApiAttribute,
- controllerType, method);
- Console.WriteLine(url);
- if (url == null) continue;
- _controllerAutoHosting[url] = true;
- _controllerTypes[url] = controllerType;
-
- _controllerInstances[url] = null;
-
- }
-
-
- /* var routeAttribute = method.GetCustomAttribute(); // 获取方法上的 WebAPIAttribute 自定义属性
- if (routeAttribute != null) // 如果存在 WebAPIAttribute 属性
- {
- var url = AddRoutesUrl(autoHostingAttribute, routeAttribute, controllerType, method);
- Console.WriteLine(url);
- if (url == null) continue;
- _controllerAutoHosting[url] = true;
- _controllerTypes[url] = controllerType;
- _controllerInstances[url] = null;
- }*/
- }
- }
- }
- ///
- /// 手动注册 自动实例化控制器实例
- ///
- public void RegisterAutoController() // 方法声明,用于动态注册路由
- {
- Type controllerType = typeof(T); // 获取控制器实例的类型
- foreach (var method in controllerType.GetMethods()) // 遍历控制器类型的所有方法
- {
- var apiGetAttribute = method.GetCustomAttribute();
- var apiPostAttribute = method.GetCustomAttribute();
- if (apiGetAttribute == null && apiPostAttribute == null)
- {
- continue;
- }
-
-
-
- WebApiAttribute webApiAttribute = new WebApiAttribute()
- {
- Type = apiGetAttribute != null ? API.GET : API.POST,
- Url = apiGetAttribute != null ? apiGetAttribute.Url : apiPostAttribute.Url,
- IsUrl = apiGetAttribute != null ? apiGetAttribute.IsUrl : apiPostAttribute.IsUrl,
- };
-
-
-
- var url = AddRoutesUrl(null, webApiAttribute, controllerType, method);
-
- if (url == null) continue;
- _controllerAutoHosting[url] = true;
- _controllerTypes[url] = controllerType;
-
- _controllerInstances[url] = null;
-
- }
- }
-
-
- ///
- /// 手动注册 实例持久控制器实例
- ///
- ///
- public void RegisterController(TController controllerInstance) where TController : ControllerBase // 方法声明,用于动态注册路由
- {
- if(controllerInstance == null) return;
- Type controllerType = controllerInstance.GetType(); // 获取控制器实例的类型
- foreach (var method in controllerType.GetMethods()) // 遍历控制器类型的所有方法
- {
- var apiGetAttribute = method.GetCustomAttribute();
- var apiPostAttribute = method.GetCustomAttribute();
- if (apiGetAttribute == null && apiPostAttribute == null)
- {
- continue;
- }
-
-
-
- WebApiAttribute webApiAttribute = new WebApiAttribute()
- {
- Type = apiGetAttribute != null ? API.GET : API.POST,
- Url = apiGetAttribute != null ? apiGetAttribute.Url : apiPostAttribute.Url,
- IsUrl = apiGetAttribute != null ? apiGetAttribute.IsUrl : apiPostAttribute.IsUrl,
- };
-
-
-
- var url = AddRoutesUrl(null, webApiAttribute, controllerType, method);
-
- if (url == null) continue;
- _controllerInstances[url] = controllerInstance;
- _controllerAutoHosting[url] = false;
- }
- }
-
- ///
- /// 从方法中收集路由信息
- ///
- ///
- public string AddRoutesUrl(AutoHostingAttribute autoHostingAttribute, WebApiAttribute webAttribute, Type controllerType, MethodInfo method)
- {
- string controllerName;
- if (autoHostingAttribute == null || string.IsNullOrWhiteSpace(autoHostingAttribute.Url))
- {
- controllerName = controllerType.Name.Replace("Controller", "").ToLower(); // 获取控制器名称并转换为小写
- }
- else
- {
- controllerName = autoHostingAttribute.Url;
- }
-
- var httpMethod = webAttribute.Type; // 获取 HTTP 方法
- var customUrl = webAttribute.Url; // 获取自定义 URL
-
- string url;
-
- if (webAttribute.IsUrl)
- {
-
- if (string.IsNullOrEmpty(customUrl)) // 如果自定义 URL 为空
- {
- url = $"/{controllerName}/{method.Name}".ToLower(); // 构建默认 URL
- }
- else
- {
- customUrl = CleanUrl(customUrl);
- url = $"/{controllerName}/{method.Name}/{customUrl}".ToLower();// 清理自定义 URL,并构建新的 URL
- }
- _routes[httpMethod.ToString()].TryAdd(url, method); // 将 URL 和方法添加到对应的路由字典中
- }
- else
- {
- if (string.IsNullOrEmpty(customUrl)) // 如果自定义 URL 为空
- {
- url = $"/{controllerName}".ToLower(); // 构建默认 URL
- }
- else
- {
- customUrl = CleanUrl(customUrl);
- url = $"/{controllerName}/{customUrl}".ToLower();// 清理自定义 URL,并构建新的 URL
- }
- _routes[httpMethod.ToString()].TryAdd(url, method); // 将 URL 和方法添加到对应的路由字典中
- }
-
- return url;
-
- }
-
-
- ///
- /// 收集路由信息
- ///
- ///
- public void CollectRoutes(Type controllerType)
- {
- string controllerName = controllerType.Name.Replace("Controller", "").ToLower(); // 获取控制器名称并转换为小写
- foreach (var method in controllerType.GetMethods()) // 遍历控制器类型的所有方法
- {
- var routeAttribute = method.GetCustomAttribute(); // 获取方法上的 WebAPIAttribute 自定义属性
- if (routeAttribute != null) // 如果存在 WebAPIAttribute 属性
- {
- var customUrl = routeAttribute.Url; // 获取自定义 URL
- string url;
- if (string.IsNullOrEmpty(customUrl)) // 如果自定义 URL 为空
- {
- url = $"/api/{controllerName}/{method.Name}".ToLower(); // 构建默认 URL
- }
- else
- {
- customUrl = CleanUrl(customUrl);
- url = $"/api/{controllerName}/{method.Name}/{customUrl}".ToLower();// 清理自定义 URL,并构建新的 URL
- }
- var httpMethod = routeAttribute.Type; // 获取 HTTP 方法
- _routes[httpMethod.ToString()].TryAdd(url, method); // 将 URL 和方法添加到对应的路由字典中
- }
- }
- }
-
-
- ///
- /// 解析路由,调用对应的方法
- ///
- ///
- ///
- public async Task RouteAsync(HttpListenerContext context)
- {
- var request = context.Request; // 获取请求对象
- var response = context.Response; // 获取响应对象
- var url = request.Url; // 获取请求的 URL
- var httpMethod = request.HttpMethod; // 获取请求的 HTTP 方法
-
- var template = request.Url.AbsolutePath.ToLower();
-
-
- if (!_routes[httpMethod].TryGetValue(template, out MethodInfo method))
- {
- return false;
- }
-
-
-
- var routeValues = GetUrlData(url); // 解析 URL 获取路由参数
-
- ControllerBase controllerInstance;
- if (!_controllerAutoHosting[template])
- {
- controllerInstance = (ControllerBase)_controllerInstances[template];
- }
- else
- {
-
- controllerInstance = (ControllerBase)serviceRegistry.Instantiate(_controllerTypes[template]);// 使用反射创建控制器实例
-
-
- }
-
- if (controllerInstance == null)
- {
- return false; // 未找到控制器实例
- }
-
- controllerInstance.Url = url.AbsolutePath;
- object result;
- switch (httpMethod) // 根据请求的 HTTP 方法执行不同的操作
- {
- case "GET": // 如果是 GET 请求,传入方法、控制器、url参数
- result = InvokeControllerMethodWithRouteValues(method, controllerInstance, routeValues);
- break;
- case "POST": // POST 请求传入方法、控制器、请求体内容,url参数
- var requestBody = await ReadRequestBodyAsync(request); // 读取请求体内容
- controllerInstance.BobyData = requestBody;
- var requestJObject = requestBody.FromJSON