From 1870829ec26ae73a9092a780572ac931ec26589c Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 18 Apr 2017 18:40:42 +0100 Subject: [PATCH] Import lib mstch and put it to good use. Response classes are given a chance to modify the dictionary that is later sent to mstch. --- .gitmodules | 3 +++ CMakeLists.txt | 1 + index.html.mstch | 7 +++++++ lib/mstch | 1 + src/CMakeLists.txt | 4 +++- src/index_response.cpp | 7 ------- src/index_response.hpp | 1 - src/response.cpp | 19 ++++++++++++++++++- src/response.hpp | 4 +++- src/tawashiConfig.h.in | 3 +++ 10 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 index.html.mstch create mode 160000 lib/mstch diff --git a/.gitmodules b/.gitmodules index cf06646..01aa736 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "lib/better-enums"] path = lib/better-enums url = https://github.com/aantron/better-enums +[submodule "lib/mstch"] + path = lib/mstch + url = https://github.com/no1msd/mstch.git diff --git a/CMakeLists.txt b/CMakeLists.txt index b604920..21a37f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,4 +5,5 @@ project(tawashi_top) set(TAWASHI_SOURCE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}") add_subdirectory(lib/incredis) +add_subdirectory(lib/mstch) add_subdirectory(src) diff --git a/index.html.mstch b/index.html.mstch new file mode 100644 index 0000000..d0ef4bc --- /dev/null +++ b/index.html.mstch @@ -0,0 +1,7 @@ +

tawashi v{{version}}

+
+ +
+ +
+
diff --git a/lib/mstch b/lib/mstch new file mode 160000 index 0000000..0fde1cf --- /dev/null +++ b/lib/mstch @@ -0,0 +1 @@ +Subproject commit 0fde1cf94c26ede7fa267f4b64c0efe5da81a77a diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a975b85..ab82adc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -project(tawashi CXX) +project(tawashi VERSION 0.1.0 LANGUAGES CXX) find_package(Boost 1.53.0 REQUIRED COMPONENTS program_options filesystem system) find_package(CURL REQUIRED) @@ -36,6 +36,7 @@ configure_file( target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/include PRIVATE ${TAWASHI_SOURCE_ROOT}/lib/kakoune + PRIVATE ${TAWASHI_SOURCE_ROOT}/lib/mstch/include ) target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${Boost_INCLUDE_DIRS} @@ -48,6 +49,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE incredis PRIVATE ${CURL_LIBRARIES} PRIVATE ${SourceHighlight_LIBRARIES} + PRIVATE mstch ) target_compile_definitions(${PROJECT_NAME} PRIVATE BOOST_SPIRIT_USE_PHOENIX_V3=1 diff --git a/src/index_response.cpp b/src/index_response.cpp index 483f4f5..f1740f8 100644 --- a/src/index_response.cpp +++ b/src/index_response.cpp @@ -23,12 +23,5 @@ namespace tawashi { Response(Response::ContentType, "text/html", "index", parIni, false) { } - - void IndexResponse::on_send (std::ostream& parStream) { - std::string html(load_mustache()); - - boost::replace_all(html, "{base_uri}", base_uri()); - parStream << html; - } } //namespace tawashi diff --git a/src/index_response.hpp b/src/index_response.hpp index 717e89a..466735c 100644 --- a/src/index_response.hpp +++ b/src/index_response.hpp @@ -26,6 +26,5 @@ namespace tawashi { explicit IndexResponse (const IniFile& parIni); private: - virtual void on_send (std::ostream& parStream) override; }; } //namespace tawashi diff --git a/src/response.cpp b/src/response.cpp index e78d50c..9c247a4 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -18,6 +18,8 @@ #include "response.hpp" #include "incredis/incredis.hpp" #include "ini_file.hpp" +#include "tawashiConfig.h" +#include "duckhandy/stringize.h" #include #include #include @@ -74,11 +76,24 @@ namespace tawashi { void Response::on_process() { } + void Response::on_send (std::ostream& parStream) { + parStream << load_mustache(); + } + + void Response::on_mustache_prepare (mstch::map&) { + } + void Response::send() { + mstch::map mustache_context { + {"version", std::string{STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)}}, + {"base_uri", std::string(m_base_uri.data(), m_base_uri.size())} + }; + if (m_redis) m_redis->wait_for_connect(); this->on_process(); + this->on_mustache_prepare(mustache_context); m_header_sent = true; switch (m_resp_type) { @@ -90,8 +105,10 @@ namespace tawashi { break; } + std::ostringstream stream_out; if (ContentType == m_resp_type) - this->on_send(std::cout); + this->on_send(stream_out); + std::cout << mstch::render(stream_out.str(), mustache_context); std::cout.flush(); } diff --git a/src/response.hpp b/src/response.hpp index 1581279..21f2d8d 100644 --- a/src/response.hpp +++ b/src/response.hpp @@ -18,6 +18,7 @@ #pragma once #include "cgi_env.hpp" +#include "mstch/mstch.hpp" #include #include #include @@ -52,7 +53,8 @@ namespace tawashi { private: virtual void on_process(); - virtual void on_send (std::ostream& parStream) = 0; + virtual void on_send (std::ostream& parStream); + virtual void on_mustache_prepare (mstch::map& parContext); cgi::Env m_cgi_env; std::string m_resp_value; diff --git a/src/tawashiConfig.h.in b/src/tawashiConfig.h.in index a98295c..43f9e5a 100644 --- a/src/tawashiConfig.h.in +++ b/src/tawashiConfig.h.in @@ -20,3 +20,6 @@ #define TAWASHI_CONFIG_PATH "@TAWASHI_CONFIG_PATH@" #define TAWASHI_CONFIG_FILE "@TAWASHI_CONFIG_FILE@" #define TAWASHI_PATH_PREFIX "@CMAKE_INSTALL_PREFIX@" +#define VERSION_MAJOR @PROJECT_VERSION_MAJOR@ +#define VERSION_MINOR @PROJECT_VERSION_MINOR@ +#define VERSION_PATCH @PROJECT_VERSION_PATCH@