集成进lumen
This commit is contained in:
parent
40d2d457f5
commit
caf6b8c8e5
|
@ -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 {
|
||||
|
|
|
@ -1,7 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: houxuejie
|
||||
* Date: 2019/1/4
|
||||
* Time: 11:56 AM
|
||||
|
||||
namespace JsonRpc\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use JsonRpc\Client;
|
||||
|
||||
class ClientServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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'];
|
||||
}
|
||||
|
||||
}
|
103
src/views/tool.blade.php
Normal file
103
src/views/tool.blade.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Json Rpc Debug Tool</title>
|
||||
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="https://cdn.bootcss.com/highlight.js/7.3/styles/dark.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0">
|
||||
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Json Rpc Debug Tool</a>
|
||||
{{--<input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">--}}
|
||||
{{--<ul class="navbar-nav px-3">--}}
|
||||
{{--<li class="nav-item text-nowrap">--}}
|
||||
{{--<a class="nav-link" href="#">Sign out</a>--}}
|
||||
{{--</li>--}}
|
||||
{{--</ul>--}}
|
||||
</nav>
|
||||
<div class="container-fluid">
|
||||
|
||||
<div class="row">
|
||||
|
||||
<nav class="col-md-3 d-none d-md-block bg-light sidebar">
|
||||
<div class="sidebar-sticky">
|
||||
<ul class="nav flex-column">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="#">
|
||||
<span data-feather="home"></span>
|
||||
History <span class="sr-only">(current)</span>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="#">
|
||||
<span data-feather="file"></span>
|
||||
abc
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
<main role="main" class="col-md-8 ml-sm-auto col-lg-9 pt-3 px-4">
|
||||
<h2>Request</h2>
|
||||
<form method="POST">
|
||||
<div class="form-row">
|
||||
<div class="form-group col-md-12">
|
||||
<label for="endpoint">Endpoint</label>
|
||||
<input type="text" class="form-control" id="endpoint" placeholder="Endpoint"
|
||||
value="{{$endpoint}}" readonly>
|
||||
</div>
|
||||
{{--<div class="form-group col-md-2">--}}
|
||||
{{--<label for="method">Request Method</label>--}}
|
||||
{{--<select class="form-control" id="method">--}}
|
||||
{{--<option>GET</option>--}}
|
||||
{{--<option>POST</option>--}}
|
||||
{{--</select>--}}
|
||||
{{--</div>--}}
|
||||
</div>
|
||||
<div class="form-row">
|
||||
|
||||
<div class="form-group col-md-12">
|
||||
<label for="inputAddress">Method</label>
|
||||
<select class="form-control" id="method" name="method">
|
||||
@foreach($methods as $k => $v)
|
||||
<option>{{$k}}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
|
||||
<div class="form-group col-md-12">
|
||||
<label for="inputAddress">Params</label>
|
||||
<input type="text" class="form-control" name="params" id="params" placeholder="逗号分隔"
|
||||
value="{{$params}}">
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Request</button>
|
||||
</form>
|
||||
<div class="row col-md-12">
|
||||
@if( !empty($error) )
|
||||
<div id='alert' class="alert alert-danger" role="alert">
|
||||
RpcServerException: {{$error['message']}} with code {{$error['code']}}
|
||||
</div>
|
||||
@endif
|
||||
@if( !empty($result) )
|
||||
<h5>Result:</h5>
|
||||
|
||||
<div class="col-md-12">
|
||||
<pre><code class="html">{{$result}}</code></pre>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
|
||||
<script>window.jQuery || document.write('<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.slim.js"><\/script>')</script>
|
||||
<script src="https://cdn.bootcss.com/twitter-bootstrap/4.2.1/js/bootstrap.min.js"></script>
|
||||
<script src="https://cdn.bootcss.com/highlight.js/9.13.1/highlight.min.js"></script>
|
||||
<script>hljs.initHighlightingOnLoad();</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user