main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #define QUIT_WAIT std::cout << "Please exit after entering any character...";std::cin.get();return -1
  34. void* start(const char* config_filepath)
  35. {
  36. return fastweb_start(config_filepath);
  37. }
  38. void close(void* app)
  39. {
  40. if(app == nullptr)
  41. fastweb_close(app);
  42. }
  43. void ouput_help()
  44. {
  45. std::cerr << "No configuration file path provided. Please provide the path to a config.ini file as an argument." << std::endl;
  46. std::cout << "------------------------------------------------------------------------------" << std::endl;
  47. std::cout << "| The instructions are as follows:" << std::endl;
  48. std::cout << "| 1、fastweb start config.ini\t\t [load the specified configuration and start Fastweb]" << std::endl;
  49. std::cout << "| 2、fastweb create config .\t\t [create a default configuration file]" << std::endl;
  50. std::cout << "| 3、fastweb create website .\t\t [create a default website directory]" << std::endl;
  51. std::cout << "------------------------------------------------------------------------------" << std::endl;
  52. }
  53. int main(int argc, char* argv[])
  54. {
  55. if (argc < 2) { ouput_help(); QUIT_WAIT; }
  56. std::string type = argv[1];
  57. if (type == "help")
  58. {
  59. ouput_help();
  60. QUIT_WAIT;
  61. }
  62. if (argc < 3){ouput_help();QUIT_WAIT;}
  63. void* app = nullptr;
  64. std::string value = argv[2];
  65. if (type == "start")
  66. {
  67. if (ylib::file::exist(std::filesystem::absolute(value).string()))
  68. {
  69. app = start(std::filesystem::absolute(value).string().c_str());
  70. if (app == nullptr)
  71. {
  72. QUIT_WAIT;
  73. }
  74. while (true) {
  75. std::string input;
  76. std::cin >> input;
  77. if (input == "quit" || input == "exit")
  78. {
  79. close(app);
  80. std::cout << "closed";
  81. return 0;
  82. }
  83. else
  84. {
  85. std::cout << "Enter \"quit\" or \"exit\" to exit the application" << std::endl;
  86. std::cout << "Enter \"restart\" to restart the application" << std::endl;
  87. }
  88. }
  89. }
  90. else
  91. {
  92. std::cerr << "file not found: " << std::filesystem::absolute(value).string() << std::endl;
  93. QUIT_WAIT;
  94. }
  95. }
  96. if (argc < 4) { ouput_help(); QUIT_WAIT; }
  97. if (type == "create")
  98. {
  99. std::string path = std::filesystem::absolute(argv[3]).string();
  100. if (value == "config")
  101. {
  102. ylib::file::copy("/usr/local/share/fastweb/config.ini",path);
  103. }
  104. else if (value == "website")
  105. {
  106. ylib::file::copy_dir("/usr/local/share/fastweb", path);
  107. }
  108. }
  109. return 0;
  110. }