server.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Protocols\Http;
  7. use Workerman\Worker;
  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. global $pdo;
  12. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8',
  13. 'benchmarkdbuser', 'benchmarkdbpass');
  14. };
  15. $http_worker->onMessage = function ($connection) {
  16. global $pdo;
  17. Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
  18. switch (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) {
  19. case '/plaintext':
  20. Http::header('Content-Type: text/plain');
  21. $connection->send('Hello, World!');
  22. break;
  23. case '/json':
  24. Http::header('Content-Type: application/json');
  25. $connection->send(json_encode(['message' => 'Hello, World!']));
  26. break;
  27. case '/db':
  28. Http::header('Content-Type: application/json');
  29. $connection->send(dbraw($pdo));
  30. break;
  31. case '/fortune':
  32. Http::header('Content-Type: text/html; charset=utf-8');
  33. $connection->send(fortune($pdo));
  34. break;
  35. case '/update':
  36. Http::header('Content-Type: application/json');
  37. $connection->send(updateraw($pdo));
  38. break;
  39. //case '/info':
  40. // Http::header('Content-Type: text/plain');
  41. // ob_start();
  42. // phpinfo();
  43. // $connection->send(ob_get_clean());
  44. //default:
  45. // $connection->send('error');
  46. }
  47. };
  48. Worker::runAll();