IndexController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Simps.
  5. *
  6. * @link https://simps.io
  7. * @document https://doc.simps.io
  8. * @license https://github.com/simple-swoole/simps/blob/master/LICENSE
  9. */
  10. namespace App\Controller;
  11. use App\Model\DbModel;
  12. use Simps\Server\Protocol\HTTP\SimpleResponse;
  13. class IndexController
  14. {
  15. public function index($server, $fd)
  16. {
  17. $server->send(
  18. $fd,
  19. SimpleResponse::build(
  20. json_encode(['message' => 'Hello, World!']),
  21. 200,
  22. ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
  23. )
  24. );
  25. }
  26. public function plaintext($server, $fd)
  27. {
  28. $server->send(
  29. $fd,
  30. SimpleResponse::build(
  31. 'Hello, World!',
  32. 200,
  33. ['Content-Type' => 'text/plain', 'Date' => gmdate("D, d M Y H:i:s T")]
  34. )
  35. );
  36. }
  37. public function fortunes($server, $fd)
  38. {
  39. $db = new DbModel();
  40. $fortune = $db->fortunes();
  41. $html = '';
  42. foreach ($fortune as $id => $message) {
  43. $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  44. $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
  45. }
  46. $data = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'
  47. . $html .
  48. '</table></body></html>';
  49. $server->send(
  50. $fd,
  51. SimpleResponse::build(
  52. $data,
  53. 200,
  54. ['Content-Type' => 'text/html; charset=utf-8', 'Date' => gmdate("D, d M Y H:i:s T")]
  55. )
  56. );
  57. }
  58. public function db($server, $fd, $data)
  59. {
  60. $db = new DbModel();
  61. if (isset($data['queries'])) {
  62. $res = $db->db((int)$data['queries']);
  63. } else {
  64. $res = $db->db(-1);
  65. }
  66. $server->send(
  67. $fd,
  68. SimpleResponse::build(
  69. $res,
  70. 200,
  71. ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
  72. )
  73. );
  74. }
  75. public function queries($server, $fd, $data)
  76. {
  77. $db = new DbModel();
  78. if (isset($data['queries'])) {
  79. $res = $db->db((int)$data['queries']);
  80. } else {
  81. $res = $db->db();
  82. }
  83. $server->send(
  84. $fd,
  85. SimpleResponse::build(
  86. $res,
  87. 200,
  88. ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
  89. )
  90. );
  91. }
  92. public function updates($server, $fd, $data)
  93. {
  94. $db = new DbModel();
  95. if (isset($data['queries'])) {
  96. $res = $db->updates((int)$data['queries']);
  97. } else {
  98. $res = $db->updates(-1);
  99. }
  100. $server->send(
  101. $fd,
  102. SimpleResponse::build(
  103. $res,
  104. 200,
  105. ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
  106. )
  107. );
  108. }
  109. public function microDb($server, $fd)
  110. {
  111. $db = new DbModel();
  112. $res = $db->microDb();
  113. $server->send(
  114. $fd,
  115. SimpleResponse::build(
  116. $res,
  117. 200,
  118. ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
  119. )
  120. );
  121. }
  122. public function microQueries($server, $fd, $data)
  123. {
  124. $db = new DbModel();
  125. if (isset($data['queries'])) {
  126. $res = $db->microQueries((int)$data['queries']);
  127. } else {
  128. $res = $db->microQueries();
  129. }
  130. $server->send(
  131. $fd,
  132. SimpleResponse::build(
  133. $res,
  134. 200,
  135. ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
  136. )
  137. );
  138. }
  139. public function microUpdates($server, $fd, $data)
  140. {
  141. $db = new DbModel();
  142. if (isset($data['queries'])) {
  143. $res = $db->microUpdates((int)$data['queries']);
  144. } else {
  145. $res = $db->microUpdates();
  146. }
  147. $server->send(
  148. $fd,
  149. SimpleResponse::build(
  150. $res,
  151. 200,
  152. ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
  153. )
  154. );
  155. }
  156. }