From 153f087fc2a88c1c89f32c904d8aa746682e5e9f Mon Sep 17 00:00:00 2001 From: xx Date: Sat, 8 Jun 2024 18:59:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9linux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 68 ++++++++++++++++++------ src/core/define.h | 4 +- src/module/filesystem.cpp | 30 ++++++----- tests/main.cpp | 108 +++++++++++++++++++++++++------------- 4 files changed, 143 insertions(+), 67 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f71b86..cdf3334 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,7 @@ if(MSVC) ) else() + set(CMAKE_INSTALL_RPATH "/usr/local/lib") link_directories(/usr/lib/x86_64-linux-gnu) link_directories(/usr/local/lib) target_link_libraries(${FASTWEBCORE} @@ -107,30 +108,67 @@ endif() # 编译测试调用示例 add_executable(${PROJECT_NAME} tests/main.cpp) -target_link_libraries(${PROJECT_NAME} ${FASTWEBCORE}) +if(MSVC) + target_link_libraries(${PROJECT_NAME} PRIVATE + ${FASTWEBCORE} + odbc32.lib + User32.lib + Advapi32.lib + IPHLPAPI.lib + WS2_32.lib + Shell32.lib + ${YLIB}/lib/libcrypto_static_win64.lib + $<$:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket_D.lib> + $<$:${YLIB}/lib/ylib_d.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket.lib> + $<$:${YLIB}/lib/ylib.lib> + ) +else() + target_link_libraries(${PROJECT_NAME} + ${FASTWEBCORE} + hpsocket + ylib + pthread + ) +endif() + set_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}") ######################## 安装 ######################## -install(TARGETS ${FASTWEBCORE} DESTINATION $,bin/debug,bin/release>) -install(TARGETS ${PROJECT_NAME} DESTINATION $,bin/debug,bin/release>) if(MSVC) + install(TARGETS ${FASTWEBCORE} DESTINATION $,bin/debug,bin/release>) + install(TARGETS ${PROJECT_NAME} DESTINATION $,bin/debug,bin/release>) install(FILES ${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket_D.dll DESTINATION bin/debug) install(FILES ${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket.dll DESTINATION bin/release) + + install(FILES config.ini DESTINATION bin/debug) + install(FILES config.ini DESTINATION bin/release) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/www DESTINATION bin/debug) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/config DESTINATION bin/debug) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/www DESTINATION bin/release) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/config DESTINATION bin/release) + install(FILES ${PROJECT_SOURCE_DIR}/src/core/entry.h DESTINATION include) + install(FILES ${PROJECT_SOURCE_DIR}/module/dll_interface.h DESTINATION include) + install(FILES ${PROJECT_SOURCE_DIR}/src/module/basemodule.h DESTINATION include) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty/sol DESTINATION include) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty/lua/include/ DESTINATION include/lua) +else() + + install(TARGETS ${FASTWEBCORE} DESTINATION lib) + install(TARGETS ${PROJECT_NAME} DESTINATION bin) + + install(FILES ${PROJECT_SOURCE_DIR}/src/core/entry.h DESTINATION include/${PROJECT_NAME}) + install(FILES ${PROJECT_SOURCE_DIR}/module/dll_interface.h DESTINATION include/${PROJECT_NAME}) + install(FILES ${PROJECT_SOURCE_DIR}/src/module/basemodule.h DESTINATION include/${PROJECT_NAME}) + + + install(FILES config.ini DESTINATION share/${PROJECT_NAME}) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/www DESTINATION share/${PROJECT_NAME}) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/config DESTINATION share/${PROJECT_NAME}) + endif() -install(FILES config.ini DESTINATION bin/debug) -install(FILES config.ini DESTINATION bin/release) -install(DIRECTORY ${PROJECT_SOURCE_DIR}/www DESTINATION bin/debug) -install(DIRECTORY ${PROJECT_SOURCE_DIR}/config DESTINATION bin/debug) -install(DIRECTORY ${PROJECT_SOURCE_DIR}/www DESTINATION bin/release) -install(DIRECTORY ${PROJECT_SOURCE_DIR}/config DESTINATION bin/release) -install(FILES ${PROJECT_SOURCE_DIR}/src/core/entry.h DESTINATION include) -install(FILES ${PROJECT_SOURCE_DIR}/module/dll_interface.h DESTINATION include) -install(FILES ${PROJECT_SOURCE_DIR}/src/module/basemodule.h DESTINATION include) - -install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty/sol DESTINATION include) -install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty/lua/include/ DESTINATION include/lua) diff --git a/src/core/define.h b/src/core/define.h index 3717b23..7b8ceb1 100644 --- a/src/core/define.h +++ b/src/core/define.h @@ -19,6 +19,4 @@ -#define GET_APP \ - sol::state_view lua(ts); \ - fastweb::app* app = lua["____app"] \ No newline at end of file +#define GET_APP sol::state_view lua(ts);fastweb::app* app = lua["____app"] \ No newline at end of file diff --git a/src/module/filesystem.cpp b/src/module/filesystem.cpp index 50897b6..5de700c 100644 --- a/src/module/filesystem.cpp +++ b/src/module/filesystem.cpp @@ -23,14 +23,14 @@ void module::filesystem::copy_dir(const std::string& src_dir, const std::string& } bool module::filesystem::create_dir(const std::string& dirpath) { - return ylib::file::create_dir(dirpath,true); + return ylib::file::create_dir(dirpath, true); } sol::object module::filesystem::read(const std::string& filepath, sol::this_state s) { sol::object result; ylib::buffer data; if (ylib::file::read(filepath, data) == false) - result = sol::make_object(s,data.to_string()); + result = sol::make_object(s, data.to_string()); else result = sol::make_object(s, sol::nil); return result; @@ -45,7 +45,11 @@ bool module::filesystem::remove(const std::string& filepath) } bool module::filesystem::remove_dir(const std::string& dirpath, bool recycle) { - return ylib::file::remove_dir(dirpath,recycle); +#ifdef _WIN32 + return ylib::file::remove_dir(dirpath, recycle); +#else + return ylib::file::remove_dir(dirpath); +#endif } bool module::filesystem::exist(const std::string& filepath) { @@ -61,7 +65,7 @@ int64 module::filesystem::size(const std::string& filepath) } bool module::filesystem::copy(const std::string& src, const std::string& dst) { - return ylib::file::copy(src,dst); + return ylib::file::copy(src, dst); } timestamp module::filesystem::last_write_time(const std::string& filepath) { @@ -77,14 +81,14 @@ void module::filesystem::regist(sol::state* lua) "list", &module::filesystem::list, "copy_dir", &module::filesystem::copy_dir, "create_dir", &module::filesystem::create_dir, - "read",module::filesystem::read, - "write",module::filesystem::write, - "remove",module::filesystem::remove, - "remove_dir",module::filesystem::remove_dir, - "exist",module::filesystem::exist, - "exist_dir",module::filesystem::exist_dir, - "size",module::filesystem::size, - "copy",module::filesystem::copy, - "last_write_time",module::filesystem::last_write_time + "read", module::filesystem::read, + "write", module::filesystem::write, + "remove", module::filesystem::remove, + "remove_dir", module::filesystem::remove_dir, + "exist", module::filesystem::exist, + "exist_dir", module::filesystem::exist_dir, + "size", module::filesystem::size, + "copy", module::filesystem::copy, + "last_write_time", module::filesystem::last_write_time ); } diff --git a/tests/main.cpp b/tests/main.cpp index 5eb1013..3693c61 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -2,52 +2,88 @@ #include #include "core/entry.h" #include -std::string config_filepath; -void* fastweb_app_ptr = nullptr; -void start() -{ - fastweb_app_ptr = fastweb_start(config_filepath.c_str()); -} -void close() -{ - if(fastweb_app_ptr == nullptr) - fastweb_close(fastweb_app_ptr); - fastweb_app_ptr = nullptr; -} -int main() -{ - config_filepath = std::filesystem::current_path().string() + "/config.ini"; +#include "util/file.h" +#include "util/strutils.h" +#define QUIT_WAIT std::cout << "Please exit after entering any character...";std::cin.get();return -1 - std::string input = "restart"; - while (true) +void* start(const char* config_filepath) +{ + return fastweb_start(config_filepath); +} +void close(void* app) +{ + if(app == nullptr) + fastweb_close(app); +} +void ouput_help() +{ + std::cerr << "No configuration file path provided. Please provide the path to a config.ini file as an argument." << std::endl; + + std::cout << "------------------------------------------------------------------------------" << std::endl; + std::cout << "| The instructions are as follows:" << std::endl; + std::cout << "| 1、fastweb start config.ini\t\t [load the specified configuration and start Fastweb]" << std::endl; + std::cout << "| 2、fastweb create config .\t\t [create a default configuration file]" << std::endl; + std::cout << "| 3、fastweb create website .\t\t [create a default website directory]" << std::endl; + std::cout << "------------------------------------------------------------------------------" << std::endl; +} +int main(int argc, char* argv[]) +{ + if (argc < 2) { ouput_help(); QUIT_WAIT; } + std::string type = argv[1]; + if (type == "help") { - if (input == "restart") + ouput_help(); + QUIT_WAIT; + } + + if (argc < 3){ouput_help();QUIT_WAIT;} + + void* app = nullptr; + std::string value = argv[2]; + if (type == "start") + { + if (ylib::file::exist(std::filesystem::absolute(value).string())) { - std::cout << "closing..." << std::endl; - close(); - std::cout << "starting..." << std::endl; - start(); - if (fastweb_app_ptr == nullptr) + app = start(std::filesystem::absolute(value).string().c_str()); + if (app == nullptr) { - std::cin.get(); - return -1; + QUIT_WAIT; + } + while (true) { + std::string input; + std::cin >> input; + if (input == "quit" || input == "exit") + { + close(app); + std::cout << "closed"; + return 0; + } + else + { + std::cout << "Enter \"quit\" or \"exit\" to exit the application" << std::endl; + std::cout << "Enter \"restart\" to restart the application" << std::endl; + } } - std::cout << "started" << std::endl; - } - else if (input == "quit" || input == "exit") - { - close(); - std::cout << "closed"; - break; } else { - std::cout << "Enter \"quit\" or \"exit\" to exit the application" << std::endl; - std::cout << "Enter \"restart\" to restart the application" << std::endl; + std::cerr << "file not found: " << std::filesystem::absolute(value).string() << std::endl; + QUIT_WAIT; } + } + if (argc < 4) { ouput_help(); QUIT_WAIT; } - - std::cin >> input; + if (type == "create") + { + std::string path = std::filesystem::absolute(argv[3]).string(); + if (value == "config") + { + ylib::file::copy("/usr/local/share/fastweb/config.ini",path); + } + else if (value == "website") + { + ylib::file::copy_dir("/usr/local/share/fastweb", path); + } } return 0; }