|
1
|
<?php
|
1
|
<?php
|
|
|
|
2
|
+
|
|
2
|
namespace Jiaoyin;
|
3
|
namespace Jiaoyin;
|
|
|
|
4
|
+
|
|
|
|
5
|
+require_once __DIR__ . '/func.php';
|
|
|
|
6
|
+
|
|
3
|
use Swoole\Http\Request;
|
7
|
use Swoole\Http\Request;
|
|
4
|
use Swoole\Http\Response;
|
8
|
use Swoole\Http\Response;
|
|
5
|
use Swoole\Coroutine\Http\Server;
|
9
|
use Swoole\Coroutine\Http\Server;
|
|
|
|
10
|
+use function Jiaoyin\output;
|
|
6
|
|
11
|
|
|
7
|
class SimpleServerCoroutine
|
12
|
class SimpleServerCoroutine
|
|
8
|
{
|
13
|
{
|
|
@@ -11,35 +16,52 @@ class SimpleServerCoroutine |
|
@@ -11,35 +16,52 @@ class SimpleServerCoroutine |
|
11
|
{
|
16
|
{
|
|
12
|
$this->httpServer = new Server($host, $port, $ssl);
|
17
|
$this->httpServer = new Server($host, $port, $ssl);
|
|
13
|
}
|
18
|
}
|
|
14
|
- public function router($method, $path, $callback){
|
|
|
|
15
|
- $this->httpServer->handle($path, function (Request $request, Response $response) use ($method,$callback) {
|
19
|
+ public function router(array $method, $path, $callback)
|
|
|
|
20
|
+ {
|
|
|
|
21
|
+ $this->httpServer->handle($path, function (Request $request, Response $response) use ($method, $callback) {
|
|
16
|
$m = $request->getMethod();
|
22
|
$m = $request->getMethod();
|
|
17
|
- if(!in_array($m,$method)){
|
23
|
+ foreach ($method as $k => $v) {
|
|
|
|
24
|
+ $method[$k] = strtolower($v);
|
|
|
|
25
|
+ }
|
|
|
|
26
|
+ $m = strtolower($m);
|
|
|
|
27
|
+ if (!in_array($m, $method)) {
|
|
18
|
$response->status(405);
|
28
|
$response->status(405);
|
|
19
|
- $response->end();
|
29
|
+ $data = [
|
|
|
|
30
|
+ 'code' => 405,
|
|
|
|
31
|
+ 'msg' => 'Method Not Allowed',
|
|
|
|
32
|
+ 'currentMethod' => $m,
|
|
|
|
33
|
+ 'allowMethod' => $method,
|
|
|
|
34
|
+ ];
|
|
|
|
35
|
+ $response->end(json_encode($data));
|
|
20
|
return;
|
36
|
return;
|
|
21
|
}
|
37
|
}
|
|
22
|
$requestInfo = [
|
38
|
$requestInfo = [
|
|
|
|
39
|
+ 'method' => $request->getMethod(),
|
|
23
|
'path' => $request->server['path_info'],
|
40
|
'path' => $request->server['path_info'],
|
|
24
|
'uri' => $request->server['request_uri'],
|
41
|
'uri' => $request->server['request_uri'],
|
|
25
|
'get' => $request->get ?: [],
|
42
|
'get' => $request->get ?: [],
|
|
26
|
'post' => $request->post ?: [],
|
43
|
'post' => $request->post ?: [],
|
|
27
|
- 'method' => $request->getMethod(),
|
|
|
|
28
|
'header' => $request->header ?: [],
|
44
|
'header' => $request->header ?: [],
|
|
29
|
'cookie' => $request->cookie ?: [],
|
45
|
'cookie' => $request->cookie ?: [],
|
|
|
|
46
|
+ 'rawContent' => $request->rawContent() ?: ''
|
|
30
|
];
|
47
|
];
|
|
|
|
48
|
+ output($requestInfo['method'], $requestInfo['path'], "GET:" . json_encode($requestInfo['get']), "POST:" . json_encode($requestInfo['post']), "rawContent:" . $requestInfo['rawContent']);
|
|
31
|
$simpleRequest = new SimpleRequest($requestInfo);
|
49
|
$simpleRequest = new SimpleRequest($requestInfo);
|
|
32
|
$res = call_user_func($callback, $simpleRequest);
|
50
|
$res = call_user_func($callback, $simpleRequest);
|
|
33
|
- if(empty($res)){
|
|
|
|
34
|
- $response->end(json_encode(['code'=>-1,'msg'=>'nothing return']));
|
|
|
|
35
|
- }else{
|
51
|
+ if (empty($res)) {
|
|
|
|
52
|
+ $response->end(json_encode(['code' => -1, 'msg' => 'nothing return']));
|
|
|
|
53
|
+ } else {
|
|
36
|
$response->end($res);
|
54
|
$response->end($res);
|
|
37
|
}
|
55
|
}
|
|
38
|
});
|
56
|
});
|
|
39
|
}
|
57
|
}
|
|
40
|
|
58
|
|
|
41
|
- public function start(){
|
59
|
+ public function start()
|
|
|
|
60
|
+ {
|
|
42
|
$this->httpServer->start();
|
61
|
$this->httpServer->start();
|
|
43
|
}
|
62
|
}
|
|
|
|
63
|
+ public function stop()
|
|
|
|
64
|
+ {
|
|
|
|
65
|
+ $this->httpServer->shutdown();
|
|
|
|
66
|
+ }
|
|
44
|
} |
67
|
} |
|
45
|
- |
|
|