Request
+ +Result:
+ +{{$result}}
+ diff --git a/src/Client.php b/src/Client.php index a69bc55..6de3664 100644 --- a/src/Client.php +++ b/src/Client.php @@ -7,19 +7,27 @@ use JsonRpc\Exception\RpcServerException; class Client { - protected $id; + protected $config; + protected $id; protected $http; - public function __construct() + public function __construct($config) { + $this->config = $config; $this->id = 0; - $this->http = new \GuzzleHttp\Client([ - 'base_uri' => 'http://auth.lo.haowumc.com', + } + + public function endpoint($k) + { + $config = $this->config[$k]; + + $default = [ 'timeout' => 3, 'allow_redirects' => false, -// 'proxy' => '192.168.16.1:10' - ]); + ]; + + $this->http = new \GuzzleHttp\Client(array_merge($default,$config)); } /** @@ -58,11 +66,11 @@ class Client protected function post($payload) { try { - $resp = $this->http->request('POST', 'rpc/gateway.json', [ + $resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [ 'json' => $payload ]); } catch (ServerException $e) { - throw new RpcServerException($e->getMessage(),$e->getCode()); + throw new RpcServerException($e->getMessage(), $e->getCode()); } try { diff --git a/src/Providers/ClientServiceProvider.php b/src/Providers/ClientServiceProvider.php index e34079d..775ae56 100644 --- a/src/Providers/ClientServiceProvider.php +++ b/src/Providers/ClientServiceProvider.php @@ -1,7 +1,30 @@ app->configure('rpc'); + + $config = config('rpc.client'); + $this->app->singleton('rpc', function () use ($config) { + return new Client($config); + }); + + foreach ($config as $k => $item) { + $this->app->singleton('rpc.' . $k, function () use ($k) { + app('rpc')->endpoint($k); + }); + } + } +} \ No newline at end of file diff --git a/src/Providers/LumenServerServiceProvider.php b/src/Providers/LumenServerServiceProvider.php index a07629f..e487751 100644 --- a/src/Providers/LumenServerServiceProvider.php +++ b/src/Providers/LumenServerServiceProvider.php @@ -4,6 +4,7 @@ namespace JsonRpc\Providers; use Illuminate\Support\ServiceProvider; use JsonRpc\Server\JsonRpcServer; +use JsonRpc\Server\JsonRpcTool; use Laravel\Lumen\Application; class LumenServerServiceProvider extends ServiceProvider @@ -27,7 +28,6 @@ class LumenServerServiceProvider extends ServiceProvider ], function () { $this->app->configure('rpc'); - $config = config('rpc.server'); $callback = function () use ($config) { @@ -39,15 +39,14 @@ class LumenServerServiceProvider extends ServiceProvider $this->app->router->get('json-rpc-v2.json', $callback); if (function_exists('env') && env('APP_DEBUG')) { - $this->app->router->get('tools.html', function () { - $doc = new JsonRpcTool(); - return $doc->render(); - }); -// $this->app->router->get('doc.html', function () { -// $doc = new JsonRpcDoc(base_path('app/Rpc/')); -// return $doc->render(); -// }); + $tool = function () use ($config) { + $doc = new JsonRpcTool($config); + return $doc->render(); + }; + + $this->app->router->get('tool.html', $tool); + $this->app->router->post('tool.html', $tool); } }); diff --git a/src/Server/JsonRpcServer.php b/src/Server/JsonRpcServer.php index fd7d184..bc6ddad 100644 --- a/src/Server/JsonRpcServer.php +++ b/src/Server/JsonRpcServer.php @@ -50,7 +50,7 @@ class JsonRpcServer } else { list($method, $params, $id) = $this->parseJson($this->request->getContent()); } - + list($class, $function) = $this->parseMethodWithMap($method); // dump($class,$function);exit; diff --git a/src/Server/JsonRpcTool.php b/src/Server/JsonRpcTool.php index a7c7825..49ce174 100644 --- a/src/Server/JsonRpcTool.php +++ b/src/Server/JsonRpcTool.php @@ -2,15 +2,73 @@ namespace JsonRpc\Server; +use Illuminate\Http\Request; +use Illuminate\View\Factory; +use JsonRpc\Exception\RpcServerException; + +/** + * Class JsonRpcTool + * for lumen + * @package JsonRpc\Server + */ class JsonRpcTool { - public function __construct() + + protected $config; + + public function __construct($config) { + $this->config = $config; } public function render() { - return file_get_contents('abc.html'); + /** + * @var $request Request + */ + $request = app('request'); + + /** + * @var $view Factory + */ + $view = view(); + + $params = json_decode($request->input('params'), true); + + if ($request->method() == Request::METHOD_POST) { + + $method = $request->input('method'); + + try { + $result = app('rpc')->call($method, $params); + $view->share('result', json_encode($result, JSON_PRETTY_PRINT)); + } catch (RpcServerException $exception) { + $view->share('error', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]); + } + } + + $view->share('endpoint', $this->getEndpoint()); + $view->share('methods', $this->getMethods()); + $view->share('params', json_encode($params)); + + return $view->exists('tool') ? + $view->make('tool') : + $view->file(__DIR__ . '/../views/tool.blade.php'); + } + + public function getEndpoint() + { + + /** + * @var $request Request + */ + $request = app('request'); + return $request->getSchemeAndHttpHost() . '/rpc/json-rpc-v2.json'; + } + + public function getMethods() + { + return include_once $this->config['map']; } } \ No newline at end of file diff --git a/src/views/tool.blade.php b/src/views/tool.blade.php new file mode 100644 index 0000000..b8b1904 --- /dev/null +++ b/src/views/tool.blade.php @@ -0,0 +1,103 @@ + +
+{{$result}}
+