diff --git a/src/Middleware/TunnelMiddleware.php b/src/Middleware/TunnelMiddleware.php index 3d49b7f..ff59f5c 100644 --- a/src/Middleware/TunnelMiddleware.php +++ b/src/Middleware/TunnelMiddleware.php @@ -1,4 +1,5 @@ getOriginalContent(); - $status = isset($content['error']) ? $content['error']['code'] : 200; - - $client = new \InfluxDB\Client('http://localhost', '8086'); - $database = $client->selectDB('rpc_monitor'); - $points = array( - new Point( - 'monitor', - 0.64, - ['app' =>env('APP_NAME'), 'status' => $status], - ['content' => $request->getContent()] - ) - ); - $result = $database->writePoints($points, Database::PRECISION_SECONDS); - app('rpc.logger')->info('rpc tunnel ctx ' [$result]); - } - } - + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * @return mixed + */ + public function handle($request, Closure $next) + { + // Pre-Middleware Action + $response = $next($request); + + // Post-Middleware Action + + return $response; + } + + /** + * @param \Illuminate\Http\Request $request + * @param \Closure $response + */ + public function terminate($request, $response) + { + //过滤tool返回结果 + if ($response instanceof JsonResponse) { + if (app()->environment('dev', 'production')) { + $content = $response->getOriginalContent(); + $status = isset($content['error']) ? $content['error']['code'] : 200; + $client = new \InfluxDB\Client("10.0.1.67"); + $database = $client->selectDB('rpc_monitor'); + $points = array( + new Point( + 'monitor', + null, + ['app' => env('APP_NAME'), 'status' => $status, 'env' => app()->environment()], + ['content' => $request->getContent()] + ) + ); + $database->writePoints($points, Database::PRECISION_SECONDS); + } + + } + } + } \ No newline at end of file diff --git a/src/Server/JsonRpcMethod.php b/src/Server/JsonRpcMethod.php index e0a8eb1..c26a94c 100644 --- a/src/Server/JsonRpcMethod.php +++ b/src/Server/JsonRpcMethod.php @@ -29,14 +29,24 @@ class JsonRpcMethod extends JsonRpc public function error($code, $msg) { - return [ - 'jsonrpc' => '2.0', - 'error' => [ - 'code' => $code, - 'message' => is_array($msg) ? json_encode($msg) : $msg, - ], - 'id' => $this->id - ]; + return is_string($msg) + ? [ + 'jsonrpc' => '2.0', + 'error' => [ + 'code' => $code, + 'message' => $msg, + ], + 'id' => $this->id + ] + : [ + 'jsonrpc' => '2.0', + 'error' => [ + 'code' => $code, + 'message' => self::ErrorMsg[$code], + 'data' => $msg + ], + 'id' => $this->id + ]; } } \ No newline at end of file