2023-02-08 05:46:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Support\ClusterSupport;
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class ReportRequestToCluster
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param Closure(Request): (Response|RedirectResponse) $next
|
|
|
|
*/
|
2023-02-08 06:21:07 +00:00
|
|
|
public function handle(Request $request, Closure $next): mixed
|
2023-02-08 05:46:21 +00:00
|
|
|
{
|
2023-02-12 19:06:41 +00:00
|
|
|
$random_id = Str::random();
|
2023-02-08 05:46:21 +00:00
|
|
|
|
|
|
|
$method = $request->method();
|
|
|
|
$path = $request->path();
|
|
|
|
|
|
|
|
// ClusterSupport::publish('http.incoming', [
|
|
|
|
// 'id' => $random_id,
|
|
|
|
// 'method' => $method,
|
|
|
|
// 'path' => $path,
|
|
|
|
// 'query' => $request->query(),
|
|
|
|
// 'body' => $request->all(),
|
|
|
|
// // 'headers' => $request->headers->all(),
|
|
|
|
// 'ip' => $request->ip(),
|
|
|
|
// 'user-agent' => $request->userAgent(),
|
|
|
|
// 'user' => $request->user(),
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
$start = microtime(true);
|
|
|
|
|
|
|
|
$response = $next($request);
|
|
|
|
|
|
|
|
$end = microtime(true);
|
|
|
|
|
2023-02-09 19:04:13 +00:00
|
|
|
$status = 0;
|
|
|
|
// 检测有没有 status() 方法。
|
|
|
|
if (method_exists($response, 'status')) {
|
|
|
|
$status = $response->status();
|
|
|
|
}
|
|
|
|
|
2023-02-08 05:46:21 +00:00
|
|
|
ClusterSupport::publish('http.outgoing', [
|
|
|
|
'id' => $random_id,
|
|
|
|
'method' => $method,
|
2023-02-09 19:04:13 +00:00
|
|
|
'status' => $status,
|
2023-02-08 05:46:21 +00:00
|
|
|
'path' => $path,
|
|
|
|
// 'headers' => $response->headers->all(),
|
2023-02-08 05:57:10 +00:00
|
|
|
// 'content' => $response->getContent(),
|
2023-02-08 05:46:21 +00:00
|
|
|
'user' => $request->user(),
|
|
|
|
'time' => round(($end - $start) * 1000),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|