SimpleServer.php
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Jiaoyin;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
class SimpleServer
{
private Server $httpServer;
public function __construct($host, $port)
{
$this->httpServer = new Server($host, $port);
}
public function router($url, $callback){
$this->httpServer->on('Request', function (Request $request, Response $response) use ($url, $callback){
$requestInfo = [
'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 ?: [],
];
$simpleRequest = new SimpleRequest($requestInfo);
if ($simpleRequest->path == '/favicon.ico' || $simpleRequest->uri == '/favicon.ico') {
$response->end();
return;
}
if($url == $simpleRequest->uri){
$res = call_user_func($callback, $simpleRequest);
if(!$res){
$response->end();
}else{
$response->end($res);
}
return;
}
$response->end("<h3>Hello Simple Server</h3>");
});
}
public function start(){
$this->httpServer->start();
}
}