Browse Source

简化调用堆栈

master
王厅 5 days ago
parent
commit
4402e4fbc9
  1. 79
      Apewer/Web/ApiProcessor.cs

79
Apewer/Web/ApiProcessor.cs

@ -1,8 +1,6 @@
using Apewer.Network; using Apewer.Network;
using Apewer.Source;
using System; using System;
using System.Collections.Generic; 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;
@ -47,8 +45,32 @@ namespace Apewer.Web
response.Function = request.Function; response.Function = request.Function;
_context.Response = response; _context.Response = response;
// 调用 API。 // OPTIONS
Invoke(); if (_context.Request.Method == HttpMethod.OPTIONS)
{
if (!_context.Options.AllowOptions)
{
_context.Response.Model = new ApiTextModel("");
return;
}
}
// 中间件
var middlewares = _context.Invoker.Middlewares;
if (middlewares.Length > 0)
{
// 设置队列和回调。
_mw_queue = new Queue<Type>(middlewares);
_context.SetMiddlewareCallback(MiddlewareNext);
// 执行。
MiddlewareNext(_context);
}
else
{
// 无中间件。
Route();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -131,23 +153,6 @@ namespace Apewer.Web
return null; return null;
} }
// 寻找入口。
void Invoke()
{
// OPTIONS
if (_context.Request.Method == HttpMethod.OPTIONS)
{
if (!_context.Options.AllowOptions)
{
_context.Response.Model = new ApiTextModel("");
return;
}
}
// 中间件
InvokeMiddwares();
}
#endregion #endregion
#region common #region common
@ -278,24 +283,6 @@ namespace Apewer.Web
middleware.Invoke(context); 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() void Route()
{ {
@ -308,7 +295,7 @@ namespace Apewer.Web
if (action != null) if (action != null)
{ {
_context.ApiAction = action; _context.ApiAction = action;
Invoke(action); InvokeAction(action);
return; return;
} }
} }
@ -318,7 +305,7 @@ namespace Apewer.Web
{ {
var appName = _context.Request.Application; var appName = _context.Request.Application;
var application = _context.Entries.GetApplication(appName); var application = _context.Entries.GetApplication(appName);
Invoke(application); InvokeApplication(application);
return; return;
} }
@ -332,7 +319,7 @@ namespace Apewer.Web
#region route #region route
// 执行 Action。 // 执行 Action。
void Invoke(ApiAction action) void InvokeAction(ApiAction action)
{ {
var controller = null as ApiController; var controller = null as ApiController;
try try
@ -375,7 +362,7 @@ namespace Apewer.Web
#region reflection #region reflection
// 创建控制器。 // 创建控制器。
void Invoke(ApiApplication application) void InvokeApplication(ApiApplication application)
{ {
var options = _context.Options; var options = _context.Options;
var entries = _context.Entries; var entries = _context.Entries;
@ -401,7 +388,7 @@ namespace Apewer.Web
try try
{ {
controller = CreateController(@default, _context); controller = CreateController(@default, _context);
Invoke(controller, application, null, options, request, response); InvokeFunction(controller, application, null, options, request, response);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -421,7 +408,7 @@ namespace Apewer.Web
try try
{ {
controller = CreateController(application.Type, _context); controller = CreateController(application.Type, _context);
Invoke(controller, application, function, options, request, response); InvokeFunction(controller, application, function, options, request, response);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -435,7 +422,7 @@ namespace Apewer.Web
} }
// 调用 Function。 // 调用 Function。
void Invoke(ApiController controller, ApiApplication application, ApiFunction function, ApiOptions options, ApiRequest request, ApiResponse response) void InvokeFunction(ApiController controller, ApiApplication application, ApiFunction function, ApiOptions options, ApiRequest request, ApiResponse response)
{ {
try try
{ {

Loading…
Cancel
Save