main.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include <iostream>
  3. #include "core/entry.h"
  4. #include <filesystem>
  5. #include "util/file.h"
  6. #include "util/strutils.h"
  7. #define QUIT_WAIT std::cout << "Please exit after entering any character...";std::cin.get();return -1
  8. void* start(const char* config_filepath)
  9. {
  10. return fastweb_start(config_filepath);
  11. }
  12. void close(void* app)
  13. {
  14. if(app == nullptr)
  15. fastweb_close(app);
  16. }
  17. void ouput_help()
  18. {
  19. std::cerr << "No configuration file path provided. Please provide the path to a config.ini file as an argument." << std::endl;
  20. std::cout << "------------------------------------------------------------------------------" << std::endl;
  21. std::cout << "| The instructions are as follows:" << std::endl;
  22. std::cout << "| 1、fastweb start config.ini\t\t [load the specified configuration and start Fastweb]" << std::endl;
  23. std::cout << "| 2、fastweb create config .\t\t [create a default configuration file]" << std::endl;
  24. std::cout << "| 3、fastweb create website .\t\t [create a default website directory]" << std::endl;
  25. std::cout << "------------------------------------------------------------------------------" << std::endl;
  26. }
  27. int main(int argc, char* argv[])
  28. {
  29. if (argc < 2) { ouput_help(); QUIT_WAIT; }
  30. std::string type = argv[1];
  31. if (type == "help")
  32. {
  33. ouput_help();
  34. QUIT_WAIT;
  35. }
  36. if (argc < 3){ouput_help();QUIT_WAIT;}
  37. void* app = nullptr;
  38. std::string value = argv[2];
  39. if (type == "start")
  40. {
  41. if (ylib::file::exist(std::filesystem::absolute(value).string()))
  42. {
  43. app = start(std::filesystem::absolute(value).string().c_str());
  44. if (app == nullptr)
  45. {
  46. QUIT_WAIT;
  47. }
  48. while (true) {
  49. std::string input;
  50. std::cin >> input;
  51. if (input == "quit" || input == "exit")
  52. {
  53. close(app);
  54. std::cout << "closed";
  55. return 0;
  56. }
  57. else
  58. {
  59. std::cout << "Enter \"quit\" or \"exit\" to exit the application" << std::endl;
  60. std::cout << "Enter \"restart\" to restart the application" << std::endl;
  61. }
  62. }
  63. }
  64. else
  65. {
  66. std::cerr << "file not found: " << std::filesystem::absolute(value).string() << std::endl;
  67. QUIT_WAIT;
  68. }
  69. }
  70. if (argc < 4) { ouput_help(); QUIT_WAIT; }
  71. if (type == "create")
  72. {
  73. std::string path = std::filesystem::absolute(argv[3]).string();
  74. if (value == "config")
  75. {
  76. ylib::file::copy("/usr/local/share/fastweb/config.ini",path);
  77. }
  78. else if (value == "website")
  79. {
  80. ylib::file::copy_dir("/usr/local/share/fastweb", path);
  81. }
  82. }
  83. return 0;
  84. }