-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
49 lines (34 loc) · 1.08 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
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Dotenv\Dotenv;
use Penate\Server\Controller;
use Penate\Server\Request;
use Penate\Server\Storage;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Server as HttpServer;
use React\Socket\Server as SocketServer;
$dotenv = new Dotenv(__DIR__);
$dotenv->load();
$loop = Factory::create();
$storage = new Storage();
$controller = new Controller($storage);
$loop->addPeriodicTimer(5, function () use ($storage) {
$limit = (int) env('MEMORY_LIMIT',128);
if ($storage->memoryLimitExceeded($limit)) {
$storage->clearStorage();
}
});
$loop->addPeriodicTimer(0.8, function () use ($storage) {
$storage->updateTime();
$storage->removeExpired();
});
$server = new HttpServer(function (ServerRequestInterface $serverRequest) use ($controller) {
$request = new Request($serverRequest);
return $controller->handler($request);
});
$socket = new SocketServer( (int) env('PORT', 8000), $loop);
$server->listen($socket);
echo "Penate server running\n";
$loop->run();