mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-11-23 00:33:44 +00:00
Simplify the response pipeline.
There is no on_send() anymore, instead responses can override the way the top-level mustache file is retrieved.
This commit is contained in:
parent
298aedc633
commit
5377d25c72
6 changed files with 32 additions and 69 deletions
|
@ -51,49 +51,34 @@ namespace tawashi {
|
|||
using opt_string = redis::IncRedis::opt_string;
|
||||
using opt_string_list = redis::IncRedis::opt_string_list;
|
||||
|
||||
if (not m_plain_text) {
|
||||
call_on_send(false);
|
||||
|
||||
auto token = boost::string_ref(cgi_env().path_info()).substr(1);
|
||||
auto& redis = this->redis();
|
||||
opt_string_list pastie_reply = redis.hmget(token, "pastie");
|
||||
opt_string pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string());
|
||||
|
||||
srchilite::SourceHighlight highlighter;
|
||||
highlighter.setDataDir(settings().as<std::string>("langmap_dir"));
|
||||
highlighter.setGenerateEntireDoc(false);
|
||||
highlighter.setGenerateLineNumbers(true);
|
||||
const auto lang = m_lang_file;
|
||||
//Escapist houdini;
|
||||
//std::istringstream iss(houdini.escape_html(*pastie));
|
||||
std::istringstream iss(*pastie);
|
||||
|
||||
std::ostringstream oss;
|
||||
highlighter.highlight(iss, oss, lang);
|
||||
|
||||
parContext["pastie"] = oss.str();
|
||||
}
|
||||
}
|
||||
|
||||
void PastieResponse::on_send (std::ostream& parStream) {
|
||||
using opt_string = redis::IncRedis::opt_string;
|
||||
using opt_string_list = redis::IncRedis::opt_string_list;
|
||||
|
||||
if (cgi_env().path_info().empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert(m_plain_text);
|
||||
|
||||
auto token = boost::string_ref(cgi_env().path_info()).substr(1);
|
||||
auto& redis = this->redis();
|
||||
opt_string_list pastie_reply = redis.hmget(token, "pastie");
|
||||
opt_string pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string());
|
||||
|
||||
if (not pastie) {
|
||||
assert(false);
|
||||
}
|
||||
srchilite::SourceHighlight highlighter;
|
||||
highlighter.setDataDir(settings().as<std::string>("langmap_dir"));
|
||||
highlighter.setGenerateEntireDoc(false);
|
||||
highlighter.setGenerateLineNumbers(true);
|
||||
const auto lang = m_lang_file;
|
||||
//Escapist houdini;
|
||||
//std::istringstream iss(houdini.escape_html(*pastie));
|
||||
|
||||
parStream << *pastie;
|
||||
if (m_plain_text) {
|
||||
parContext["pastie"] = *pastie;
|
||||
}
|
||||
else {
|
||||
std::istringstream iss(*pastie);
|
||||
std::ostringstream oss;
|
||||
highlighter.highlight(iss, oss, lang);
|
||||
parContext["pastie"] = oss.str();
|
||||
}
|
||||
}
|
||||
|
||||
std::string PastieResponse::on_mustache_retrieve() {
|
||||
if (m_plain_text)
|
||||
return "{{pastie}}";
|
||||
else
|
||||
return load_mustache();
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -28,8 +28,8 @@ namespace tawashi {
|
|||
|
||||
private:
|
||||
virtual void on_process() override;
|
||||
virtual void on_send (std::ostream& parStream) override;
|
||||
virtual void on_mustache_prepare (mstch::map& parContext) override;
|
||||
virtual std::string on_mustache_retrieve() override;
|
||||
|
||||
std::string m_lang_file;
|
||||
std::string m_langmap_dir;
|
||||
|
|
|
@ -110,8 +110,7 @@ namespace tawashi {
|
|||
m_website_root(make_root_path(*parSettings)),
|
||||
m_page_basename(std::move(parPageBaseName)),
|
||||
m_resp_type(parRespType),
|
||||
m_header_sent(false),
|
||||
m_call_derived_on_send(true)
|
||||
m_header_sent(false)
|
||||
{
|
||||
if (parWantRedis) {
|
||||
m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings));
|
||||
|
@ -126,10 +125,6 @@ namespace tawashi {
|
|||
void Response::on_process() {
|
||||
}
|
||||
|
||||
void Response::on_send (std::ostream& parStream) {
|
||||
parStream << load_mustache();
|
||||
}
|
||||
|
||||
void Response::on_mustache_prepare (mstch::map&) {
|
||||
}
|
||||
|
||||
|
@ -161,16 +156,8 @@ namespace tawashi {
|
|||
break;
|
||||
}
|
||||
|
||||
std::ostringstream stream_out;
|
||||
if (ContentType == m_resp_type) {
|
||||
if (m_call_derived_on_send)
|
||||
this->on_send(stream_out);
|
||||
else
|
||||
Response::on_send(stream_out);
|
||||
}
|
||||
|
||||
std::cout << mstch::render(
|
||||
stream_out.str(),
|
||||
on_mustache_retrieve(),
|
||||
mustache_context,
|
||||
std::bind(
|
||||
&load_whole_file,
|
||||
|
@ -183,6 +170,10 @@ namespace tawashi {
|
|||
std::cout.flush();
|
||||
}
|
||||
|
||||
std::string Response::on_mustache_retrieve() {
|
||||
return load_mustache();
|
||||
}
|
||||
|
||||
const cgi::Env& Response::cgi_env() const {
|
||||
return m_cgi_env;
|
||||
}
|
||||
|
@ -216,8 +207,4 @@ namespace tawashi {
|
|||
assert(m_settings);
|
||||
return *m_settings;
|
||||
}
|
||||
|
||||
void Response::call_on_send (bool parCall) {
|
||||
m_call_derived_on_send = parCall;
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -49,15 +49,14 @@ namespace tawashi {
|
|||
void change_type (Types parRespType, std::string&& parValue);
|
||||
const boost::string_ref& base_uri() const;
|
||||
const std::string& page_basename() const;
|
||||
std::string load_mustache() const;
|
||||
redis::IncRedis& redis() const;
|
||||
const SettingsBag& settings() const;
|
||||
void call_on_send (bool parCall);
|
||||
virtual std::string load_mustache() const;
|
||||
|
||||
private:
|
||||
virtual void on_process();
|
||||
virtual void on_send (std::ostream& parStream);
|
||||
virtual void on_mustache_prepare (mstch::map& parContext);
|
||||
virtual std::string on_mustache_retrieve();
|
||||
|
||||
cgi::Env m_cgi_env;
|
||||
std::string m_resp_value;
|
||||
|
@ -67,6 +66,5 @@ namespace tawashi {
|
|||
Types m_resp_type;
|
||||
std::unique_ptr<redis::IncRedis> m_redis;
|
||||
bool m_header_sent;
|
||||
bool m_call_derived_on_send;
|
||||
};
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -108,12 +108,6 @@ namespace tawashi {
|
|||
}
|
||||
}
|
||||
|
||||
void SubmitPasteResponse::on_send (std::ostream& parStream) {
|
||||
assert(not m_error_message.empty());
|
||||
parStream << "something happened? :/\n" <<
|
||||
m_error_message << '\n';
|
||||
}
|
||||
|
||||
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const {
|
||||
auto& redis = this->redis();
|
||||
if (not redis.is_connected())
|
||||
|
|
|
@ -29,7 +29,6 @@ namespace tawashi {
|
|||
|
||||
private:
|
||||
virtual void on_process() override;
|
||||
virtual void on_send (std::ostream& parStream) override;
|
||||
boost::optional<std::string> submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const;
|
||||
|
||||
std::string m_error_message;
|
||||
|
|
Loading…
Reference in a new issue