Archetype: cpp_service_main.cpp.mustache
Entry point: construct host, run, map exceptions to exit codes. component-service profile.
See the Template variable reference for the complete list of available variables and their semantics.
Template
The full template source. Edit here and re-tangle with
compass build --direct tangle_codegen_templates to regenerate
library/templates/cpp_service_main.cpp.mustache.
{{! GENERATED FILE — tangled from projects/ores.codegen/library/templates/cpp_service_app.org. Edit the org source. }}
{{{cpp_license}}}
{{#component}}
#include <iostream>
#include <boost/asio/io_context.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/detached.hpp>
#include <openssl/crypto.h>
#include "{{full_name}}/app/host.hpp"
#include "{{full_name}}/config/parser_exception.hpp"
namespace {
boost::asio::awaitable<int>
async_main(int argc, char** argv, boost::asio::io_context& io_ctx) {
using {{namespace}}::app::host;
using {{namespace}}::config::parser_exception;
try {
const auto args(std::vector<std::string>(argv + 1, argv + argc));
co_return co_await host::execute(args, std::cout, std::cerr, io_ctx);
} catch (const parser_exception& /*e*/) {
co_return EXIT_FAILURE;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Failed to execute command." << std::endl;
co_return EXIT_FAILURE;
} catch (...) {
std::cerr << "Application was forced to terminate." << std::endl;
co_return EXIT_FAILURE;
}
}
}
int main(int argc, char** argv) {
boost::asio::io_context io_ctx;
int result = EXIT_FAILURE;
boost::asio::co_spawn(io_ctx, [&]() -> boost::asio::awaitable<void> {
result = co_await async_main(argc, argv, io_ctx);
io_ctx.stop();
}, boost::asio::detached);
io_ctx.run();
OPENSSL_cleanup();
return result;
}
{{/component}}
See also
- Parent facet: C++ service application templates
- Template variable reference