Merge branch 'feature/tunnel'

This commit is contained in:
dongwei 2019-01-31 15:06:57 +08:00
commit d28d9973c1
2 changed files with 63 additions and 52 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
namespace JsonRpc\Middleware; namespace JsonRpc\Middleware;
use Closure; use Closure;
@ -13,48 +14,48 @@ use InfluxDB\Point;
*/ */
class TunnelMiddleware class TunnelMiddleware
{ {
/** /**
* Handle an incoming request. * Handle an incoming request.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Closure $next * @param \Closure $next
* @return mixed * @return mixed
*/ */
public function handle($request, Closure $next) public function handle($request, Closure $next)
{ {
// Pre-Middleware Action // Pre-Middleware Action
$response = $next($request); $response = $next($request);
// Post-Middleware Action // Post-Middleware Action
return $response; return $response;
} }
/** /**
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Closure $response * @param \Closure $response
*/ */
public function terminate($request, $response) public function terminate($request, $response)
{ {
//过滤tool返回结果 //过滤tool返回结果
if ($response instanceof JsonResponse) if ($response instanceof JsonResponse) {
{ if (app()->environment('dev', 'production')) {
$content = $response->getOriginalContent(); $content = $response->getOriginalContent();
$status = isset($content['error']) ? $content['error']['code'] : 200; $status = isset($content['error']) ? $content['error']['code'] : 200;
$client = new \InfluxDB\Client("10.0.1.67");
$client = new \InfluxDB\Client('http://localhost', '8086'); $database = $client->selectDB('rpc_monitor');
$database = $client->selectDB('rpc_monitor'); $points = array(
$points = array( new Point(
new Point( 'monitor',
'monitor', null,
0.64, ['app' => env('APP_NAME'), 'status' => $status, 'env' => app()->environment()],
['app' =>env('APP_NAME'), 'status' => $status], ['content' => $request->getContent()]
['content' => $request->getContent()] )
) );
); $database->writePoints($points, Database::PRECISION_SECONDS);
$result = $database->writePoints($points, Database::PRECISION_SECONDS); }
app('rpc.logger')->info('rpc tunnel ctx ' [$result]);
} }
} }
} }

View File

@ -29,14 +29,24 @@ class JsonRpcMethod extends JsonRpc
public function error($code, $msg) public function error($code, $msg)
{ {
return [ return is_string($msg)
'jsonrpc' => '2.0', ? [
'error' => [ 'jsonrpc' => '2.0',
'code' => $code, 'error' => [
'message' => is_array($msg) ? json_encode($msg) : $msg, 'code' => $code,
], 'message' => $msg,
'id' => $this->id ],
]; 'id' => $this->id
]
: [
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
'message' => self::ErrorMsg[$code],
'data' => $msg
],
'id' => $this->id
];
} }
} }