IndexController.php 4.4 KB

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