main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 
  2. // MIT License
  3. // Copyright(c) 2024 FastWeb - fwlua.com - nianhua
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files(the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions :
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  15. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. // SOFTWARE.
  19. // ## Additional Terms for Commercial Use
  20. // This software is licensed for personal, educational, and non - commercial use.
  21. // For commercial use or use within a company, organization, or institution, a
  22. // separate commercial license is required.To obtain a commercial license,
  23. // please contact
  24. // EMail:1585346868@qq.com
  25. // Mobile:17367918735
  26. // QQ:1585346868
  27. #pragma once
  28. #include <iostream>
  29. #include "core/entry.h"
  30. #include <filesystem>
  31. #include "util/file.h"
  32. #include "util/strutils.h"
  33. #include "util/system.h"
  34. #include "slave.h"
  35. #define QUIT_WAIT std::cout << "Please exit after entering any character...";std::cin.get();return -1
  36. void* start(const char* config_filepath)
  37. {
  38. return fastweb_start(config_filepath);
  39. }
  40. void close(void* app)
  41. {
  42. if(app == nullptr)
  43. fastweb_close(app);
  44. }
  45. void ouput_help()
  46. {
  47. std::cerr << "No configuration file path provided. Please provide the path to a config.ini file as an argument." << std::endl;
  48. std::cout << "------------------------------------------------------------------------------" << std::endl;
  49. std::cout << "| The instructions are as follows:" << std::endl;
  50. std::cout << "| 1、fastweb start config.ini\t\t [load the specified configuration and start Fastweb]" << std::endl;
  51. std::cout << "| 2、fastweb create config .\t\t [create a default configuration file]" << std::endl;
  52. std::cout << "| 3、fastweb create website .\t\t [create a default website directory]" << std::endl;
  53. std::cout << "------------------------------------------------------------------------------" << std::endl;
  54. }
  55. int main(int argc, char* argv[])
  56. {
  57. if (argc < 2) { ouput_help(); QUIT_WAIT; }
  58. std::string type = argv[1];
  59. if (type == "help")
  60. {
  61. ouput_help();
  62. QUIT_WAIT;
  63. }
  64. if (argc < 3){ouput_help();QUIT_WAIT;}
  65. void* app = nullptr;
  66. std::string value = argv[2];
  67. if (type == "start")
  68. {
  69. if (ylib::file::exist(std::filesystem::absolute(value).string()))
  70. {
  71. app = start(std::filesystem::absolute(value).string().c_str());
  72. if (app == nullptr)
  73. {
  74. QUIT_WAIT;
  75. }
  76. while (true) {
  77. std::string input;
  78. std::cin >> input;
  79. if (input == "quit" || input == "exit")
  80. {
  81. close(app);
  82. std::cout << "closed";
  83. return 0;
  84. }
  85. else
  86. {
  87. std::cout << "Enter \"quit\" or \"exit\" to exit the application" << std::endl;
  88. std::cout << "Enter \"restart\" to restart the application" << std::endl;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. std::cerr << "file not found: " << std::filesystem::absolute(value).string() << std::endl;
  95. QUIT_WAIT;
  96. }
  97. }
  98. else if (type == "start2")
  99. {
  100. if (ylib::file::exist(std::filesystem::absolute(value).string()))
  101. {
  102. app = start(std::filesystem::absolute(value).string().c_str());
  103. if (app == nullptr)
  104. return -1;
  105. while (true)
  106. system::sleep_msec(1000 * 60);
  107. }
  108. else
  109. return -1;
  110. }
  111. else if (type == "fastwebmanager")
  112. {
  113. int flag = ylib::stoi(argv[2]);
  114. slave s("fastweb_shardmemory", flag);
  115. app = start(argv[3]);
  116. if (app == nullptr)
  117. {
  118. s.write(1);
  119. return -1;
  120. }
  121. s.write(2);
  122. // 等待关闭信号
  123. s.wait();
  124. return 0;
  125. }
  126. if (argc < 4) { ouput_help(); QUIT_WAIT; }
  127. if (type == "create")
  128. {
  129. std::string path = std::filesystem::absolute(argv[3]).string();
  130. if (value == "config")
  131. {
  132. ylib::file::copy("/usr/local/share/fastweb/config.ini",path);
  133. }
  134. else if (value == "website")
  135. {
  136. ylib::file::copy_dir("/usr/local/share/fastweb", path);
  137. }
  138. }
  139. return -1;
  140. }