server.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. PDO::ATTR_EMULATE_PREPARES => false]
  16. );
  17. $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
  18. $statement = $pdo->prepare('SELECT id,randomNumber FROM World 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();