fastweb.cpp 2.9 KB

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