subscribemanager.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "subscribemanager.h"
  2. #include "core/config.h"
  3. #include "core/statemanager.h"
  4. #include "module/http/request.h"
  5. #include "module/http/response.h"
  6. subscribe_manager::subscribe_manager()
  7. {
  8. }
  9. subscribe_manager::~subscribe_manager()
  10. {
  11. clear();
  12. }
  13. void subscribe_manager::load(network::http::router* router)
  14. {
  15. clear();
  16. m_router = router;
  17. // 初始化全部订阅
  18. auto files = sConfig->lua_app_files();
  19. for (size_t i = 0; i < files.size(); i++)
  20. {
  21. init_subscribe(files[i]);
  22. }
  23. // 其它绑定
  24. router->other(&subscribe_manager::other);
  25. }
  26. void subscribe_manager::clear()
  27. {
  28. if (m_router != nullptr)
  29. {
  30. m_router->clear_subscribe();
  31. }
  32. for (size_t i = 0; i < m_subextra.size(); i++)
  33. delete m_subextra[i];
  34. m_subextra.clear();
  35. m_router = nullptr;
  36. }
  37. void subscribe_manager::init_subscribe(const std::string& filepath)
  38. {
  39. auto state = sStateMgr->get();
  40. std::string route_pattern;
  41. network::http::method method = network::http::ALL;
  42. try
  43. {
  44. auto result = state->state->script_file(sConfig->scripts.app_dir + "/" + filepath);
  45. if (result.valid()) {
  46. auto router = (*state->state)["route"];
  47. auto type = router.get_type();
  48. if (router.is<sol::table>())
  49. {
  50. sol::optional<std::string> route_pattern_param = router[1];
  51. sol::optional<int> method_param = router[2];
  52. if (route_pattern_param && route_pattern_param->empty() == false)
  53. route_pattern = *route_pattern_param;
  54. if (method_param)
  55. method = (network::http::method)*method_param;
  56. }
  57. }
  58. }
  59. catch (const std::exception& e)
  60. {
  61. LOG_ERROR(e.what());
  62. }
  63. sStateMgr->push(state);
  64. if (route_pattern.empty())
  65. route_pattern = sConfig->scripts.app_mapping_dir + filepath;
  66. // OutPutLog
  67. {
  68. std::string log;
  69. log = "[subscribe] lua: " + filepath + "\t pattern: " + route_pattern + "\t method: ";
  70. switch (method)
  71. {
  72. case ylib::network::http::GET:
  73. log.append("GET");
  74. break;
  75. case ylib::network::http::POST:
  76. log.append("POST");
  77. break;
  78. case ylib::network::http::PUT:
  79. log.append("PUT");
  80. break;
  81. case ylib::network::http::DEL:
  82. log.append("DEL");
  83. break;
  84. case ylib::network::http::HEAD:
  85. log.append("HEAD");
  86. break;
  87. case ylib::network::http::ALL:
  88. log.append("ALL");
  89. break;
  90. default:
  91. break;
  92. }
  93. LOG_INFO(log);
  94. }
  95. std::string* extra = new std::string(sConfig->scripts.app_dir + "/" + filepath);
  96. m_subextra.push_back(extra);
  97. m_router->subscribe(route_pattern, method, &subscribe_manager::callback, extra);
  98. }
  99. void subscribe_manager::callback(network::http::request* request, network::http::response* response, void* extra)
  100. {
  101. std::string lua_filepath = *(std::string*)extra;
  102. auto lua = sStateMgr->get();
  103. std::string exception_string;
  104. try
  105. {
  106. auto lbResult = lua->state->load_file(lua_filepath);
  107. if (lbResult.valid() == false)
  108. {
  109. sol::error err = lbResult;
  110. throw ylib::exception("Failed to load bytecode, " + std::string(err.what()));
  111. }
  112. module::request m_request(request);
  113. module::response m_response(response);
  114. lbResult();
  115. (*lua->state)["response"] = &m_response;
  116. (*lua->state)["request"] = &m_request;
  117. auto result = (*lua->state)["access"]();
  118. if (!result.valid()) {
  119. sol::error err = result;
  120. throw ylib::exception(err.what());
  121. }
  122. }
  123. catch (const std::exception& e)
  124. {
  125. exception_string = e.what();
  126. if (sConfig->website.debug)
  127. LOG_ERROR("[subscribe_service][" + request->filepath() + "]: " + e.what());
  128. }
  129. // 清理
  130. lua->state->collect_garbage();
  131. sStateMgr->push(lua);
  132. if (exception_string.empty() == false)
  133. throw ylib::exception(exception_string);
  134. }
  135. void subscribe_manager::other(network::http::request* request, network::http::response* response)
  136. {
  137. auto send_404 = [](network::http::response* response) {
  138. std::string default_404 = sConfig->website.static_dir + "\\" + sConfig->website.default_404;
  139. if (sConfig->website.default_404 == "" || ylib::file::exist(default_404) == false)
  140. {
  141. response->send((std::string)"404 Not Found", 404, "Not Found");
  142. }
  143. else
  144. {
  145. response->send_file(default_404, -1, 404, "Not Found");
  146. }
  147. };
  148. auto send_file = [&](network::http::response* response, std::string filepath)
  149. {
  150. filepath = sConfig->website.static_dir + filepath;
  151. if (ylib::file::exist(filepath))
  152. {
  153. response->send_file(filepath);
  154. }
  155. else
  156. {
  157. send_404(response);
  158. }
  159. };
  160. if (request->filepath() == "/")
  161. {
  162. bool find = false;
  163. for (size_t i = 0; i < sConfig->website.default_index.size(); i++)
  164. {
  165. std::string filepath = sConfig->website.static_dir + request->filepath() + sConfig->website.default_index[i];
  166. if (ylib::file::exist(filepath))
  167. {
  168. find = true;
  169. response->send_file(filepath);
  170. break;
  171. }
  172. }
  173. if (find == false)
  174. {
  175. send_404(response);
  176. }
  177. return;
  178. }
  179. send_file(response, request->filepath());
  180. }