mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-02-09 09:23:56 +00:00
Pass the ResponseFactory object down to the Response itself.
This commit is contained in:
parent
10da75caf5
commit
3bc6c56c03
12 changed files with 38 additions and 11 deletions
|
@ -54,8 +54,14 @@ namespace {
|
|||
}
|
||||
|
||||
template <typename T>
|
||||
std::unique_ptr<tawashi::Response> make_response (const Kakoune::SafePtr<tawashi::SettingsBag>& parSettings, const Kakoune::SafePtr<tawashi::cgi::Env>& parCgiEnv) {
|
||||
return static_cast<std::unique_ptr<tawashi::Response>>(std::make_unique<T>(parSettings, &std::cout, parCgiEnv));
|
||||
std::unique_ptr<tawashi::Response> make_response (
|
||||
const Kakoune::SafePtr<tawashi::ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<tawashi::SettingsBag>& parSettings,
|
||||
const Kakoune::SafePtr<tawashi::cgi::Env>& parCgiEnv
|
||||
) {
|
||||
return static_cast<std::unique_ptr<tawashi::Response>>(
|
||||
std::make_unique<T>(parFactory, parSettings, &std::cout, parCgiEnv)
|
||||
);
|
||||
}
|
||||
|
||||
void fill_defaults (tawashi::SettingsBag& parSettings) {
|
||||
|
|
|
@ -20,11 +20,12 @@
|
|||
|
||||
namespace tawashi {
|
||||
IndexResponse::IndexResponse (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
) :
|
||||
Response(parSettings, parStreamOut, parCgiEnv, false)
|
||||
Response(parFactory, parSettings, parStreamOut, parCgiEnv, false)
|
||||
{
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -24,6 +24,7 @@ namespace tawashi {
|
|||
class IndexResponse : public Response {
|
||||
public:
|
||||
IndexResponse (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
|
|
|
@ -31,11 +31,12 @@ namespace tawashi {
|
|||
} //unnamed namespace
|
||||
|
||||
PastieResponse::PastieResponse (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
) :
|
||||
Response(parSettings, parStreamOut, parCgiEnv, true),
|
||||
Response(parFactory, parSettings, parStreamOut, parCgiEnv, true),
|
||||
m_langmap_dir(parSettings->as<std::string>("langmap_dir")),
|
||||
m_plain_text(false),
|
||||
m_syntax_highlight(true)
|
||||
|
|
|
@ -25,6 +25,7 @@ namespace tawashi {
|
|||
class PastieResponse : public Response {
|
||||
public:
|
||||
PastieResponse (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "pathname/pathname.hpp"
|
||||
#include "list_highlight_langs.hpp"
|
||||
#include "cgi_env.hpp"
|
||||
#include "response_factory.hpp"
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
|
@ -108,12 +109,14 @@ namespace tawashi {
|
|||
} //unnamed namespace
|
||||
|
||||
Response::Response (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
|
||||
bool parWantRedis
|
||||
) :
|
||||
m_resp_value(g_def_response_type),
|
||||
m_factory(parFactory),
|
||||
//m_page_basename(fetch_page_basename(m_cgi_env)),
|
||||
m_cgi_env(parCgiEnv),
|
||||
m_settings(parSettings),
|
||||
|
@ -124,6 +127,7 @@ namespace tawashi {
|
|||
{
|
||||
assert(m_cgi_env);
|
||||
assert(m_stream_out);
|
||||
assert(m_factory);
|
||||
|
||||
if (parWantRedis) {
|
||||
m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings));
|
||||
|
|
|
@ -30,6 +30,7 @@ namespace redis {
|
|||
|
||||
namespace tawashi {
|
||||
class SettingsBag;
|
||||
class ResponseFactory;
|
||||
|
||||
namespace cgi {
|
||||
class Env;
|
||||
|
@ -48,6 +49,7 @@ namespace tawashi {
|
|||
};
|
||||
|
||||
Response (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
|
||||
|
@ -67,6 +69,7 @@ namespace tawashi {
|
|||
virtual std::string on_mustache_retrieve();
|
||||
|
||||
std::string m_resp_value;
|
||||
Kakoune::SafePtr<ResponseFactory> m_factory;
|
||||
Kakoune::SafePtr<cgi::Env> m_cgi_env;
|
||||
Kakoune::SafePtr<SettingsBag> m_settings;
|
||||
std::string m_website_root;
|
||||
|
|
|
@ -45,11 +45,12 @@ namespace tawashi {
|
|||
//spdlog::get("statuslog")->info("making response object for \"{}\"", parName);
|
||||
|
||||
auto maker_it = m_local_data->makers.find(std::string(parName.data(), parName.size()));
|
||||
Kakoune::SafePtr<ResponseFactory> self(this);
|
||||
if (m_local_data->makers.end() != maker_it) {
|
||||
return maker_it->second(m_local_data->settings, m_local_data->cgi_env);
|
||||
return maker_it->second(self, m_local_data->settings, m_local_data->cgi_env);
|
||||
}
|
||||
else if (m_local_data->jolly_maker) {
|
||||
return m_local_data->jolly_maker(m_local_data->settings, m_local_data->cgi_env);
|
||||
return m_local_data->jolly_maker(self, m_local_data->settings, m_local_data->cgi_env);
|
||||
}
|
||||
else {
|
||||
assert(false);
|
||||
|
|
|
@ -28,9 +28,13 @@ namespace tawashi {
|
|||
class Env;
|
||||
} //namespace cgi
|
||||
|
||||
class ResponseFactory {
|
||||
class ResponseFactory : public Kakoune::SafeCountable {
|
||||
public:
|
||||
typedef std::function<std::unique_ptr<Response>(const Kakoune::SafePtr<SettingsBag>&, const Kakoune::SafePtr<cgi::Env>& parCgiEnv)> ResponseMakerFunc;
|
||||
typedef std::function<std::unique_ptr<Response>(
|
||||
const Kakoune::SafePtr<ResponseFactory>&,
|
||||
const Kakoune::SafePtr<SettingsBag>&,
|
||||
const Kakoune::SafePtr<cgi::Env>&
|
||||
)> ResponseMakerFunc;
|
||||
|
||||
explicit ResponseFactory (const Kakoune::SafePtr<SettingsBag>& parSettings, const Kakoune::SafePtr<cgi::Env>& parCgiEnv);
|
||||
~ResponseFactory() noexcept;
|
||||
|
|
|
@ -90,11 +90,12 @@ namespace tawashi {
|
|||
} //unnamed namespace
|
||||
|
||||
SubmitPasteResponse::SubmitPasteResponse (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
) :
|
||||
Response(parSettings, parStreamOut, parCgiEnv, true)
|
||||
Response(parFactory, parSettings, parStreamOut, parCgiEnv, true)
|
||||
{
|
||||
this->change_type(Response::ContentType, "text/plain");
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ namespace tawashi {
|
|||
class SubmitPasteResponse : public Response {
|
||||
public:
|
||||
SubmitPasteResponse (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "ini_file.hpp"
|
||||
#include "settings_bag.hpp"
|
||||
#include "safe_stack_object.hpp"
|
||||
#include "response_factory.hpp"
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
@ -57,11 +58,12 @@ namespace tawashi {
|
|||
class IndexResponseCustomMustache : public IndexResponse {
|
||||
public:
|
||||
IndexResponseCustomMustache (
|
||||
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
) :
|
||||
IndexResponse(parSettings, parStreamOut, parCgiEnv)
|
||||
IndexResponse(parFactory, parSettings, parStreamOut, parCgiEnv)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -106,7 +108,8 @@ TEST_CASE ("Index response", "[index][response]") {
|
|||
|
||||
std::stringstream response_stream;
|
||||
|
||||
tawashi::IndexResponseCustomMustache response(settings, &response_stream, fake_env);
|
||||
SafeStackObject<tawashi::ResponseFactory> resp_factory(settings, fake_env);
|
||||
tawashi::IndexResponseCustomMustache response(resp_factory, settings, &response_stream, fake_env);
|
||||
response.send();
|
||||
|
||||
response_stream.seekg(0);
|
||||
|
|
Loading…
Add table
Reference in a new issue