Skip to content

Commit

Permalink
Merge branch 'swoolefy-5.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Feb 6, 2024
2 parents 63489e3 + 3873a3d commit adaee2b
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Test/Config/dc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// sql执行日志条目设置,不能设置太大,适合调试使用,设置为0,则不使用
'spend_log_limit' => 30,
// 是否开启dubug
'debug' => SystemEnv::isPrdEnv() ? 0 : 1
'debug' => 0
],

'predis' => [
Expand Down
22 changes: 22 additions & 0 deletions Test/Middleware/Route/RateLimiterMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Test\Middleware\Route;

use Swoolefy\Http\RequestInput;
use Swoolefy\Http\ResponseOutput;
use Swoolefy\Core\RouteMiddleware;
use Test\Factory;

class RateLimiterMiddleware implements RouteMiddleware
{
public function handle(RequestInput $requestInput, ResponseOutput $responseOutput)
{
$uri = $requestInput->getRequestUri();
$rateLimit = Factory::getRateLimit();
$rateLimit->setRateKey($uri);
// 每10s内滑动窗口限制2次请求
$rateLimit->setLimitParams(30, 60, 120);
if ($rateLimit->isLimit()) {
throw new \Exception("请求过快,请稍后重试!");
}
}
}
32 changes: 32 additions & 0 deletions Test/Module/Order/Controller/UserOrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ public function userList()
$count = $orderList->total();
$list = $orderList->find();

goApp(function () {
// 列表方式查询
$orderList = new OrderList();
$orderList->setUserId([101,102]);
$count = $orderList->total();
$list = $orderList->find();
});

$this->returnJson([
'total' => $count,
'list' => $list
]);
}

