From c9144856c93052be8c92b74ff59fc4e7be07fbf7 Mon Sep 17 00:00:00 2001 From: Mystic <215104920@qq.com> Date: Mon, 16 Dec 2024 13:28:43 +0800 Subject: [PATCH] feat(cxx): exclude main.cpp from lib builds with xmake - Ensure the program is not treated as a server to allow normal termination during CI test runs --- .../src/main.cpp | 49 ++++++++++++++----- .../{{cookiecutter.project_slug}}/xmake.lua | 2 +- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/template/cxx/{{cookiecutter.project_slug}}/src/main.cpp b/template/cxx/{{cookiecutter.project_slug}}/src/main.cpp index 006b505..51c6848 100644 --- a/template/cxx/{{cookiecutter.project_slug}}/src/main.cpp +++ b/template/cxx/{{cookiecutter.project_slug}}/src/main.cpp @@ -1,5 +1,4 @@ #include - #include #include "cxxopts/cxxopts.hpp" @@ -8,16 +7,22 @@ int main(int argc, char* argv[]) { try { // Define the options - cxxopts::Options options("MyProgram", "A brief description of the program"); + cxxopts::Options options("MyProgram", "A simple HTTP server with command-line options"); - options.add_options()("h,help", "Print usage information")("n,name", "Name of the user", cxxopts::value())("v,verbose", "Enable verbose mode"); + // Add command-line options + options.add_options() + ("h,help", "Print usage information") + ("m,mode", "Server mode", cxxopts::value()->default_value("")) + ("n,name", "Name of the user", cxxopts::value()) + ("v,verbose", "Enable verbose mode") + ("p,port", "Server port", cxxopts::value()->default_value("8080")); // Parse the arguments auto result = options.parse(argc, argv); // Handle help option if (result.count("help")) { - spdlog::info(options.help()); + std::cout << options.help() << std::endl; return 0; } @@ -35,16 +40,36 @@ int main(int argc, char* argv[]) { spdlog::debug("Verbose mode is enabled."); } - // Start a simple server using cpp-httplib - httplib::Server svr; + // Check if server mode is specified + std::string mode = result["mode"].as(); + if (mode == "server") { + // Get port from command-line or use default + int port = result["port"].as(); + + // Start a simple server using cpp-httplib + httplib::Server svr; - svr.Get("/", [](const httplib::Request&, httplib::Response& res) { - res.set_content("Hello, World!", "text/plain"); - spdlog::info("Handled request at /"); - }); + // Root route + svr.Get("/", [](const httplib::Request&, httplib::Response& res) { + res.set_content("Hello, World!", "text/plain"); + spdlog::info("Handled request at /"); + }); - spdlog::info("Starting server on http://localhost:8080"); - svr.listen("localhost", 8080); + // Health check route + svr.Get("/health", [](const httplib::Request&, httplib::Response& res) { + res.set_content("Server is running", "text/plain"); + spdlog::info("Health check performed"); + }); + + spdlog::info("Starting server on http://localhost:{}", port); + if (!svr.listen("localhost", port)) { + spdlog::error("Failed to start server on port {}", port); + return 1; + } + } else if (!mode.empty()) { + spdlog::error("Invalid mode. Use --mode server to start the HTTP server."); + return 1; + } } catch (const cxxopts::exceptions::exception& e) { spdlog::error("Error parsing options: {}", e.what()); diff --git a/template/cxx/{{cookiecutter.project_slug}}/xmake.lua b/template/cxx/{{cookiecutter.project_slug}}/xmake.lua index 3c6757a..602813d 100644 --- a/template/cxx/{{cookiecutter.project_slug}}/xmake.lua +++ b/template/cxx/{{cookiecutter.project_slug}}/xmake.lua @@ -12,7 +12,7 @@ target("{{cookiecutter.package_name}}_lib") set_targetdir("build/lib") -- Specify output directory add_includedirs("include", {public = true}) -- Include project's header files add_includedirs("third_party", {public = true}) -- Include project's header files - add_files("src/*.cpp") -- Add project source files + add_files("src/*.cpp|main.cpp") -- Add project source files, but exclude main.cpp add_headerfiles("include/**/*.h") -- Add project's header files add_headerfiles("include/**/*.hpp") -- Add project's header files add_headerfiles("third_party/**/*.h") -- Add project's header files