Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy-peasy library (WIP) #767

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmake/UserverTestsuite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ function(userver_testsuite_add_simple)
CONFIG_VARS_PATH
DYNAMIC_CONFIG_FALLBACK_PATH
SECDIST_PATH
DUMP_CONFIG
)
set(multiValueArgs
PYTEST_ARGS
Expand Down Expand Up @@ -358,9 +359,16 @@ function(userver_testsuite_add_simple)
set(ARG_TEST_SUFFIX "")
endif()

set(DUMP_CONFIG_OPTION "")
if (ARG_DUMP_CONFIG)
set(DUMP_CONFIG_OPTION "--dump-config")
endif()

if(ARG_CONFIG_PATH)
get_filename_component(config_path "${ARG_CONFIG_PATH}"
REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
elseif(ARG_DUMP_CONFIG)
set(config_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary/static_config.yaml")
else()
foreach(probable_config_path IN ITEMS
"${CMAKE_CURRENT_SOURCE_DIR}/configs/static_config.yaml"
Expand Down Expand Up @@ -460,6 +468,7 @@ function(userver_testsuite_add_simple)
"--service-config=${config_path}"
"--service-source-dir=${CMAKE_CURRENT_SOURCE_DIR}"
"--service-binary=${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}"
"${DUMP_CONFIG_OPTION}"
${pytest_additional_args}
${ARG_PYTEST_ARGS}
REQUIREMENTS ${ARG_REQUIREMENTS}
Expand Down
10 changes: 10 additions & 0 deletions cmake/install/userver-easy-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include_guard(GLOBAL)

if(userver_easy_FOUND)
return()
endif()

find_package(userver REQUIRED COMPONENTS postgresql)

set(userver_easy_FOUND TRUE)

18 changes: 15 additions & 3 deletions core/include/userver/utils/daemon_run.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@

#include <userver/components/component_list.hpp>


namespace boost::program_options {
class options_description;
class variables_map;
}

USERVER_NAMESPACE_BEGIN

namespace utils {

/// Parses command line arguments and calls components::Run with config file
/// from --config parameter.
/// @returns default options of DaemonMain
///
/// Other command line arguments:
/// List of options:
/// * --help - show all command line arguments
/// * --config CONFIG - path to config.yaml
/// * --config_vars CONFIG_VARS - path to config_vars.yaml
/// * --config_vars_override CONFIG_VARS - path to config_vars.override.yaml
/// * --print-config-schema - print config.yaml YAML Schema
/// * --print-dynamic-config-defaults - print JSON with dynamic config defaults
boost::program_options::options_description BaseRunOptions();

/// Parses command line arguments and calls components::Run with config file
/// from --config parameter. See BaseRunOptions() for a list of options
int DaemonMain(int argc, const char* const argv[], const components::ComponentList& components_list);

/// @overload
int DaemonMain(const boost::program_options::variables_map& vm, const components::ComponentList& components_list);

} // namespace utils

USERVER_NAMESPACE_END
54 changes: 31 additions & 23 deletions core/src/utils/daemon_run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,38 @@ namespace utils {

namespace {

std::optional<std::string> ToOptional(std::string&& s) {
if (s.empty())
template <class Value>
std::optional<std::string> ToOptional(const Value& val) {
if (val.empty())
return {};
else
return {std::move(s)};
return {val.template as<std::string>()};
}

} // namespace

int DaemonMain(const int argc, const char* const argv[], const components::ComponentList& components_list) {
utils::impl::FinishStaticRegistration();

boost::program_options::options_description BaseRunOptions() {
namespace po = boost::program_options;

po::variables_map vm;
po::options_description desc("Allowed options");
std::string config_path;
std::string config_vars_path;
std::string config_vars_override_path;

// clang-format off
desc.add_options()
("help,h", "produce this help message")
("print-config-schema", "print config.yaml YAML Schema")
("print-dynamic-config-defaults", "print JSON object with dynamic config defaults")
("config,c", po::value(&config_path)->required(), "path to server config")
("config_vars", po::value(&config_vars_path), "path to config_vars.yaml; if set, config_vars in config.yaml are ignored")
("config_vars_override", po::value(&config_vars_override_path), "path to an additional config_vars.yaml, which overrides vars of config_vars.yaml")
;
desc.add_options()
("help,h", "produce this help message")
("print-config-schema", "print config.yaml YAML Schema")
("print-dynamic-config-defaults", "print JSON object with dynamic config defaults")
("config_vars", po::value<std::string>(), "path to config_vars.yaml; if set, config_vars in config.yaml are ignored")
("config_vars_override", po::value<std::string>(), "path to an additional config_vars.yaml, which overrides vars of config_vars.yaml")
;
// clang-format on
return desc;
}

int DaemonMain(const int argc, const char* const argv[], const components::ComponentList& components_list) {
namespace po = boost::program_options;
po::variables_map vm;
auto desc = BaseRunOptions();
desc.add_options()
("config,c", po::value<std::string>()->required(), "path to server config")
;

try {
po::store(po::parse_command_line(argc, argv, desc), vm);
Expand All @@ -59,6 +61,12 @@ int DaemonMain(const int argc, const char* const argv[], const components::Compo
return 0;
}

return DaemonMain(vm, components_list);
}

int DaemonMain(const boost::program_options::variables_map& vm, const components::ComponentList& components_list) {
utils::impl::FinishStaticRegistration();

if (vm.count("print-config-schema")) {
std::cout << components::impl::GetStaticConfigSchema(components_list) << "\n";
return 0;
Expand All @@ -71,9 +79,9 @@ int DaemonMain(const int argc, const char* const argv[], const components::Compo

try {
components::Run(
config_path,
ToOptional(std::move(config_vars_path)),
ToOptional(std::move(config_vars_override_path)),
vm["config"].as<std::string>(),
ToOptional(vm["config_vars"]),
ToOptional(vm["config_vars_override"]),
components_list
);
return 0;
Expand Down
8 changes: 8 additions & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
option(USERVER_FEATURE_EASY "Build easy HTTP server library" "${USERVER_LIB_ENABLED_DEFAULT}")
option(USERVER_FEATURE_S3API "Build S3 api client library" "${USERVER_LIB_ENABLED_DEFAULT}")
option(USERVER_FEATURE_GRPC_REFLECTION "Build grpc reflection library" "${USERVER_LIB_ENABLED_DEFAULT}")

if (USERVER_FEATURE_S3API)
add_subdirectory(s3api)
endif()

if (USERVER_FEATURE_EASY)
if (NOT USERVER_FEATURE_POSTGRESQL)
message(FATAL_ERROR "'USERVER_FEATURE_EASY' requires 'USERVER_FEATURE_POSTGRESQL=ON'")
endif()
add_subdirectory(easy)
endif()

if (USERVER_FEATURE_GRPC_REFLECTION)
if (NOT USERVER_FEATURE_GRPC)
message(FATAL_ERROR "'USERVER_FEATURE_GRPC_REFLECTION' requires 'USERVER_FEATURE_GRPC=ON'")
Expand Down
11 changes: 11 additions & 0 deletions libraries/easy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
project(userver-easy CXX)

userver_module(easy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module -> component?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, но не в этом PR

SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
LINK_LIBRARIES userver::postgresql
UTEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*_test.cpp"
)

if(USERVER_BUILD_SAMPLES)
add_subdirectory(samples)
endif()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_sample_subdirectory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Возможно, но не в этом PR

Loading
Loading