StaticWebPageController.cpp 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "../processor/processor.h"
  5. #include "../webuiconf.h"
  6. #include "../processor/templates/framework7/Framework7Document.hpp"
  7. #include <sys/stat.h>
  8. using namespace std;
  9. using namespace xc::processor;
  10. using namespace xc::processor::templates;
  11. using namespace configor;
  12. namespace xc::controller {
  13. ContentGeneratorDefine({
  14. if (request.getMethod() != "GET") { return false; }
  15. if (request.getURL().length() == 0) { return false; }
  16. struct stat buffer;
  17. string filePath = "html";
  18. if (request.getURL()[0] == '/') {
  19. filePath += request.getURL();
  20. } else {
  21. filePath += "/" + request.getURL();
  22. }
  23. if (stat(filePath.c_str(), &buffer) == 0) {
  24. if (S_ISREG(buffer.st_mode)) {
  25. return true;
  26. } else {
  27. for (auto file: conf::defaultFiles) {
  28. string newFilePath = filePath;
  29. if (filePath[filePath.length() - 1] == '/') {
  30. newFilePath += file;
  31. } else {
  32. newFilePath += "/" + file;
  33. }
  34. if (stat(newFilePath.c_str(), &buffer) == 0) {
  35. return S_ISREG(buffer.st_mode);
  36. }
  37. }
  38. }
  39. }
  40. return false;
  41. }, {
  42. struct stat buffer;
  43. string filePath = "html";
  44. if (request.getURL()[0] == '/') {
  45. filePath += request.getURL();
  46. } else {
  47. filePath += "/" + request.getURL();
  48. }
  49. if (stat(filePath.c_str(), &buffer) == 0) {
  50. if (S_ISREG(buffer.st_mode)) {
  51. return (ResponseData *) new BinaryResponseData(200, filePath,
  52. mimeTypeOfFile(filePath));
  53. } else {
  54. for (auto file: conf::defaultFiles) {
  55. string newFilePath = filePath;
  56. if (filePath[filePath.length() - 1] == '/') {
  57. newFilePath += file;
  58. } else {
  59. newFilePath += "/" + file;
  60. }
  61. if (stat(newFilePath.c_str(), &buffer) == 0) {
  62. if (S_ISREG(buffer.st_mode)) {
  63. return (ResponseData *) new BinaryResponseData(200, newFilePath,
  64. mimeTypeOfFile(
  65. newFilePath));
  66. }
  67. }
  68. }
  69. }
  70. }
  71. return (ResponseData *) new FileResponseData(conf::errorPage404);
  72. })
  73. ResponseData *test1(RequestData request) {
  74. return new TemplateResponseData({
  75. framework7::Framework7Document()
  76. });
  77. }
  78. ContentGeneratorDefineS(request.getURL() == "/test1",
  79. test1(request))
  80. ContentGeneratorDefineS(request.getURL() == "/test2",
  81. new TextResponseData(200, "test2 controller response"))
  82. }