### Install Ppconsul with Make Source: https://github.com/oliora/ppconsul/blob/master/README.md If the Makefile generator was used, you can install the built Ppconsul library using the 'make install' command. ```bash make install ``` -------------------------------- ### Build Ppconsul using CMake and Conan Source: https://github.com/oliora/ppconsul/blob/master/README.md Steps to build the Ppconsul library using CMake and Conan, including dependency installation, configuration, building, and installation. ```bash # Install dependencies conan install . # Make workspace directory mkdir workspace cd workspace # Configure cmake .. # Optionally override the default installation destination: cmake -DCMAKE_INSTALL_PREFIX= .. #Build cmake --build . --config Release # Install (needs a writable installation destination, see CMAKE_INSTALL_PREFIX above) cmake --build . --config Release --target install ``` -------------------------------- ### Install Ppconsul with CMake Source: https://github.com/oliora/ppconsul/blob/master/README.md Install the built Ppconsul library using CMake after building it. This command is cross-platform. ```bash cmake --build . --config Release --target install ``` -------------------------------- ### Run Consul Agent (0.7-0.8) Source: https://github.com/oliora/ppconsul/blob/master/README.md Start a development Consul agent for versions 0.7 and 0.8. This is for running Ppconsul tests. ```bash consul agent -dev -datacenter=ppconsul_test ``` -------------------------------- ### Run Consul Agent (0.9+) Source: https://github.com/oliora/ppconsul/blob/master/README.md Start a development Consul agent with script checks enabled. This is for running Ppconsul tests. ```bash consul agent -dev -datacenter=ppconsul_test -enable-script-checks=true ``` -------------------------------- ### Configure Project with CMake Source: https://github.com/oliora/ppconsul/blob/master/README.md Use these commands to configure the Ppconsul project using CMake. You can specify Boost and libCURL roots, installation prefix, and build static libraries. ```bash mkdir workspace cd workspace cmake .. ``` -------------------------------- ### Manual libCURL Initialization for Multi-Threaded Applications Source: https://github.com/oliora/ppconsul/blob/master/README.md Manually call curl_global_init and curl_global_cleanup if Ppconsul's default initialization timing is not suitable for your multi-threaded application. Ensure initialization happens before extra threads start. ```cpp int main(int argc, char *argv[]) { curl_global_init(CURL_GLOBAL_DEFAULT | CURL_GLOBAL_SSL); // Existing code that uses Ppconsul and starts extra threads... curl_global_cleanup(); return 0; } ``` -------------------------------- ### Use Ppconsul in a CMake Project Source: https://github.com/oliora/ppconsul/blob/master/README.md Integrate the installed Ppconsul library into your CMake-based project by finding the package and linking against it. ```cmake find_package(ppconsul) add_executable( ...) target_link_libraries( ppconsul) ``` -------------------------------- ### Set Minimum CMake Version Source: https://github.com/oliora/ppconsul/blob/master/tests/doc_examples/CMakeLists.txt Specifies the minimum required version of CMake for the project. Ensure your CMake installation meets this requirement. ```cmake cmake_minimum_required(VERSION 3.10) ``` -------------------------------- ### Include Ppconsul as a Submodule in CMake Source: https://github.com/oliora/ppconsul/blob/master/README.md Add Ppconsul as a Git submodule to your project and include it as a subdirectory in your CMake build. This example disables tests and forces static library build. ```cmake set(BUILD_TESTS OFF) set(BUILD_STATIC_LIB ON) add_subdirectory(ppconsul) ... target_link_libraries( ppconsul) ``` -------------------------------- ### Registering Checks and Services with Keyword Parameters Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Demonstrates various styles of registering checks and services using keyword parameters for optional fields. ```cpp // New style 1: all optional params are keywords agent.registerCheck("check1", params::ttl=std::chrono::seconds(10)); agent.registerCheck("check1", params::notes="", params::ttl=std::chrono::seconds(10)); agent.registerCheck("check1", params::notes="", params::script="bla-bla.py", params::interval=std::chrono::seconds(10)); agent.registerService("service1", params::tags={"udp", "service1", "service2"}, params::ttl=std::chrono::seconds(10)); agent.registerService("service1", params::tags={"udp", "service1", "service2"}, params::script="bla-bla.py", params::interval=std::chrono::seconds(10)); // New style 2: structures with keywords agent.registerCheck({"check1", params::notes=""}, HttpCheck("http://localhost/test", std::chrono::seconds(10), params::tiemout=std::chrono::seconds(2))); agent.registerService({"service1", params::tags={"udp", "service1", "service2"}}); // ... // New Style 3: higher granulated keywords agent.registerCheck("check1", params::notes="", params::ttl_check={std::chrono::seconds(10)}); agent.registerCheck("check1", params::notes="", params::script_check={params::script="bla-bla.py", params::interval=std::chrono::seconds(10)}); agent.registerService("service1", params::tags={"udp", "service1", "service2"}); agent.registerService("service1", params::tags={"udp", "service1", "service2"}, params::ttl_check={std::chrono::seconds(10)}); agent.registerService("service1", params::tags={"udp", "service1", "service2"}, params::script_check={"bla-bla.py", std::chrono::seconds(10), params::timeout=std::chrono::seconds(2)}); // New Style 4: higher granulated keywords using variant agent.registerCheck("check1", TtlCheck{std::chrono::seconds(10)}); agent.registerCheck("check1", ScriptCheck{"bla-bla.py", std::chrono::seconds(10), params::timeout=std::chrono::seconds(2)}, params::notes=""); agent.registerService("service1", params::tags={"udp", "service1", "service2"}); agent.registerService("service1", params::tags={"udp", "service1", "service2"}, params::check=TtlCheck{std::chrono::seconds(10)}); agent.registerService("service1", params::tags={"udp", "service1", "service2"}, params::check=ScriptCheck{"bla-bla.py", std::chrono::seconds(10), params::timeout=std::chrono::seconds(2)}); ``` -------------------------------- ### CMakeLists.txt Configuration Source: https://github.com/oliora/ppconsul/blob/master/tests/consul-get/CMakeLists.txt Defines the project name, adds the main executable, and links test libraries. ```cmake # Copyright (c) 2014-2020 Andrey Upadyshev # # Use, modification and distribution are subject to the # Boost Software License, Version 1.0. (See accompanying file # LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) project(consul-get) add_executable(${PROJECT_NAME} consul_get.cpp) link_test_libs(${PROJECT_NAME}) ``` -------------------------------- ### Registering a Check with Keyword Arguments Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Demonstrates registering a check using a combination of a registration object and additional keyword arguments for configuration. ```cpp agent.registerCheck( keywords::name = "", keywords::check*** = agent::ScriptCheck{"ccc.sh", std::chrono::seconds(20)}, keywords::notes = "", keywords::id = "", keywords::token = "" ); agent.registerCheck( { keywords::name = "", keywords::check*** = agent::ScriptCheck{"ccc.sh", std::chrono::seconds(20)}, keywords::notes = "", keywords::id = "" }, keywords::token = "" ); ``` -------------------------------- ### Build Ppconsul with CMake Source: https://github.com/oliora/ppconsul/blob/master/README.md Build the Ppconsul project in Release configuration using CMake. This command is cross-platform. ```bash cmake --build . --config Release ``` -------------------------------- ### Run Tests with Make Source: https://github.com/oliora/ppconsul/blob/master/README.md If the Makefile generator was used, you can run the Ppconsul tests using the 'make test' command. ```bash make test ``` -------------------------------- ### Build Ppconsul with Make Source: https://github.com/oliora/ppconsul/blob/master/README.md If the Makefile generator was used with CMake, you can build the project using the 'make' command. ```bash make ``` -------------------------------- ### Skip libCURL Initialization in Ppconsul Source: https://github.com/oliora/ppconsul/blob/master/README.md Create a default HttpClientFactory explicitly with 'false' to skip libCURL initialization within Ppconsul, allowing exclusive control over libCURL initialization. ```cpp Consul consul(makeDefaultHttpClientFactory(false), ...); ``` -------------------------------- ### Registering a Check with Keyword Arguments (Alternative) Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Shows an alternative way to register a check where all fields are specified using keyword arguments. ```cpp agent.registerCheck( { keywords::name = "", keywords::check*** = agent::ScriptCheck{"ccc.sh", std::chrono::seconds(20)}, keywords::notes = "", keywords::id = "" }, keywords::token = "" ); or even agent.registerCheck( { keywords::name = "", keywords::script_check = {"ccc.sh", std::chrono::seconds(20)}, keywords::notes = "", keywords::id = "" }, keywords::token = "" ); ``` -------------------------------- ### Project and Executable Configuration Source: https://github.com/oliora/ppconsul/blob/master/tests/health/CMakeLists.txt Defines the project name and sets up an executable for health tests, linking it with main application sources and test-specific files. It also links necessary test libraries and adds the Catch2 test framework. ```cmake project(health-tests) add_executable(${PROJECT_NAME} ../main.cpp health_tests.cpp ) link_test_libs(${PROJECT_NAME}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Register Service - Current Approach Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Registers a service using the current approach with explicit check parameters and service ID. ```C++ ppconsul::agent::Agent agent(consul); agent.registerService({ name, ppconsul::kw::check = ppconsul::agent::TtlCheckParams{std::chrono::minutes(5)}, ppconsul::kw::params::id = "id" }); ``` -------------------------------- ### Define Project and Executable Source: https://github.com/oliora/ppconsul/blob/master/tests/consul/CMakeLists.txt Sets the project name and defines the main executable for the tests, linking it to the core application and test libraries. ```cmake project(consul-tests) add_executable(${PROJECT_NAME} ../main.cpp consul_tests.cpp ) link_test_libs(${PROJECT_NAME}) ``` -------------------------------- ### Run Tests with CTest Source: https://github.com/oliora/ppconsul/blob/master/README.md Execute the Ppconsul tests using CTest in Release configuration. Ensure Consul is running. ```bash ctest -C Release ``` -------------------------------- ### TTL and Script Check Parameters Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Shows alternative structures for TTL and script check parameters, including an optional description for script checks. ```C++ ppconsul::agent::params::ttl_check = {std::chrono::minutes(5)} ppconsul::agent::params::scipt_check = {"run.sh", std::chrono::minutes(5), "bla-bla"} ``` -------------------------------- ### Define Executable and Link Libraries Source: https://github.com/oliora/ppconsul/blob/master/tests/sessions/CMakeLists.txt Configures the C++ executable for session tests, linking against test libraries and the Catch2 testing framework. ```cmake # Copyright (c) 2019-2020 Andrey Upadyshev # # Use, modification and distribution are subject to the # Boost Software License, Version 1.0. (See accompanying file # LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) project(sessions-tests) add_executable(${PROJECT_NAME} ../main.cpp sessions_tests.cpp ) link_test_libs(${PROJECT_NAME}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Typed Parameters for Check Params Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Illustrates using typed parameters for check configurations, specifically for TTL checks. ```cpp ppconsul::agent::params::check_params = ppconsul::agent::TtlCheckParams{std::chrono::minutes(5)} ``` -------------------------------- ### Registering a Check with Mixed Arguments Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Illustrates registering a check where some fields are positional and optional fields use keyword arguments. ```cpp agent.registerCheck( { "name", agent::ScriptCheck{"ccc.sh", std::chrono::seconds(20)}, keywords::notes = "", keywords::id = "" }, keywords::token = "" ); ``` -------------------------------- ### Register Service - Use Type's Namespace Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Registers a service using a type's namespace for checks and IDs, aiming for clarity but potentially causing confusion. ```C++ ppconsul::agent::Agent agent(consul); agent.registerService({ name, ppconsul::service::check = ppconsul::agent::TtlCheckParams{std::chrono::minutes(5)}, ppconsul::service::id = "id" }); ``` -------------------------------- ### Define Project and Executable Source: https://github.com/oliora/ppconsul/blob/master/tests/doc_examples/CMakeLists.txt Sets the project name and defines the source files for an executable. This is a standard way to configure a build target in CMake. ```cmake project(doc_examples) add_executable(${PROJECT_NAME} agent.cpp kv.cpp main.cpp ) ``` -------------------------------- ### Link ppconsul Library Source: https://github.com/oliora/ppconsul/blob/master/tests/doc_examples/CMakeLists.txt Links the ppconsul library to the target executable. This makes ppconsul's functions and classes available to your project's source code. ```cmake target_link_libraries(${PROJECT_NAME} ppconsul) ``` -------------------------------- ### Define Catalog Tests Executable Source: https://github.com/oliora/ppconsul/blob/master/tests/catalog/CMakeLists.txt Defines the main executable for the catalog tests, specifying source files and linking necessary libraries. ```cmake project(catalog-tests) add_executable(${PROJECT_NAME} ../main.cpp catalog_tests.cpp ) link_test_libs(${PROJECT_NAME}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Connect to Consul via HTTPS (TLS/SSL) Source: https://github.com/oliora/ppconsul/blob/master/README.md Configure Consul client to connect using HTTPS by providing certificate, private key, and CA certificate paths. ```cpp #include "ppconsul/consul.h" using namespace ppconsul; Consul consul("https://localhost:8080", kw::tls::cert="path/to/cert", kw::tls::key="path/to/private/key", kw::tls::ca_info="path/to/ca/cert"); // Use consul ... ``` -------------------------------- ### Blocking Query to Key-Value Storage Source: https://github.com/oliora/ppconsul/blob/master/README.md Perform a blocking query on a key-value item, waiting for changes up to a specified duration. ```cpp // Get key+value+metadata item KeyValue item = kv.item("status.last-event-id"); // Wait for the item change for no more than 1 minute: item = kv.item("status.last-event-id", kw::block_for = {std::chrono::minutes(1), item.modifyIndex}); // If key exists, print it: if (item) std::cout << item.key << "=" << item.value << "\n"; ``` -------------------------------- ### Define kv-tests Executable Source: https://github.com/oliora/ppconsul/blob/master/tests/kv/CMakeLists.txt Defines the kv-tests executable and lists its source files. It also links necessary test libraries and the Threads library. ```cmake project(kv-tests) add_executable(${PROJECT_NAME} ../main.cpp kv_tests.cpp ) link_test_libs(${PROJECT_NAME}) find_package(Threads) target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Configure Consul JSON Input Source: https://github.com/oliora/ppconsul/blob/master/tests/CMakeLists.txt Configures the 'consul.json' file from a template 'consul.json.in'. This is typically used to inject build-time variables into configuration files. ```cmake configure_file(consul.json.in consul.json) ``` -------------------------------- ### Configure Connect and Request Timeouts for Consul Source: https://github.com/oliora/ppconsul/blob/master/README.md Override default connect and request timeouts when constructing the Consul object. Ensure request timeout is longer than block interval for blocking queries. ```cpp #include "ppconsul/consul.h" using namespace ppconsul; Consul consul("https://localhost:8080", kw::connect_timeout = std::chrono::milliseconds{15000} kw::request_timeout = std::chrono::milliseconds{5000}); // Use consul ... ``` -------------------------------- ### Link Test Libraries Macro Source: https://github.com/oliora/ppconsul/blob/master/tests/CMakeLists.txt Defines a CMake macro to set include directories and link libraries for test targets. Use this macro to configure common settings for test executables. ```cmake macro(link_test_libs project_name) target_include_directories(${project_name} PRIVATE ${Boost_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/ext ${CMAKE_SOURCE_DIR}/tests ) target_link_libraries(${project_name} ppconsul) endmacro() ``` -------------------------------- ### Use Key-Value Storage Source: https://github.com/oliora/ppconsul/blob/master/README.md Read, erase, and set key-value pairs in Consul's storage. Supports specifying consistency mode for reads. ```cpp #include "ppconsul/kv.h" using ppconsul::Consul; using ppconsul::Consistency; using namespace ppconsul::kv; Consul consul; // We need the 'kv' endpoint Kv kv(consul); // Read the value of a key from the storage std::string something = kv.get("settings.something", "default-value"); // Read the value of a key from the storage with consistency mode specified something = kv.get("settings.something", "default-value", kw::consistency = Consistency::Consistent); // Erase a key from the storage kv.erase("settings.something-else"); // Set the value of a key kv.set("settings.something", "new-value"); ``` -------------------------------- ### Register Service - Agent Service Namespace Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Registers a service using the 'agent::service' namespace for checks and IDs, providing a distinct naming convention. ```C++ ppconsul::agent::Agent agent(consul); agent.registerService({ name, ppconsul::agent::service_check = ppconsul::agent::TtlCheckParams{std::chrono::minutes(5)}, ppconsul::agent::service_id = "id" }); ``` -------------------------------- ### Register Service - Remove Endpoint Namespace Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Registers a service after removing the endpoint namespace, potentially reducing verbosity but still prone to collisions. ```C++ ppconsul::Agent agent(consul); agent.registerService({ name, ppconsul::params::check = ppconsul::agent::TtlCheckParams{std::chrono::minutes(5)}, ppconsul::params::id = "id" }); ``` -------------------------------- ### Script Check Parameters Source: https://github.com/oliora/ppconsul/blob/master/rationales.md Defines parameters for a script-based check with a specified interval. ```C++ ppconsul::agent::params::check_params = ppconsul::agent::ScriptCheckParams{"run.sh", std::chrono::minutes(5)} ``` -------------------------------- ### Define Unittests Executable Source: https://github.com/oliora/ppconsul/blob/master/tests/unittests/CMakeLists.txt Defines the unittests executable and lists its source files. It also links test libraries and adds the Catch2 testing framework. ```cmake project(unittests) add_executable(${PROJECT_NAME} ../main.cpp consul.cpp helpers.cpp parameters.cpp ) link_test_libs(${PROJECT_NAME}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Find ppconsul Package Source: https://github.com/oliora/ppconsul/blob/master/tests/doc_examples/CMakeLists.txt Attempts to find and load the ppconsul package. This is required for projects that depend on ppconsul's functionality. It should be uncommented if ppconsul is an external dependency. ```cmake # Should be uncommented for external project #find_package(ppconsul REQUIRED) ``` -------------------------------- ### Link Thread Library and Add Catch Test Source: https://github.com/oliora/ppconsul/blob/master/tests/consul/CMakeLists.txt Finds and links the Threads library and adds the executable as a test case for the Catch2 framework. ```cmake find_package(Threads) target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Register, Deregister, and Report Service State Source: https://github.com/oliora/ppconsul/blob/master/README.md Register a service with associated HTTP check, deregister it, or register with a TTL and report its status. ```cpp #include "ppconsul/agent.h" using ppconsul::Consul; using namespace ppconsul::agent; // Create a consul client that uses default local endpoint `http://127.0.0.1:8500` and default data center Consul consul; // We need the 'agent' endpoint for a service registration Agent agent(consul); // Register a service with associated HTTP check: agent.registerService( kw::name = "my-service", kw::port = 9876, kw::tags = {"tcp", "super_server"}, kw::check = HttpCheck{"http://localhost:80/", std::chrono::seconds(2)} ); ... // Unregister service agent.deregisterService("my-service"); ... // Register a service with TTL agent.registerService( kw::name = "my-service", kw::port = 9876, kw::id = "my-service-1", kw::check = TtlCheck{std::chrono::seconds(5)} ); // Report service is OK agent.servicePass("my-service-1"); // Report service is failed agent.serviceFail("my-service-1", "Disk is full"); ``` -------------------------------- ### Define Executable for Status Tests Source: https://github.com/oliora/ppconsul/blob/master/tests/status/CMakeLists.txt Defines the executable for status tests, specifying its source files and linking required libraries. This is typically used at the top level of a test suite's CMakeLists.txt. ```cmake # Copyright (c) 2014-2020 Andrey Upadyshev # # Use, modification and distribution are subject to the # Boost Software License, Version 1.0. (See accompanying file # LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) project(status-tests) add_executable(${PROJECT_NAME} ../main.cpp status_tests.cpp ) link_test_libs(${PROJECT_NAME}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Define Agent Tests Executable Source: https://github.com/oliora/ppconsul/blob/master/tests/agent/CMakeLists.txt Configures the agent tests executable, specifying source files and linking test libraries. This is used to build the test suite for the agent component. ```cmake project(agent-tests) add_executable(${PROJECT_NAME} ../main.cpp agent_checks_tests.cpp agent_services_tests.cpp agent_other_tests.cpp ) link_test_libs(${PROJECT_NAME}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Define Coordinate Tests Executable Source: https://github.com/oliora/ppconsul/blob/master/tests/coordinate/CMakeLists.txt Defines the main executable for the coordinate tests. It includes the main application source and the specific test source file. This snippet also links necessary test libraries and adds the test runner. ```cmake project(coordinate-tests) add_executable(${PROJECT_NAME} ../main.cpp coordinate_tests.cpp ) link_test_libs(${PROJECT_NAME}) add_catch_test(${PROJECT_NAME}) ``` -------------------------------- ### Add Subdirectories for Modules Source: https://github.com/oliora/ppconsul/blob/master/tests/CMakeLists.txt Includes various subdirectories into the CMake build. These subdirectories likely contain source code and build configurations for different modules of the project. ```cmake add_subdirectory(unittests) # to run unittests before consul ones add_subdirectory(consul) add_subdirectory(agent) add_subdirectory(catalog) add_subdirectory(coordinate) add_subdirectory(kv) add_subdirectory(sessions) add_subdirectory(status) add_subdirectory(consul-get) add_subdirectory(doc_examples) ``` -------------------------------- ### Abort All Blocking Queries in Consul Source: https://github.com/oliora/ppconsul/blob/master/README.md Use Consul::stop() to abort all pending requests. This call is irreversible and switches the Consul object to a stopped state. ```cpp Consul consul(kw::enable_stop = true); // Must be enabled at construction time Kv kv(consul); // Issue blocking queries, similarly to example above, on background threads etc. // Stop all pending requests, e.g. at shutdown. No further requests can be done after this call. consul.stop(); ``` -------------------------------- ### Add Catch Test Macro Source: https://github.com/oliora/ppconsul/blob/master/tests/CMakeLists.txt Defines a CMake macro to add a test to CTest, with optional support for test report formatting. Use this macro to integrate Catch2 tests into the build system. ```cmake macro(add_catch_test name) if (TEST_REPORT_FORMAT) add_test(NAME ${name} COMMAND ${name} -r ${TEST_REPORT_FORMAT} -o "${name}.test_out.xml") else() add_test(NAME ${name} COMMAND ${name}) endif() endmacro() ``` -------------------------------- ### Determine Raft Leader and Peers Source: https://github.com/oliora/ppconsul/blob/master/README.md Check if a Raft leader is elected, determine the current Raft leader, and retrieve the list of Raft peers. ```cpp #include "ppconsul/status.h" using ppconsul::Consul; using namespace ppconsul::status; // Create a consul client that uses default local endpoint `http://127.0.0.1:8500` and default data center Consul consul; // We need the status endpoint Status status(consul); // Determine whether a leader has been elected bool isLeaderElected = status.isLeaderElected(); // Determine the actual raft leader auto leader = status.leader(); // Determine the raft peers auto peers = status.peers(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.