server-mysqli.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. require_once __DIR__.'/vendor/autoload.php';
  3. require_once __DIR__.'/db-mysqli.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 = shell_exec('nproc');
  8. $http_worker->onWorkerStart = function () {
  9. global $db;
  10. $db = new mysqli('p:tfb-database', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  11. };
  12. $http_worker->onMessage = static function ($connection) {
  13. Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
  14. switch (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) {
  15. case '/db':
  16. Http::header('Content-Type: application/json');
  17. return $connection->send(db());
  18. case '/fortune':
  19. //Http::header('Content-Type: text/html; charset=utf-8');
  20. return $connection->send(fortune());
  21. case '/update':
  22. Http::header('Content-Type: application/json');
  23. return $connection->send(update());
  24. //case '/info':
  25. // Http::header('Content-Type: text/plain');
  26. // ob_start();
  27. // phpinfo();
  28. // return $connection->send(ob_get_clean());
  29. default:
  30. Http::responseCode(404);
  31. $connection->send('Error 404');
  32. }
  33. };
  34. Worker::runAll();