server.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Worker;
  7. use Workerman\Protocols\Http;
  8. function get_processor_cores_number() {
  9. $command = 'cat /proc/cpuinfo | grep processor | wc -l';
  10. return (int) shell_exec($command);
  11. }
  12. $http_worker = new Worker('http://0.0.0.0:8080');
  13. $http_worker->count = get_processor_cores_number() * 2 || 8;
  14. $http_worker->onMessage = function($connection, $data)
  15. {
  16. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8',
  17. 'benchmarkdbuser', 'benchmarkdbpass', array(
  18. PDO::ATTR_PERSISTENT => true
  19. ));
  20. $base = $_SERVER['REQUEST_URI'];
  21. $question = strpos($base, '?');
  22. if ($question !== false) {
  23. $base = substr($base, 0, $question);
  24. }
  25. Http::header('Date: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  26. if ($base == '/fortune.php') {
  27. Http::header('Content-Type: text/html; charset=utf-8');
  28. ob_start();
  29. fortune($pdo);
  30. $connection->send(ob_get_clean());
  31. } else if ($base == '/dbraw.php') {
  32. Http::header('Content-Type: application/json');
  33. ob_start();
  34. dbraw($pdo);
  35. $connection->send(ob_get_clean());
  36. } else if ($base == '/updateraw.php') {
  37. Http::header('Content-Type: application/json');
  38. ob_start();
  39. updateraw($pdo);
  40. $connection->send(ob_get_clean());
  41. } else if ($base == '/plaintext.php') {
  42. Http::header('Content-Type: text/plain');
  43. $connection->send('Hello, World!');
  44. } else if ($base == '/json.php') {
  45. Http::header('Content-Type: application/json');
  46. $connection->send(json_encode(['message'=>'Hello, World!']));
  47. } else {
  48. Http::header('Content-Type: application/json');
  49. $connection->send(json_encode(['message'=>'Hello, World!']));
  50. }
  51. };
  52. Worker::runAll();