StaticWebPageController.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "../processor/processor.h"
  5. #include "../webuiconf.h"
  6. #include <sys/stat.h>
  7. using namespace std;
  8. using namespace xc::processor;
  9. using namespace xc::processor::templates;
  10. using namespace configor;
  11. namespace xc::controller {
  12. ContentGeneratorDefine({
  13. if (request.getMethod() != "GET") { return false; }
  14. if (request.getURL().length() == 0) { return false; }
  15. struct stat buffer;
  16. string filePath = "html";
  17. if (request.getURL()[0] == '/') {
  18. filePath += request.getURL();
  19. } else {
  20. filePath += "/" + request.getURL();
  21. }
  22. if (stat(filePath.c_str(), &buffer) == 0) {
  23. if (S_ISREG(buffer.st_mode)) {
  24. return true;
  25. } else {
  26. for (auto file: conf::defaultFiles) {
  27. string newFilePath = filePath;
  28. if (filePath[filePath.length() - 1] == '/') {
  29. newFilePath += file;
  30. } else {
  31. newFilePath += "/" + file;
  32. }
  33. if (stat(newFilePath.c_str(), &buffer) == 0) {
  34. return S_ISREG(buffer.st_mode);
  35. }
  36. }
  37. }
  38. }
  39. return false;
  40. }, {
  41. struct stat buffer;
  42. string filePath = "html";
  43. if (request.getURL()[0] == '/') {
  44. filePath += request.getURL();
  45. } else {
  46. filePath += "/" + request.getURL();
  47. }
  48. if (stat(filePath.c_str(), &buffer) == 0) {
  49. if (S_ISREG(buffer.st_mode)) {
  50. return (ResponseData *) new BinaryResponseData(200, filePath,
  51. mimeTypeOfFile(filePath));
  52. } else {
  53. for (auto file: conf::defaultFiles) {
  54. string newFilePath = filePath;
  55. if (filePath[filePath.length() - 1] == '/') {
  56. newFilePath += file;
  57. } else {
  58. newFilePath += "/" + file;
  59. }
  60. if (stat(newFilePath.c_str(), &buffer) == 0) {
  61. if (S_ISREG(buffer.st_mode)) {
  62. return (ResponseData *) new BinaryResponseData(200, newFilePath,
  63. mimeTypeOfFile(
  64. newFilePath));
  65. }
  66. }
  67. }
  68. }
  69. }
  70. return (ResponseData *) new FileResponseData(conf::errorPage404);
  71. })
  72. ResponseData *test1(RequestData request) {
  73. vector<map<string, string>> model = {
  74. {
  75. { "name", "user1" },
  76. { "pwd", "123456" }
  77. },
  78. {
  79. { "name", "user2" },
  80. { "pwd", "234567" }
  81. }
  82. };
  83. return new TemplateResponseData({
  84. p("Hello world"),
  85. Foreach(model, [](map<string, string> it) {
  86. return p({
  87. "Hello ",
  88. b(it["name"]),
  89. ", You password is ",
  90. it["pwd"]
  91. }).style("text-align", "center");
  92. })
  93. });
  94. }
  95. ContentGeneratorDefineS(request.getURL() == "/test1",
  96. test1(request))
  97. ContentGeneratorDefineS(request.getURL() == "/test2",
  98. new TextResponseData(200, "test2 controller response"))
  99. }