server.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 = static 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. return $connection->send('Hello, World!');
  22. case '/json':
  23. Http::header('Content-Type: application/json');
  24. return $connection->send(json_encode(['message' => 'Hello, World!']));
  25. case '/db':
  26. Http::header('Content-Type: application/json');
  27. return $connection->send(dbraw($pdo));
  28. case '/fortune':
  29. Http::header('Content-Type: text/html; charset=utf-8');
  30. return $connection->send(fortune($pdo));
  31. case '/update':
  32. Http::header('Content-Type: application/json');
  33. return $connection->send(updateraw($pdo));
  34. //case '/info':
  35. // Http::header('Content-Type: text/plain');
  36. // ob_start();
  37. // phpinfo();
  38. // $connection->send(ob_get_clean());
  39. //default:
  40. // $connection->send('error');
  41. }
  42. };
  43. Worker::runAll();