app-pg.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. use Workerman\Protocols\Http\Response;
  3. use Workerman\Protocols\Http\Request;
  4. include 'dbraw.php';
  5. function router(Request $request)
  6. {
  7. return match($request->path()) {
  8. '/plaintext' => text(),
  9. '/json' => json(),
  10. '/db' => db(),
  11. '/fortunes' => fortune(),
  12. '/query' => query($request),
  13. '/update' => updateraw($request),
  14. // '/info' => info(),
  15. default => new Response(404, [], 'Error 404'),
  16. };
  17. }
  18. function text()
  19. {
  20. return new Response(200, [
  21. 'Content-Type' => 'text/plain',
  22. 'Date' => Header::$date
  23. ], 'Hello, World!');
  24. }
  25. function json()
  26. {
  27. return new Response(200, [
  28. 'Content-Type' => 'application/json',
  29. 'Date' => Header::$date
  30. ], json_encode(['message' => 'Hello, World!']));
  31. }
  32. function db()
  33. {
  34. DbRaw::$random->execute([mt_rand(1, 10000)]);
  35. return new Response(200, [
  36. 'Content-Type' => 'application/json',
  37. 'Date' => Header::$date
  38. ], json_encode(DbRaw::$random->fetch()));
  39. }
  40. function query($request)
  41. {
  42. $random = DbRaw::$random;
  43. $query_count = 1;
  44. $q = (int) $request->get('q');
  45. if ($q > 1) {
  46. $query_count = min($q, 500);
  47. }
  48. while ($query_count--) {
  49. $random->execute([mt_rand(1, 10000)]);
  50. $arr[] = $random->fetch();
  51. }
  52. return new Response(200, [
  53. 'Content-Type' => 'application/json',
  54. 'Date' => Header::$date
  55. ], json_encode($arr));
  56. }
  57. function updateraw($request)
  58. {
  59. $random = DbRaw::$random;
  60. $query_count = 1;
  61. $q = (int) $request->get('q');
  62. if ($q > 1) {
  63. $query_count = min($q, 500);
  64. }
  65. while ($query_count--) {
  66. $random->execute([mt_rand(1, 10000)]);
  67. $row = $random->fetch();
  68. $row['randomNumber'] = mt_rand(1, 10000);
  69. $worlds[] = $row;
  70. }
  71. DbRaw::update($worlds);
  72. return new Response(200, [
  73. 'Content-Type' => 'application/json',
  74. 'Date' => Header::$date
  75. ], json_encode($worlds));
  76. }
  77. function fortune()
  78. {
  79. DbRaw::$fortune->execute();
  80. $arr = DbRaw::$fortune->fetchAll(PDO::FETCH_KEY_PAIR);
  81. $arr[0] = 'Additional fortune added at request time.';
  82. asort($arr);
  83. $html = '';
  84. foreach ($arr as $id => $message) {
  85. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  86. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  87. }
  88. return new Response(200, [
  89. 'Date' => Header::$date
  90. ], "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>"
  91. );
  92. }
  93. /* function info()
  94. {
  95. ob_start();
  96. phpinfo();
  97. return new Response(200, ['Content-Type' => 'text/plain'], ob_get_clean());
  98. }
  99. */