1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-08-07 12:59:45 +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:
King_DuckZ 2017-04-26 09:17:57 +01:00
parent 298aedc633
commit 5377d25c72
6 changed files with 32 additions and 69 deletions

View file

@ -51,9 +51,6 @@ namespace tawashi {
using opt_string = redis::IncRedis::opt_string; using opt_string = redis::IncRedis::opt_string;
using opt_string_list = redis::IncRedis::opt_string_list; 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 token = boost::string_ref(cgi_env().path_info()).substr(1);
auto& redis = this->redis(); auto& redis = this->redis();
opt_string_list pastie_reply = redis.hmget(token, "pastie"); opt_string_list pastie_reply = redis.hmget(token, "pastie");
@ -66,34 +63,22 @@ namespace tawashi {
const auto lang = m_lang_file; const auto lang = m_lang_file;
//Escapist houdini; //Escapist houdini;
//std::istringstream iss(houdini.escape_html(*pastie)); //std::istringstream iss(houdini.escape_html(*pastie));
std::istringstream iss(*pastie);
if (m_plain_text) {
parContext["pastie"] = *pastie;
}
else {
std::istringstream iss(*pastie);
std::ostringstream oss; std::ostringstream oss;
highlighter.highlight(iss, oss, lang); highlighter.highlight(iss, oss, lang);
parContext["pastie"] = oss.str(); parContext["pastie"] = oss.str();
} }
} }
void PastieResponse::on_send (std::ostream& parStream) { std::string PastieResponse::on_mustache_retrieve() {
using opt_string = redis::IncRedis::opt_string; if (m_plain_text)
using opt_string_list = redis::IncRedis::opt_string_list; return "{{pastie}}";
else
if (cgi_env().path_info().empty()) { return load_mustache();
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);
}
parStream << *pastie;
} }
} //namespace tawashi } //namespace tawashi

View file

@ -28,8 +28,8 @@ namespace tawashi {
private: private:
virtual void on_process() override; virtual void on_process() override;
virtual void on_send (std::ostream& parStream) override;
virtual void on_mustache_prepare (mstch::map& parContext) 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_lang_file;
std::string m_langmap_dir; std::string m_langmap_dir;

View file

@ -110,8 +110,7 @@ namespace tawashi {
m_website_root(make_root_path(*parSettings)), m_website_root(make_root_path(*parSettings)),
m_page_basename(std::move(parPageBaseName)), m_page_basename(std::move(parPageBaseName)),
m_resp_type(parRespType), m_resp_type(parRespType),
m_header_sent(false), m_header_sent(false)
m_call_derived_on_send(true)
{ {
if (parWantRedis) { if (parWantRedis) {
m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings)); m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings));
@ -126,10 +125,6 @@ namespace tawashi {
void Response::on_process() { void Response::on_process() {
} }
void Response::on_send (std::ostream& parStream) {
parStream << load_mustache();
}
void Response::on_mustache_prepare (mstch::map&) { void Response::on_mustache_prepare (mstch::map&) {
} }
@ -161,16 +156,8 @@ namespace tawashi {
break; 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( std::cout << mstch::render(
stream_out.str(), on_mustache_retrieve(),
mustache_context, mustache_context,
std::bind( std::bind(
&load_whole_file, &load_whole_file,
@ -183,6 +170,10 @@ namespace tawashi {
std::cout.flush(); std::cout.flush();
} }
std::string Response::on_mustache_retrieve() {
return load_mustache();
}
const cgi::Env& Response::cgi_env() const { const cgi::Env& Response::cgi_env() const {
return m_cgi_env; return m_cgi_env;
} }
@ -216,8 +207,4 @@ namespace tawashi {
assert(m_settings); assert(m_settings);
return *m_settings; return *m_settings;
} }
void Response::call_on_send (bool parCall) {
m_call_derived_on_send = parCall;
}
} //namespace tawashi } //namespace tawashi

View file

@ -49,15 +49,14 @@ namespace tawashi {
void change_type (Types parRespType, std::string&& parValue); void change_type (Types parRespType, std::string&& parValue);
const boost::string_ref& base_uri() const; const boost::string_ref& base_uri() const;
const std::string& page_basename() const; const std::string& page_basename() const;
std::string load_mustache() const;
redis::IncRedis& redis() const; redis::IncRedis& redis() const;
const SettingsBag& settings() const; const SettingsBag& settings() const;
void call_on_send (bool parCall); virtual std::string load_mustache() const;
private: private:
virtual void on_process(); virtual void on_process();
virtual void on_send (std::ostream& parStream);
virtual void on_mustache_prepare (mstch::map& parContext); virtual void on_mustache_prepare (mstch::map& parContext);
virtual std::string on_mustache_retrieve();
cgi::Env m_cgi_env; cgi::Env m_cgi_env;
std::string m_resp_value; std::string m_resp_value;
@ -67,6 +66,5 @@ namespace tawashi {
Types m_resp_type; Types m_resp_type;
std::unique_ptr<redis::IncRedis> m_redis; std::unique_ptr<redis::IncRedis> m_redis;
bool m_header_sent; bool m_header_sent;
bool m_call_derived_on_send;
}; };
} //namespace tawashi } //namespace tawashi

View file

@ -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 { 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(); auto& redis = this->redis();
if (not redis.is_connected()) if (not redis.is_connected())

View file

@ -29,7 +29,6 @@ namespace tawashi {
private: private:
virtual void on_process() override; 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; 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; std::string m_error_message;