Example to use various APIs of servers.
#include <cstdlib>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#include <fmt/ranges.h>
#include <lyra/lyra.hpp>
void parse_command_line_arguments(int argc, char** argv,
std::string& config_file_path, std::string& config_name);
int main(int argc, char** argv) {
std::string config_file_path;
std::string config_name;
parse_command_line_arguments(argc, argv, config_file_path, config_name);
parser.
parse(config_file_path);
const std::shared_ptr<msgpack_rpc::logging::Logger> logger =
"add", [](int x, int y) { return x + y; })
.add_method<void(std::string)>("print",
[logger](const std::string& str) {
})
.add_method<void()>("throw",
[]() -> void {
throw std::runtime_error("Example exception.");
})
.add_method<void(int)>("throw_int",
[](int val) -> void {
})
.listen_to("tcp://localhost:8246")
const std::vector<msgpack_rpc::addresses::URI> server_uris =
return 0;
}
void parse_command_line_arguments(int argc, char** argv,
std::string& config_file_path, std::string& config_name) {
config_file_path = "./examples/more/config.toml";
config_name = "example";
bool show_help = false;
const auto cli = lyra::cli()
.add_argument(lyra::opt(config_file_path, "file path")
.name("--config-file")
.name("-f")
.optional()
.help("Configuration file."))
.add_argument(lyra::opt(config_name, "name")
.name("--config-name")
.name("-n")
.optional()
.help("Configuration name."))
.add_argument(lyra::help(show_help));
const auto result = cli.parse({argc, argv});
if (!result) {
std::cerr << result.message() << "\n\n" << cli << std::endl;
std::exit(1);
}
if (show_help) {
std::cout << cli << std::endl;
std::exit(0);
}
}
Class to parse configuration.
const LoggingConfig & logging_config(std::string_view name) const
Get a configuration of logging.
const ServerConfig & server_config(std::string_view name) const
Get a configuration of server.
void parse(std::string_view file_path)
Parse a file.
static std::shared_ptr< Logger > create(const config::LoggingConfig &config=config::LoggingConfig())
Create a logger.
Class of exceptions in methods.
Class of builders of servers.
ServerBuilder & add_method(std::unique_ptr< methods::IMethod > method)
Add a method.
Server build()
Build a server.
ServerBuilder & listen_to_tcp(std::string_view host, std::uint16_t port_number)
Add a TCP address to listen to.
ServerBuilder & listen_to(addresses::URI uri)
Add a URI to listen to.
void stop()
Stop processing of this server.
void run_until_signal()
Run processing of this server until SIGINT or SIGTERM is received.
std::vector< addresses::URI > local_endpoint_uris()
Get the URIs of the local endpoints in this server.
Definition of ConfigParser class.
Definition of Logger class.
#define MSGPACK_RPC_INFO(LOGGER_PTR,...)
Write a information log.
Definition of MethodException class.
Definition of Server class.
Definition of ServerBuilder class.