-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver
108 lines (90 loc) · 3.02 KB
/
server
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use React\EventLoop\Factory;
use React\Http\Server;
use RingCentral\Psr7\Response;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
require __DIR__ . "/vendor/autoload.php";
const HTTP_OK = 200;
const HTTP_INTERNAL_SERVER_ERROR = 500;
const HTTP_MOVED_PERMANENTLY = 301;
const HTTP_PERMANENT_REDIRECT = 308;
/**
* @param int $responseCode
*
* @return int
*/
function getColorCodeByResponseCode(int $responseCode): int
{
if (300 <= $responseCode && $responseCode < 400) {
return 34;
} elseif (418 === $responseCode) {
return 96;
} elseif (400 <= $responseCode && $responseCode < HTTP_MOVED_PERMANENTLY) {
return 31;
} elseif (HTTP_MOVED_PERMANENTLY === $responseCode) {
return 95;
}
return 32;
}
/**
* Server initialization
*/
$inputDefinition = (new InputDefinition([
new InputArgument('port', InputArgument::REQUIRED)
]));
$input = new ArgvInput($argv, $inputDefinition);
$port = $input->getArgument('port');
$loop = Factory::create();
echo '#' . PHP_EOL;
echo '# Tiny Server, by DriftPHP' . PHP_EOL;
echo '#' . PHP_EOL;
echo '# EventLoop > running ('.get_class($loop).')' . PHP_EOL;
echo '# Listening > 0.0.0.0:'.$port . PHP_EOL;
echo '# Each dot is a request' . PHP_EOL;
echo "# \033[32m*\033[0m ~200" . PHP_EOL;
echo "# \033[34m*\033[0m ~300" . PHP_EOL;
echo "# \033[31m*\033[0m ~400" . PHP_EOL;
echo "# \033[95m*\033[0m =500" . PHP_EOL;
echo '#' . PHP_EOL;
$server = new Server($loop, function (ServerRequestInterface $request) use ($port) {
$requestCode = \intval(trim($request->getUri()->getPath(), '/'));
$responseCode = HTTP_OK;
$responseMessage = 'DriftPHP rocks!';
$responseHeaders = [];
if (
$requestCode >= HTTP_OK &&
$requestCode <= HTTP_INTERNAL_SERVER_ERROR
) {
$responseCode = $requestCode;
}
if ($requestCode === HTTP_MOVED_PERMANENTLY || $requestCode === HTTP_PERMANENT_REDIRECT) {
$host = $request->getServerParams()['SERVER_ADDR'] ?? '0.0.0.0';
$responseHeaders['Location'] = "$host:$port/418";
$responseMessage = 'DriftPHP is a Teapot!';
}
$color = getColorCodeByResponseCode($responseCode);
echo "\033[{$color}m*\033[0m";
return new Response($responseCode, $responseHeaders, $responseMessage);
});
$signalHandler = function () use (&$signalHandler, $server, $loop) {
echo PHP_EOL;
echo '#' . PHP_EOL;
echo '# EventLoop > stopped' . PHP_EOL;
echo '# Bye Bye!' . PHP_EOL;
echo '#' . PHP_EOL;
$loop->removeSignal(SIGINT, $signalHandler);
$loop->removeSignal(SIGTERM, $signalHandler);
$loop->stop();
};
$loop->addSignal(SIGINT, $signalHandler);
$loop->addSignal(SIGTERM, $signalHandler);
$socket = new React\Socket\Server("0.0.0.0:$port", $loop);
$server->listen($socket);
$loop->run();