|
|
@ -1,6 +1,8 @@ |
|
|
using Apewer.Network; |
|
|
using Apewer.Network; |
|
|
using Apewer.Source; |
|
|
using Apewer.Source; |
|
|
using System; |
|
|
using System; |
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
using System.Data; |
|
|
using System.Net; |
|
|
using System.Net; |
|
|
using System.Reflection; |
|
|
using System.Reflection; |
|
|
using static Apewer.Web.ApiUtility; |
|
|
using static Apewer.Web.ApiUtility; |
|
|
@ -50,6 +52,7 @@ namespace Apewer.Web |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
|
|
|
ApiUtility.Exception(_context.Response, ex, _context.Options.WithException); |
|
|
var message = ex.Message(); |
|
|
var message = ex.Message(); |
|
|
Logger.Internals.Error(typeof(ApiInvoker), message); |
|
|
Logger.Internals.Error(typeof(ApiInvoker), message); |
|
|
} |
|
|
} |
|
|
@ -141,32 +144,8 @@ namespace Apewer.Web |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 路由
|
|
|
// 中间件
|
|
|
if (_context.Options.UseRoute) |
|
|
InvokeMiddwares(); |
|
|
{ |
|
|
|
|
|
var path = _context?.Request?.Url?.AbsolutePath; |
|
|
|
|
|
path = path.TrimEnd('/'); |
|
|
|
|
|
var action = _context.Entries.GetAction(path); |
|
|
|
|
|
if (action != null) |
|
|
|
|
|
{ |
|
|
|
|
|
_context.ApiAction = action; |
|
|
|
|
|
Invoke(action); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 反射
|
|
|
|
|
|
if (_context.Options.UseReflection) |
|
|
|
|
|
{ |
|
|
|
|
|
var appName = _context.Request.Application; |
|
|
|
|
|
var application = _context.Entries.GetApplication(appName); |
|
|
|
|
|
Invoke(application); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 未匹配到
|
|
|
|
|
|
_context.Response.Duration = Duration(_context.Beginning); |
|
|
|
|
|
_context.Response.Model = new ApiStatusModel(404); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endregion
|
|
|
#endregion
|
|
|
@ -277,6 +256,79 @@ namespace Apewer.Web |
|
|
|
|
|
|
|
|
#endregion
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region middleware
|
|
|
|
|
|
|
|
|
|
|
|
Queue<Type> _mw_queue = null; |
|
|
|
|
|
|
|
|
|
|
|
void MiddlewareNext(ApiContext context) |
|
|
|
|
|
{ |
|
|
|
|
|
if (_mw_queue.Count < 1) |
|
|
|
|
|
{ |
|
|
|
|
|
Route(); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 创建下一个中间件的实例
|
|
|
|
|
|
var type = _mw_queue.Dequeue(); |
|
|
|
|
|
var instance = Activator.CreateInstance(type); |
|
|
|
|
|
var middleware = instance as IApiMiddleware; |
|
|
|
|
|
if (middleware == null) throw new Exception($"类型【{type.FullName}】不是有效的中间件。"); |
|
|
|
|
|
|
|
|
|
|
|
// 调用
|
|
|
|
|
|
middleware.Invoke(context); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 调用中间件。
|
|
|
|
|
|
void InvokeMiddwares() |
|
|
|
|
|
{ |
|
|
|
|
|
var types = _context.Invoker.Middlewares; |
|
|
|
|
|
if (types.Length < 1) |
|
|
|
|
|
{ |
|
|
|
|
|
Route(); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 设置队列和回调。
|
|
|
|
|
|
_mw_queue = new Queue<Type>(types); |
|
|
|
|
|
_context.SetMiddlewareCallback(MiddlewareNext); |
|
|
|
|
|
|
|
|
|
|
|
// 执行。
|
|
|
|
|
|
MiddlewareNext(_context); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 执行路由。
|
|
|
|
|
|
void Route() |
|
|
|
|
|
{ |
|
|
|
|
|
// 路由
|
|
|
|
|
|
if (_context.Options.UseRoute) |
|
|
|
|
|
{ |
|
|
|
|
|
var path = _context?.Request?.Url?.AbsolutePath; |
|
|
|
|
|
path = path.TrimEnd('/'); |
|
|
|
|
|
var action = _context.Entries.GetAction(path); |
|
|
|
|
|
if (action != null) |
|
|
|
|
|
{ |
|
|
|
|
|
_context.ApiAction = action; |
|
|
|
|
|
Invoke(action); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 反射
|
|
|
|
|
|
if (_context.Options.UseReflection) |
|
|
|
|
|
{ |
|
|
|
|
|
var appName = _context.Request.Application; |
|
|
|
|
|
var application = _context.Entries.GetApplication(appName); |
|
|
|
|
|
Invoke(application); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 未匹配到
|
|
|
|
|
|
_context.Response.Duration = Duration(_context.Beginning); |
|
|
|
|
|
_context.Response.Model = new ApiStatusModel(404); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region route
|
|
|
#region route
|
|
|
|
|
|
|
|
|
// 执行 Action。
|
|
|
// 执行 Action。
|
|
|
|