fastweb.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "fastweb.h"
  2. #include <regex>
  3. #include "net/http_website.h"
  4. #include "net/http_router.h"
  5. #include "net/http_interceptor.h"
  6. #include "net/http_reqpack.h"
  7. #include "module/http/request.h"
  8. #include "module/http/response.h"
  9. #include "module/globalfuns.h"
  10. #include "core/config.h"
  11. #include "core/global.h"
  12. #include "core/statemanager.h"
  13. #include "core/subscribemanager.h"
  14. #include "core/interceptormanager.h"
  15. fastweb::fastweb()
  16. {
  17. }
  18. bool fastweb::start()
  19. {
  20. stop();
  21. // 虚拟机管理器
  22. if(sStateMgr->start() == false)
  23. {
  24. m_lastErrorDesc = sStateMgr->last_error();
  25. return false;
  26. }
  27. m_center = new network::http::center();
  28. network::http::start_config config;
  29. network::http::website_config ws_config;
  30. for_iter(iter, sConfig->domain)
  31. {
  32. network::http::host_config host_config;
  33. host_config.domain = iter->first;
  34. host_config.port = iter->second.port;
  35. host_config.ssl = iter->second.https;
  36. ws_config.host.push_back(host_config);
  37. std::string url;
  38. if (host_config.ssl)
  39. url = "https://";
  40. else
  41. url = "http://";
  42. url += host_config.domain;
  43. url += ":" + std::to_string(host_config.port);
  44. LOG_WARN("URL: "+url);
  45. }
  46. ws_config.name = "master";
  47. ws_config.router.threadpool.size = 40;
  48. ws_config.router.threadpool.queuemax = 10000;
  49. ws_config.session.dirpath = sConfig->website.session_dir;
  50. ws_config.session.timeout_sec = sConfig->website.session_timeout_sec;
  51. config.website.push_back(ws_config);
  52. for_iter(iter, sConfig->domain)
  53. {
  54. if(iter->second.https)
  55. config.cert.emplace(iter->first, iter->second.ssl);
  56. }
  57. if (!m_center->create(config))
  58. {
  59. m_lastErrorDesc = "start failed," + m_center->last_error();
  60. stop();
  61. return false;
  62. }
  63. auto website = m_center->website_byname(ws_config.name);
  64. auto router = website->router();
  65. // 初始化脚本
  66. if (initialization_script() == false)
  67. return false;
  68. // 加载订阅
  69. m_subscribe.load(router);
  70. // 加载拦截器
  71. m_interceptor.load(router);
  72. return m_center->start();
  73. }
  74. void fastweb::stop()
  75. {
  76. m_subscribe.clear();
  77. m_interceptor.clear();
  78. if (m_center != nullptr)
  79. {
  80. m_center->close();
  81. delete m_center;
  82. }
  83. m_center = nullptr;
  84. global::getInstance()->clear();
  85. if (m_state_init != nullptr)
  86. delete m_state_init;
  87. m_state_init = nullptr;
  88. sStateMgr->close();
  89. }
  90. bool fastweb::initialization_script()
  91. {
  92. auto script_filepath = sConfig->website.Initialization_script;
  93. if (script_filepath == "")
  94. return true;
  95. if (ylib::file::exist(script_filepath) == false)
  96. {
  97. m_lastErrorDesc = "Initialization script not found, filepath: " + script_filepath;
  98. return false;
  99. }
  100. auto state = sStateMgr->get();
  101. try
  102. {
  103. state->state->set_function("global_regist", module::global_regist);
  104. auto result = state->state->script_file(script_filepath);
  105. if (!result.valid()) {
  106. sol::error err = result;
  107. throw ylib::exception(err.what());
  108. }
  109. if (result.get<bool>() == false)
  110. {
  111. throw ylib::exception("user interrupt");
  112. }
  113. }
  114. catch (const std::exception& e)
  115. {
  116. m_lastErrorDesc = "Exception in executing initialization script: " + std::string(e.what());
  117. }
  118. // 不可DELETE,否则注册的全局变量会被自动销毁
  119. //delete state;
  120. m_state_init = state;
  121. return m_lastErrorDesc == "";
  122. }