server.php 1.9 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\Protocols\Http;
  7. use Workerman\Worker;
  8. $http_worker = new Worker('http://0.0.0.0:8080');
  9. $http_worker->count = shell_exec('nproc') * 3;
  10. $http_worker->onWorkerStart = function () {
  11. global $pdo, $fortune, $statement;
  12. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world',
  13. 'benchmarkdbuser', 'benchmarkdbpass',
  14. [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
  15. $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
  16. $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
  17. };
  18. $http_worker->onMessage = static function ($connection) {
  19. Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
  20. switch (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) {
  21. case '/plaintext':
  22. Http::header('Content-Type: text/plain');
  23. return $connection->send('Hello, World!');
  24. case '/json':
  25. Http::header('Content-Type: application/json');
  26. return $connection->send(json_encode(['message' => 'Hello, World!']));
  27. case '/db':
  28. Http::header('Content-Type: application/json');
  29. return $connection->send(dbraw());
  30. case '/fortune':
  31. //Http::header('Content-Type: text/html; charset=utf-8');
  32. return $connection->send(fortune());
  33. case '/update':
  34. Http::header('Content-Type: application/json');
  35. return $connection->send(updateraw());
  36. //case '/info':
  37. // Http::header('Content-Type: text/plain');
  38. // ob_start();
  39. // phpinfo();
  40. // return $connection->send(ob_get_clean());
  41. default:
  42. Http::responseCode(404);
  43. $connection->send('Error 404');
  44. }
  45. };
  46. Worker::runAll();