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>
|
template <typename T>
|
||||||
std::unique_ptr<tawashi::Response> make_response (const Kakoune::SafePtr<tawashi::SettingsBag>& parSettings, const Kakoune::SafePtr<tawashi::cgi::Env>& parCgiEnv) {
|
std::unique_ptr<tawashi::Response> make_response (
|
||||||
return static_cast<std::unique_ptr<tawashi::Response>>(std::make_unique<T>(parSettings, &std::cout, parCgiEnv));
|
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) {
|
void fill_defaults (tawashi::SettingsBag& parSettings) {
|
||||||
|
|
|
@ -20,11 +20,12 @@
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
IndexResponse::IndexResponse (
|
IndexResponse::IndexResponse (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||||
) :
|
) :
|
||||||
Response(parSettings, parStreamOut, parCgiEnv, false)
|
Response(parFactory, parSettings, parStreamOut, parCgiEnv, false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -24,6 +24,7 @@ namespace tawashi {
|
||||||
class IndexResponse : public Response {
|
class IndexResponse : public Response {
|
||||||
public:
|
public:
|
||||||
IndexResponse (
|
IndexResponse (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||||
|
|
|
@ -31,11 +31,12 @@ namespace tawashi {
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
PastieResponse::PastieResponse (
|
PastieResponse::PastieResponse (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
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_langmap_dir(parSettings->as<std::string>("langmap_dir")),
|
||||||
m_plain_text(false),
|
m_plain_text(false),
|
||||||
m_syntax_highlight(true)
|
m_syntax_highlight(true)
|
||||||
|
|
|
@ -25,6 +25,7 @@ namespace tawashi {
|
||||||
class PastieResponse : public Response {
|
class PastieResponse : public Response {
|
||||||
public:
|
public:
|
||||||
PastieResponse (
|
PastieResponse (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include "pathname/pathname.hpp"
|
#include "pathname/pathname.hpp"
|
||||||
#include "list_highlight_langs.hpp"
|
#include "list_highlight_langs.hpp"
|
||||||
#include "cgi_env.hpp"
|
#include "cgi_env.hpp"
|
||||||
|
#include "response_factory.hpp"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -108,12 +109,14 @@ namespace tawashi {
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
Response::Response (
|
Response::Response (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
|
||||||
bool parWantRedis
|
bool parWantRedis
|
||||||
) :
|
) :
|
||||||
m_resp_value(g_def_response_type),
|
m_resp_value(g_def_response_type),
|
||||||
|
m_factory(parFactory),
|
||||||
//m_page_basename(fetch_page_basename(m_cgi_env)),
|
//m_page_basename(fetch_page_basename(m_cgi_env)),
|
||||||
m_cgi_env(parCgiEnv),
|
m_cgi_env(parCgiEnv),
|
||||||
m_settings(parSettings),
|
m_settings(parSettings),
|
||||||
|
@ -124,6 +127,7 @@ namespace tawashi {
|
||||||
{
|
{
|
||||||
assert(m_cgi_env);
|
assert(m_cgi_env);
|
||||||
assert(m_stream_out);
|
assert(m_stream_out);
|
||||||
|
assert(m_factory);
|
||||||
|
|
||||||
if (parWantRedis) {
|
if (parWantRedis) {
|
||||||
m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings));
|
m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings));
|
||||||
|
|
|
@ -30,6 +30,7 @@ namespace redis {
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
class SettingsBag;
|
class SettingsBag;
|
||||||
|
class ResponseFactory;
|
||||||
|
|
||||||
namespace cgi {
|
namespace cgi {
|
||||||
class Env;
|
class Env;
|
||||||
|
@ -48,6 +49,7 @@ namespace tawashi {
|
||||||
};
|
};
|
||||||
|
|
||||||
Response (
|
Response (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
|
||||||
|
@ -67,6 +69,7 @@ namespace tawashi {
|
||||||
virtual std::string on_mustache_retrieve();
|
virtual std::string on_mustache_retrieve();
|
||||||
|
|
||||||
std::string m_resp_value;
|
std::string m_resp_value;
|
||||||
|
Kakoune::SafePtr<ResponseFactory> m_factory;
|
||||||
Kakoune::SafePtr<cgi::Env> m_cgi_env;
|
Kakoune::SafePtr<cgi::Env> m_cgi_env;
|
||||||
Kakoune::SafePtr<SettingsBag> m_settings;
|
Kakoune::SafePtr<SettingsBag> m_settings;
|
||||||
std::string m_website_root;
|
std::string m_website_root;
|
||||||
|
|
|
@ -45,11 +45,12 @@ namespace tawashi {
|
||||||
//spdlog::get("statuslog")->info("making response object for \"{}\"", parName);
|
//spdlog::get("statuslog")->info("making response object for \"{}\"", parName);
|
||||||
|
|
||||||
auto maker_it = m_local_data->makers.find(std::string(parName.data(), parName.size()));
|
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) {
|
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) {
|
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 {
|
else {
|
||||||
assert(false);
|
assert(false);
|
||||||
|
|
|
@ -28,9 +28,13 @@ namespace tawashi {
|
||||||
class Env;
|
class Env;
|
||||||
} //namespace cgi
|
} //namespace cgi
|
||||||
|
|
||||||
class ResponseFactory {
|
class ResponseFactory : public Kakoune::SafeCountable {
|
||||||
public:
|
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);
|
explicit ResponseFactory (const Kakoune::SafePtr<SettingsBag>& parSettings, const Kakoune::SafePtr<cgi::Env>& parCgiEnv);
|
||||||
~ResponseFactory() noexcept;
|
~ResponseFactory() noexcept;
|
||||||
|
|
|
@ -90,11 +90,12 @@ namespace tawashi {
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
SubmitPasteResponse::SubmitPasteResponse (
|
SubmitPasteResponse::SubmitPasteResponse (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||||
) :
|
) :
|
||||||
Response(parSettings, parStreamOut, parCgiEnv, true)
|
Response(parFactory, parSettings, parStreamOut, parCgiEnv, true)
|
||||||
{
|
{
|
||||||
this->change_type(Response::ContentType, "text/plain");
|
this->change_type(Response::ContentType, "text/plain");
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ namespace tawashi {
|
||||||
class SubmitPasteResponse : public Response {
|
class SubmitPasteResponse : public Response {
|
||||||
public:
|
public:
|
||||||
SubmitPasteResponse (
|
SubmitPasteResponse (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include "ini_file.hpp"
|
#include "ini_file.hpp"
|
||||||
#include "settings_bag.hpp"
|
#include "settings_bag.hpp"
|
||||||
#include "safe_stack_object.hpp"
|
#include "safe_stack_object.hpp"
|
||||||
|
#include "response_factory.hpp"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -57,11 +58,12 @@ namespace tawashi {
|
||||||
class IndexResponseCustomMustache : public IndexResponse {
|
class IndexResponseCustomMustache : public IndexResponse {
|
||||||
public:
|
public:
|
||||||
IndexResponseCustomMustache (
|
IndexResponseCustomMustache (
|
||||||
|
const Kakoune::SafePtr<ResponseFactory>& parFactory,
|
||||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||||
std::ostream* parStreamOut,
|
std::ostream* parStreamOut,
|
||||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
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;
|
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.send();
|
||||||
|
|
||||||
response_stream.seekg(0);
|
response_stream.seekg(0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue