From 2dd4ebe5155fcf4882ddee3384560dd56677d288 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 11 Apr 2017 18:19:19 +0100 Subject: [PATCH] Load redis parameters from config file. --- src/CMakeLists.txt | 5 +++-- src/main.cpp | 33 ++++++++++++++++++++++++++++++++- src/pathname/pathname.hpp | 2 ++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4fd77a..76a62a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,8 +7,8 @@ find_package(SourceHighlight REQUIRED) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(TAWASHI_CONFIG_PATH "etc") -set(TAWASHI_CONFIG_FILE "${PROJECT_NAME}.ini") +set(TAWASHI_CONFIG_PATH "etc" CACHE STRING "Path where config file will be located, absolute or relative to the install prefix") +set(TAWASHI_CONFIG_FILE "${PROJECT_NAME}.ini" CACHE STRING "Filename of the config file in TAWASHI_CONFIG_PATH") add_executable(${PROJECT_NAME} main.cpp @@ -24,6 +24,7 @@ add_executable(${PROJECT_NAME} index_response.cpp pastie_response.cpp ini_file.cpp + pathname/pathname.cpp ) configure_file( diff --git a/src/main.cpp b/src/main.cpp index e9c1c1f..ae412a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,18 +21,49 @@ #include "pastie_response.hpp" #include "index_response.hpp" #include "cgi_env.hpp" +#include "ini_file.hpp" +#include "pathname/pathname.hpp" +#include "duckhandy/compatibility.h" +#include "duckhandy/lexical_cast.hpp" #include #include +#include +#include +#include //www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4150.pdf namespace { + std::string config_file_path() a_pure; + + std::string config_file_path() { + mchlib::PathName config_path(TAWASHI_CONFIG_PATH); + mchlib::PathName full_path(""); + if (config_path.is_absolute()) { + full_path = std::move(config_path); + } + else { + full_path = mchlib::PathName(TAWASHI_PATH_PREFIX); + full_path.join(config_path); + } + full_path.join(TAWASHI_CONFIG_FILE); + return full_path.path(); + } } //unnamed namespace int main() { //std::cout << "Content-type:text/plain\n\n"; - redis::IncRedis incredis("127.0.0.1", 6379); +#if !defined(NDEBUG) + std::cerr << "Loading config: \"" << config_file_path() << "\"\n"; +#endif + std::ifstream conf(config_file_path()); + conf >> std::noskipws; + tawashi::IniFile ini = tawashi::IniFile(std::istream_iterator(conf), std::istream_iterator()); + conf.close(); + const auto& settings = ini.parsed().at("tawashi"); + + redis::IncRedis incredis(std::string(settings.at("redis_server")), dhandy::lexical_cast(settings.at("redis_port"))); incredis.connect(); tawashi::cgi::Env cgi_env; diff --git a/src/pathname/pathname.hpp b/src/pathname/pathname.hpp index f9e0d5e..ef9910c 100644 --- a/src/pathname/pathname.hpp +++ b/src/pathname/pathname.hpp @@ -34,6 +34,8 @@ namespace mchlib { explicit PathName ( boost::string_ref parPath ); ~PathName ( void ) noexcept = default; + PathName& operator= ( PathName&& ) = default; + bool is_absolute ( void ) const { return m_absolute; } std::string path ( void ) const; std::size_t str_path_size ( void ) const;