main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // QQ:1585346868
  26. #pragma once
  27. #include <iostream>
  28. #include "core/entry.h"
  29. #include <filesystem>
  30. #include "util/file.h"
  31. #include "util/strutils.h"
  32. #include "util/system.h"
  33. #include "slave.h"
  34. #define QUIT_WAIT std::cout << "Please exit after entering any character...";std::cin.get();return -1
  35. void* start(const char* config_filepath)
  36. {
  37. return fastweb_start(config_filepath);
  38. }
  39. void close(void* app)
  40. {
  41. if(app == nullptr)
  42. fastweb_close(app);
  43. }
  44. void ouput_help()
  45. {
  46. std::cerr << "No configuration file path provided. Please provide the path to a config.ini file as an argument." << std::endl;
  47. std::cout << "------------------------------------------------------------------------------" << std::endl;
  48. std::cout << "| The instructions are as follows:" << std::endl;
  49. std::cout << "| 1、fastweb start config.ini\t\t [load the specified configuration and start Fastweb]" << std::endl;
  50. std::cout << "| 2、fastweb create config .\t\t [create a default configuration file]" << std::endl;
  51. std::cout << "| 3、fastweb create website .\t\t [create a default website directory]" << std::endl;
  52. std::cout << "------------------------------------------------------------------------------" << std::endl;
  53. }
  54. int main(int argc, char* argv[])
  55. {
  56. std::cout << "|------------------------[FASTWEB]----------------------------" << std::endl;
  57. std::cout << "| WorkingDirectory:\t"<< strutils::replace(system::current_dir(),'\\','/') << std::endl;
  58. std::cout << "| Args" << std::endl;
  59. for (int i = 0; i < argc; i++)
  60. {
  61. std::cout << "|\t" << argv[i] << std::endl;
  62. }
  63. std::cout << "|----------------------------------------------------------------" << std::endl;
  64. if (argc < 2) { ouput_help(); QUIT_WAIT; }
  65. std::string type = argv[1];
  66. if (type == "help")
  67. {
  68. ouput_help();
  69. QUIT_WAIT;
  70. }
  71. if (argc < 3){ouput_help();QUIT_WAIT;}
  72. void* app = nullptr;
  73. std::string value = argv[2];
  74. if (type == "start")
  75. {
  76. if (ylib::file::exist(std::filesystem::absolute(value).string()))
  77. {
  78. app = start(std::filesystem::absolute(value).string().c_str());
  79. if (app == nullptr)
  80. {
  81. QUIT_WAIT;
  82. }
  83. while (true) {
  84. std::string input;
  85. std::cin >> input;
  86. if (input == "quit" || input == "exit")
  87. {
  88. close(app);
  89. std::cout << "closed";
  90. return 0;
  91. }
  92. else
  93. {
  94. std::cout << "Enter \"quit\" or \"exit\" to exit the application" << std::endl;
  95. std::cout << "Enter \"restart\" to restart the application" << std::endl;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. std::cerr << "file not found: " << std::filesystem::absolute(value).string() << std::endl;
  102. QUIT_WAIT;
  103. }
  104. }
  105. else if (type == "start2")
  106. {
  107. if (ylib::file::exist(std::filesystem::absolute(value).string()))
  108. {
  109. app = start(std::filesystem::absolute(value).string().c_str());
  110. if (app == nullptr)
  111. return -1;
  112. while (true)
  113. system::sleep_msec(1000 * 60);
  114. }
  115. else
  116. return -1;
  117. }
  118. else if (type == "fastwebmanager")
  119. {
  120. int flag = ylib::stoi(argv[2]);
  121. slave s("fastweb_shardmemory", flag);
  122. app = start(argv[3]);
  123. if (app == nullptr)
  124. {
  125. s.write(1);
  126. return -1;
  127. }
  128. s.write(2);
  129. // 等待关闭信号
  130. s.wait();
  131. return 0;
  132. }
  133. if (argc < 4) { ouput_help(); QUIT_WAIT; }
  134. if (type == "create")
  135. {
  136. std::string path = std::filesystem::absolute(argv[3]).string();
  137. if (value == "config")
  138. {
  139. ylib::file::copy("/usr/local/share/fastweb/config.ini",path);
  140. }
  141. else if (value == "website")
  142. {
  143. ylib::file::copy_dir("/usr/local/share/fastweb", path);
  144. }
  145. }
  146. return -1;
  147. }