server.php 2.2 KB

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