|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Jiaoyin;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/func.php';
|
|
|
|
|
|
|
|
use Swoole\Http\Request;
|
|
|
|
use Swoole\Http\Response;
|
|
|
|
use Swoole\Coroutine\Http\Server;
|
|
|
|
use function Jiaoyin\output;
|
|
|
|
|
|
|
|
class SimpleServerCoroutine
|
|
|
|
{
|
|
...
|
...
|
@@ -11,35 +16,52 @@ class SimpleServerCoroutine |
|
|
|
{
|
|
|
|
$this->httpServer = new Server($host, $port, $ssl);
|
|
|
|
}
|
|
|
|
public function router($method, $path, $callback){
|
|
|
|
$this->httpServer->handle($path, function (Request $request, Response $response) use ($method,$callback) {
|
|
|
|
public function router(array $method, $path, $callback)
|
|
|
|
{
|
|
|
|
$this->httpServer->handle($path, function (Request $request, Response $response) use ($method, $callback) {
|
|
|
|
$m = $request->getMethod();
|
|
|
|
if(!in_array($m,$method)){
|
|
|
|
foreach ($method as $k => $v) {
|
|
|
|
$method[$k] = strtolower($v);
|
|
|
|
}
|
|
|
|
$m = strtolower($m);
|
|
|
|
if (!in_array($m, $method)) {
|
|
|
|
$response->status(405);
|
|
|
|
$response->end();
|
|
|
|
$data = [
|
|
|
|
'code' => 405,
|
|
|
|
'msg' => 'Method Not Allowed',
|
|
|
|
'currentMethod' => $m,
|
|
|
|
'allowMethod' => $method,
|
|
|
|
];
|
|
|
|
$response->end(json_encode($data));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$requestInfo = [
|
|
|
|
'method' => $request->getMethod(),
|
|
|
|
'path' => $request->server['path_info'],
|
|
|
|
'uri' => $request->server['request_uri'],
|
|
|
|
'get' => $request->get ?: [],
|
|
|
|
'post' => $request->post ?: [],
|
|
|
|
'method' => $request->getMethod(),
|
|
|
|
'header' => $request->header ?: [],
|
|
|
|
'cookie' => $request->cookie ?: [],
|
|
|
|
'rawContent' => $request->rawContent() ?: ''
|
|
|
|
];
|
|
|
|
output($requestInfo['method'], $requestInfo['path'], "GET:" . json_encode($requestInfo['get']), "POST:" . json_encode($requestInfo['post']), "rawContent:" . $requestInfo['rawContent']);
|
|
|
|
$simpleRequest = new SimpleRequest($requestInfo);
|
|
|
|
$res = call_user_func($callback, $simpleRequest);
|
|
|
|
if(empty($res)){
|
|
|
|
$response->end(json_encode(['code'=>-1,'msg'=>'nothing return']));
|
|
|
|
}else{
|
|
|
|
if (empty($res)) {
|
|
|
|
$response->end(json_encode(['code' => -1, 'msg' => 'nothing return']));
|
|
|
|
} else {
|
|
|
|
$response->end($res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function start(){
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
$this->httpServer->start();
|
|
|
|
}
|
|
|
|
public function stop()
|
|
|
|
{
|
|
|
|
$this->httpServer->shutdown();
|
|
|
|
}
|
|
|
|
} |
|
|
|
|
...
|
...
|
|