app-pg.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $query_count = 1;
  43. $q = (int) $request->get('q');
  44. if ($q > 1) {
  45. $query_count = min($q, 500);
  46. }
  47. while ($query_count--) {
  48. DbRaw::$random->execute([mt_rand(1, 10000)]);
  49. $arr[] = DbRaw::$random->fetch();
  50. }
  51. return new Response(200, [
  52. 'Content-Type' => 'application/json',
  53. 'Date' => Header::$date
  54. ], json_encode($arr));
  55. }
  56. function updateraw($request)
  57. {
  58. $query_count = 1;
  59. $q = (int) $request->get('q');
  60. if ($q > 1) {
  61. $query_count = min($q, 500);
  62. }
  63. while ($query_count--) {
  64. DbRaw::$random->execute([mt_rand(1, 10000)]);
  65. $row = DbRaw::$random->fetch();
  66. $row['randomNumber'] = mt_rand(1, 10000);
  67. $worlds[] = $row;
  68. }
  69. DbRaw::update($worlds);
  70. return new Response(200, [
  71. 'Content-Type' => 'application/json',
  72. 'Date' => Header::$date
  73. ], json_encode($worlds));
  74. }
  75. function fortune()
  76. {
  77. DbRaw::$fortune->execute();
  78. $arr = DbRaw::$fortune->fetchAll(PDO::FETCH_KEY_PAIR);
  79. $arr[0] = 'Additional fortune added at request time.';
  80. asort($arr);
  81. $html = '';
  82. foreach ($arr as $id => $message) {
  83. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  84. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  85. }
  86. return new Response(200, [
  87. 'Date' => Header::$date
  88. ], "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>"
  89. );
  90. }
  91. /* function info()
  92. {
  93. ob_start();
  94. phpinfo();
  95. return new Response(200, ['Content-Type' => 'text/plain'], ob_get_clean());
  96. }
  97. */