-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.php
52 lines (41 loc) · 1.38 KB
/
server.php
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
<?php
require __DIR__ . "/vendor/autoload.php";
use Icicle\Http\Message\RequestInterface;
use Icicle\Http\Message\Response;
use Icicle\Http\Server\Server;
use Icicle\Loop;
use Icicle\Socket\Client\ClientInterface;
use League\Route\Http\Exception\MethodNotAllowedException;
use League\Route\Http\Exception\NotFoundException;
use League\Route\RouteCollection;
use League\Route\Strategy\UriStrategy;
$server = new Server(
function(RequestInterface $request, ClientInterface $client) {
$router = new RouteCollection();
$router->setStrategy(new UriStrategy());
require __DIR__ . "/routes.php";
$dispatcher = $router->getDispatcher();
try {
$result = $dispatcher->dispatch(
$request->getMethod(),
$request->getRequestTarget()
);
$status = 200;
$content = $result->getContent();
} catch (NotFoundException $exception) {
$status = 404;
$content = "not found";
} catch (MethodNotAllowedException $exception) {
$status = 405;
$content = "method not allowed";
}
$response = new Response($status);
$response = $response->withHeader(
"Content-Type", "text/html"
);
yield $response->getBody()->end($content);
yield $response;
}
);
$server->listen(9001);
Loop\run();