server.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. require_once __DIR__ . '/fortune.php';
  4. require_once __DIR__ . '/dbraw.php';
  5. require_once __DIR__ . '/updateraw.php';
  6. use Workerman\Worker;
  7. use Workerman\Protocols\Http;
  8. $http_worker = new Worker('http://0.0.0.0:8080');
  9. $http_worker->count = (int) shell_exec('nproc') ?? 64;
  10. $http_worker->onWorkerStart = function()
  11. {
  12. global $pdo;
  13. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8',
  14. 'benchmarkdbuser', 'benchmarkdbpass');
  15. };
  16. $http_worker->onMessage = function($connection)
  17. {
  18. global $pdo;
  19. $base = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  20. Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
  21. if ($base === '/fortune') {
  22. Http::header('Content-Type: text/html; charset=utf-8');
  23. ob_start();
  24. fortune($pdo);
  25. $connection->send(ob_get_clean());
  26. } elseif ($base === '/db') {
  27. Http::header('Content-Type: application/json');
  28. ob_start();
  29. dbraw($pdo);
  30. $connection->send(ob_get_clean());
  31. } elseif ($base === '/update') {
  32. Http::header('Content-Type: application/json');
  33. ob_start();
  34. updateraw($pdo);
  35. $connection->send(ob_get_clean());
  36. } elseif ($base === '/plaintext') {
  37. Http::header('Content-Type: text/plain');
  38. $connection->send('Hello, World!');
  39. } elseif ($base === '/json') {
  40. Http::header('Content-Type: application/json');
  41. $connection->send(json_encode(['message'=>'Hello, World!']));
  42. // } elseif ($base === '/info') {
  43. // Http::header('Content-Type: text/plain');
  44. // ob_start();
  45. // phpinfo();
  46. // $connection->send(ob_get_clean());
  47. }
  48. };
  49. Worker::runAll();