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.
This commit is contained in:
King_DuckZ 2017-04-18 18:40:42 +01:00
parent 680f13e1f6
commit 1870829ec2
10 changed files with 39 additions and 11 deletions

3
.gitmodules vendored
View File

@ -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

View File

@ -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)

7
index.html.mstch Normal file
View File

@ -0,0 +1,7 @@
<p>tawashi v{{version}}</p>
<form action="{{base_uri}}/paste.cgi" method="POST" accept-charset="UTF-8">
<textarea name="pastie" cols="80" rows="24"></textarea>
<br>
<button type="submit">tawashi</button>
</br>
</form>

1
lib/mstch Submodule

@ -0,0 +1 @@
Subproject commit 0fde1cf94c26ede7fa267f4b64c0efe5da81a77a

View File

@ -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

View File

@ -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

View File

@ -26,6 +26,5 @@ namespace tawashi {
explicit IndexResponse (const IniFile& parIni);
private:
virtual void on_send (std::ostream& parStream) override;
};
} //namespace tawashi

View File

@ -18,6 +18,8 @@
#include "response.hpp"
#include "incredis/incredis.hpp"
#include "ini_file.hpp"
#include "tawashiConfig.h"
#include "duckhandy/stringize.h"
#include <utility>
#include <cassert>
#include <fstream>
@ -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();
}

View File

@ -18,6 +18,7 @@
#pragma once
#include "cgi_env.hpp"
#include "mstch/mstch.hpp"
#include <string>
#include <iostream>
#include <boost/utility/string_ref.hpp>
@ -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;

View File

@ -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@