Skip to content

Commit

Permalink
feat(cxx): exclude main.cpp from lib builds with xmake
Browse files Browse the repository at this point in the history
- Ensure the program is not treated as a server to allow normal termination during CI test runs
  • Loading branch information
pplmx committed Dec 16, 2024
1 parent 7a7b76e commit c914485
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
49 changes: 37 additions & 12 deletions template/cxx/{{cookiecutter.project_slug}}/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <spdlog/spdlog.h>

#include <iostream>

#include "cxxopts/cxxopts.hpp"
Expand All @@ -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<std::string>())("v,verbose", "Enable verbose mode");
// Add command-line options
options.add_options()
("h,help", "Print usage information")
("m,mode", "Server mode", cxxopts::value<std::string>()->default_value(""))
("n,name", "Name of the user", cxxopts::value<std::string>())
("v,verbose", "Enable verbose mode")
("p,port", "Server port", cxxopts::value<int>()->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;
}

Expand All @@ -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<std::string>();
if (mode == "server") {
// Get port from command-line or use default
int port = result["port"].as<int>();

// 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());
Expand Down
2 changes: 1 addition & 1 deletion template/cxx/{{cookiecutter.project_slug}}/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c914485

Please sign in to comment.