1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-27 21:35:41 +00:00

Response keeps a ptr to the SettingsBag now.

This commit is contained in:
King_DuckZ 2017-04-23 13:40:48 +01:00
parent 6c5497ae49
commit 75674525de
12 changed files with 27 additions and 21 deletions

View file

@ -19,7 +19,7 @@
#include <boost/algorithm/string/replace.hpp>
namespace tawashi {
IndexResponse::IndexResponse (const SettingsBag& parSettings) :
IndexResponse::IndexResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
Response(Response::ContentType, "text/html", "index", parSettings, false)
{
}

View file

@ -23,7 +23,7 @@
namespace tawashi {
class IndexResponse : public Response {
public:
explicit IndexResponse (const SettingsBag& parSettings);
explicit IndexResponse (const Kakoune::SafePtr<SettingsBag>& parSettings);
private:
};

View file

@ -52,7 +52,7 @@ namespace {
}
template <typename T>
std::unique_ptr<tawashi::Response> make_response (const tawashi::SettingsBag& parSettings) {
std::unique_ptr<tawashi::Response> make_response (const Kakoune::SafePtr<tawashi::SettingsBag>& parSettings) {
return static_cast<std::unique_ptr<tawashi::Response>>(std::make_unique<T>(parSettings));
}

View file

@ -17,13 +17,14 @@
#include "pastie_response.hpp"
#include "incredis/incredis.hpp"
#include "settings_bag.hpp"
#include <ciso646>
#include <srchilite/sourcehighlight.h>
#include <srchilite/langmap.h>
#include <sstream>
namespace tawashi {
PastieResponse::PastieResponse (const SettingsBag& parSettings) :
PastieResponse::PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
Response(Response::ContentType, "text/html", "", parSettings, true),
m_plain_text(false)
{

View file

@ -24,7 +24,7 @@
namespace tawashi {
class PastieResponse : public Response {
public:
explicit PastieResponse (const SettingsBag& parSettings);
explicit PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings);
private:
virtual void on_process() override;

View file

@ -99,18 +99,17 @@ namespace tawashi {
}
} //unnamed namespace
Response::Response (Types parRespType, std::string&& parValue, std::string&& parPageBaseName, const SettingsBag& parSettings, bool parWantRedis) :
Response::Response (Types parRespType, std::string&& parValue, std::string&& parPageBaseName, const Kakoune::SafePtr<SettingsBag>& parSettings, bool parWantRedis) :
m_resp_value(std::move(parValue)),
m_base_uri(parSettings["base_uri"]),
//m_page_basename(fetch_page_basename(m_cgi_env)),
m_redis_db(parSettings["redis_db"]),
m_website_root(make_root_path(parSettings)),
m_settings(parSettings),
m_website_root(make_root_path(*parSettings)),
m_page_basename(std::move(parPageBaseName)),
m_resp_type(parRespType),
m_header_sent(false)
{
if (parWantRedis) {
m_redis = std::make_unique<redis::IncRedis>(make_incredis(parSettings));
m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings));
m_redis->connect();
}
}
@ -128,16 +127,17 @@ namespace tawashi {
}
void Response::send() {
auto& base_uri = this->base_uri();
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())},
{"base_uri", std::string(base_uri.data(), base_uri.size())},
{"languages", make_mstch_langmap()}
};
if (m_redis) {
m_redis->wait_for_connect();
auto batch = m_redis->make_batch();
batch.select(dhandy::lexical_cast<int>(m_redis_db));
batch.select(dhandy::lexical_cast<int>((*m_settings)["redis_db"]));
batch.client_setname("tawashi_v" STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH));
batch.throw_if_failed();
}
@ -184,7 +184,7 @@ namespace tawashi {
}
const boost::string_ref& Response::base_uri() const {
return m_base_uri;
return (*m_settings)["base_uri"];
}
const std::string& Response::page_basename() const {

View file

@ -19,6 +19,7 @@
#include "cgi_env.hpp"
#include "mstch/mstch.hpp"
#include "kakoune/safe_ptr.hh"
#include <string>
#include <iostream>
#include <boost/utility/string_ref.hpp>
@ -43,7 +44,7 @@ namespace tawashi {
Location
};
Response (Types parRespType, std::string&& parValue, std::string&& parPageBaseName, const SettingsBag& parSettings, bool parWantRedis);
Response (Types parRespType, std::string&& parValue, std::string&& parPageBaseName, const Kakoune::SafePtr<SettingsBag>& parSettings, bool parWantRedis);
const cgi::Env& cgi_env() const;
void change_type (Types parRespType, std::string&& parValue);
const boost::string_ref& base_uri() const;
@ -58,8 +59,7 @@ namespace tawashi {
cgi::Env m_cgi_env;
std::string m_resp_value;
boost::string_ref m_base_uri;
boost::string_ref m_redis_db;
Kakoune::SafePtr<SettingsBag> m_settings;
std::string m_website_root;
std::string m_page_basename;
Types m_resp_type;

View file

@ -42,10 +42,10 @@ namespace tawashi {
std::unique_ptr<Response> ResponseFactory::make_response (const boost::string_ref& parName) {
auto maker_it = m_local_data->makers.find(std::string(parName.data(), parName.size()));
if (m_local_data->makers.end() != maker_it) {
return maker_it->second(*m_local_data->settings);
return maker_it->second(m_local_data->settings);
}
else if (m_local_data->jolly_maker) {
return m_local_data->jolly_maker(*m_local_data->settings);
return m_local_data->jolly_maker(m_local_data->settings);
}
else {
assert(false);

View file

@ -26,7 +26,7 @@ namespace tawashi {
class ResponseFactory {
public:
typedef std::function<std::unique_ptr<Response>(const SettingsBag&)> ResponseMakerFunc;
typedef std::function<std::unique_ptr<Response>(const Kakoune::SafePtr<SettingsBag>&)> ResponseMakerFunc;
explicit ResponseFactory (const Kakoune::SafePtr<SettingsBag>& parSettings);
~ResponseFactory() noexcept;

View file

@ -22,6 +22,7 @@
#include <map>
#include <boost/utility/string_ref.hpp>
#include <functional>
#include <string>
namespace tawashi {
class SettingsBag : public Kakoune::SafeCountable {
@ -38,4 +39,8 @@ namespace tawashi {
Kakoune::SafePtr<IniFile> m_ini;
const IniFile::KeyValueMapType* m_values;
};
inline std::string string_ref_to_string (const boost::string_ref& parIn) {
return std::string(parIn.data(), parIn.size());
}
} //namespace tawashi

View file

@ -27,7 +27,7 @@ namespace tawashi {
const char g_post_key[] = "pastie";
} //unnamed namespace
SubmitPasteResponse::SubmitPasteResponse (const SettingsBag& parSettings) :
SubmitPasteResponse::SubmitPasteResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
Response(Response::ContentType, "text/plain", "paste", parSettings, true)
{
}

View file

@ -24,7 +24,7 @@
namespace tawashi {
class SubmitPasteResponse : public Response {
public:
explicit SubmitPasteResponse (const SettingsBag& parSettings);
explicit SubmitPasteResponse (const Kakoune::SafePtr<SettingsBag>& parSettings);
private:
virtual void on_process() override;