public function userList1()
{
// $db = Factory::getDb();
// $query = $db->newQuery()->table('tbl_users')->where('user_id','>', '100')->limit(0,10);
// $count = $query->count();
// if ($count > 0) {
// $list = $query->select()->toArray();
// }
//
// var_dump($this->request->get);

// 列表方式查询
$orderList = new OrderList();
$orderList->setUserId([101,102]);
$count = $orderList->total();
$list = $orderList->find();


$this->returnJson([
'total' => $count,
'list' => $list
Expand Down
10 changes: 3 additions & 7 deletions Test/Router/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Swoolefy\Http\RequestInput;
use Swoolefy\Http\Route;
use Test\Middleware\Group\GroupTestMiddleware;
use Test\Middleware\Route\RateLimiterMiddleware;

/**
* Module/Controller 下的控制器路由
Expand All @@ -25,12 +26,7 @@
]);


Route::get('/user-order/userList', [
// 针对该接口启动sql-debug
'beforeHandle' => function(RequestInput $requestInput) {
Context::set('db_debug', true);
var_dump($requestInput->getHttpRoutePrefix());
},
Route::any('/user-order/userList', [
'beforeHandle1' => function(RequestInput $requestInput) {
$requestInput->input('name');
$requestInput->input('order_ids');
Expand All @@ -41,7 +37,7 @@
],
'dispatch_route' => [\Test\Module\Order\Controller\UserOrderController::class, 'userList'],
//GroupTestMiddleware::class => GroupTestMiddleware::class
]);
])->enableDbDebug(true)->withRateLimiterMiddleware(RateLimiterMiddleware::class);

Route::post('/user-order/userList', [
// 针对该接口启动sql-debug
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
defined('MQTT_PROTOCOL_LEVEL3') or define('MQTT_PROTOCOL_LEVEL3', 4);
defined('MQTT_PROTOCOL_LEVEL5') or define('MQTT_PROTOCOL_LEVEL5', 5);

defined('SWOOLEFY_VERSION') or define('SWOOLEFY_VERSION', '5.0.20');
defined('SWOOLEFY_VERSION') or define('SWOOLEFY_VERSION', '5.0.21');
defined('SWOOLEFY_EOF_FLAG') or define('SWOOLEFY_EOF_FLAG', '::');

defined('WORKER_CLI_STOP') or define('WORKER_CLI_STOP', 'stop');
Expand Down
14 changes: 12 additions & 2 deletions src/Core/EventCtrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function registerSqlLogger()
}else if (SystemEnv::isScriptService()) {
$sqlLogName = 'sql_script.log';
}else {
$sqlLogName = 'sql.log';
$sqlLogName = 'sql_cli.log';
}
$sqlFilePath = $baseSqlPath.DIRECTORY_SEPARATOR.$sqlLogName;
$logger->setLogFilePath($sqlFilePath);
Expand All @@ -140,7 +140,17 @@ protected function registerGuzzleCurlLogger()
if (!is_dir($baseSqlPath)) {
mkdir($baseSqlPath,0777);
}
$sqlFilePath = $baseSqlPath.DIRECTORY_SEPARATOR.'guzzle_curl.log';

if (SystemEnv::isDaemonService()) {
$sqlFilePath = $baseSqlPath.DIRECTORY_SEPARATOR.'curl_daemon.log';
}else if (SystemEnv::isCronService()) {
$sqlFilePath = $baseSqlPath.DIRECTORY_SEPARATOR.'curl_cron.log';
}else if (SystemEnv::isScriptService()) {
$sqlFilePath = $baseSqlPath.DIRECTORY_SEPARATOR.'curl_script.log';
}else {
$sqlFilePath = $baseSqlPath.DIRECTORY_SEPARATOR.'curl_cli.log';
}

$logger->setLogFilePath($sqlFilePath);
return $logger;
}, LogManager::GUZZLE_CURL_LOG);
Expand Down
26 changes: 24 additions & 2 deletions src/Http/HttpRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Swoolefy\Core\AppDispatch;
use Swoolefy\Core\Application;
use Swoolefy\Core\Controller\BController;
use Swoolefy\Core\Coroutine\Context;
use Swoolefy\Core\RouteMiddleware;
use Swoolefy\Exception\DispatchException;
use Swoolefy\Exception\SystemException;
Expand Down Expand Up @@ -90,6 +91,11 @@ class HttpRoute extends AppDispatch
*/
protected $groupMeta;

/**
* @var RouteOption
*/
protected $routeOption;

/**
* @var array
*/
Expand All @@ -115,7 +121,7 @@ public function __construct(RequestInput $requestInput, ResponseOutput $response
$this->responseOutput = $responseOutput;
$this->extendData = $extendData;
$this->httpMethod = $this->requestInput->getMethod();
list($this->groupMiddleware, $this->beforeMiddleware, $this->dispatchRoute, $this->afterMiddleware, $this->groupMeta) = self::getHttpRouterMapUri($this->requestInput->getServerParams('PATH_INFO'), $this->httpMethod);
list($this->groupMiddleware, $this->beforeMiddleware, $this->dispatchRoute, $this->afterMiddleware, $this->groupMeta, $this->routeOption) = self::getHttpRouterMapUri($this->requestInput->getRequestUri(), $this->httpMethod);
$this->requestInput->setHttpGroupMeta($this->groupMeta);
}

Expand Down Expand Up @@ -195,6 +201,21 @@ protected function invoke(
// reset app conf
$this->app->setAppConf($this->appConf);

// 是否动态开启db-debug
if ($this->routeOption->isEnableDbDebug()) {
Context::set('db_debug', true);
}

// rateLimiterMiddleware handle
if ($rateLimiterMiddleware = $this->routeOption->getRateLimiterMiddleware()) {
if (class_exists($rateLimiterMiddleware)) {
$rateLimiter = new $rateLimiterMiddleware();
if ($rateLimiter instanceof RouteMiddleware) {
$rateLimiter->handle($this->requestInput, $this->responseOutput);
}
}
}

// handle route group middles
$this->handleGroupRouteMiddles();

Expand Down Expand Up @@ -399,6 +420,7 @@ protected static function getHttpRouterMapUri(string $uri, string $method): arra
$groupMeta = $routerMapInfo['group_meta'] ?? [];
$routerMeta = $routerMapInfo['route_meta'];
$groupMiddleware = $routerMapInfo['group_meta']['middleware'] ?? [];
$routeOption = $routerMapInfo['route_option'];
if(!isset($routerMeta['dispatch_route'])) {
$routerMeta['dispatch_route'] = $uri;
}else {
Expand Down Expand Up @@ -439,7 +461,7 @@ protected static function getHttpRouterMapUri(string $uri, string $method): arra
}
}

$routeCacheItems = [$groupMiddleware, $beforeMiddleware, $originDispatchRoute, $afterMiddleware, $groupMeta];
$routeCacheItems = [$groupMiddleware, $beforeMiddleware, $originDispatchRoute, $afterMiddleware, $groupMeta, $routeOption];
self::$routeCache[$uri][$method] = $routeCacheItems;
unset($routerMap[$uri][$method]);
return $routeCacheItems;
Expand Down
58 changes: 40 additions & 18 deletions src/Http/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,21 @@ public static function get(string $uri, array $routeMeta)
$groupMeta = Context::get('__current_request_group_meta') ?? [];
}

$routeOption = new RouteOption();
$uri = self::parseUri($uri, $groupMeta);
return self::$routeMap[$uri]['GET'] = [
self::$routeMap[$uri]['GET'] = [
'group_meta' => $groupMeta,
'method' => ['GET'],
'route_meta' => $routeMeta
'route_meta' => $routeMeta,
'route_option' => &$routeOption
];
return $routeOption;
}

/**
* @param string $uri
* @param array $routeMeta
* @return array
* @return RouteOption
*/
public static function post(string $uri, array $routeMeta)
{
Expand All @@ -74,18 +77,22 @@ public static function post(string $uri, array $routeMeta)
$groupMeta = Context::get('__current_request_group_meta') ?? [];
}

$routeOption = new RouteOption();
$uri = self::parseUri($uri, $groupMeta);
return self::$routeMap[$uri]['POST'] = [
self::$routeMap[$uri]['POST'] = [
'group_meta' => $groupMeta,
'method' => ['POST'],
'route_meta' => $routeMeta
'route_meta' => $routeMeta,
'route_option' => &$routeOption
];

return $routeOption;
}

/**
* @param string $uri
* @param array $routeMeta
* @return array
* @return RouteOption
*/
public static function put(string $uri, array $routeMeta)
{
Expand All @@ -94,18 +101,21 @@ public static function put(string $uri, array $routeMeta)
$groupMeta = Context::get('__current_request_group_meta') ?? [];
}

$routeOption = new RouteOption();
$uri = self::parseUri($uri, $groupMeta);
return self::$routeMap[$uri]['PUT'] = [
self::$routeMap[$uri]['PUT'] = [
'group_meta' => $groupMeta,
'method' => ['PUT'],
'route_meta' => $routeMeta
'route_meta' => $routeMeta,
'route_option' => &$routeOption
];
return $routeOption;
}

/**
* @param string $uri
* @param array $routeMeta
* @return array
* @return RouteOption
*/
public static function delete(string $uri, array $routeMeta)
{
Expand All @@ -114,18 +124,21 @@ public static function delete(string $uri, array $routeMeta)
$groupMeta = Context::get('__current_request_group_meta') ?? [];
}

$routeOption = new RouteOption();
$uri = self::parseUri($uri, $groupMeta);
return self::$routeMap[$uri]['DELETE'] = [
self::$routeMap[$uri]['DELETE'] = [
'group_meta' => $groupMeta,
'method' => ['DELETE'],
'route_meta' => $routeMeta
'route_meta' => $routeMeta,
'route_option' => &$routeOption
];
return $routeOption;
}

/**
* @param string $uri
* @param array $routeMeta
* @return array
* @return RouteOption
*/
public static function head(string $uri, array $routeMeta)
{
Expand All @@ -134,19 +147,22 @@ public static function head(string $uri, array $routeMeta)
$groupMeta = Context::get('__current_request_group_meta') ?? [];
}

$routeOption = new RouteOption();
$uri = self::parseUri($uri, $groupMeta);
return self::$routeMap[$uri]['HEAD'] = [
self::$routeMap[$uri]['HEAD'] = [
'group_meta' => $groupMeta,
'method' => ['HEAD'],
'route_meta' => $routeMeta
'route_meta' => $routeMeta,
'route_option' => &$routeOption
];
return $routeOption;
}

/**
* @param array $methods
* @param string $uri
* @param array $routeMeta
* @return array
* @return RouteOption
*/
public static function match(array $methods, string $uri, array $routeMeta)
{
Expand All @@ -155,21 +171,24 @@ public static function match(array $methods, string $uri, array $routeMeta)
$groupMeta = Context::get('__current_request_group_meta') ?? [];
}

$routeOption = new RouteOption();
foreach ($methods as $method) {
$method = strtoupper($method);
$uri = self::parseUri($uri, $groupMeta);
self::$routeMap[$uri][$method] = [
'group_meta' => $groupMeta,
'method' => [$method],
'route_meta' => $routeMeta
'route_meta' => $routeMeta,
'route_option' => &$routeOption
];
}
return $routeOption;
}

/**
* @param string $uri
* @param array $routeMeta
* @return array
* @return RouteOption
*/
public static function any(string $uri, array $routeMeta)
{
Expand All @@ -178,14 +197,17 @@ public static function any(string $uri, array $routeMeta)
$groupMeta = Context::get('__current_request_group_meta') ?? [];
}

$routeOption = new RouteOption();
foreach (self::HTTP_METHODS as $method) {
$uri = self::parseUri($uri, $groupMeta);
self::$routeMap[$uri][$method] = [
'group_meta' => $groupMeta,
'method' => [$method],
'route_meta' => $routeMeta
'route_meta' => $routeMeta,
'route_option' => &$routeOption
];
}
return $routeOption;
}

/**
Expand Down
Loading

0 comments on commit adaee2b

Please sign in to comment.