2017-04-15 02:18:33 +00:00
|
|
|
/* Copyright 2017, Michele Santullo
|
|
|
|
* This file is part of "tawashi".
|
|
|
|
*
|
|
|
|
* "tawashi" is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* "tawashi" is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with "tawashi". If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "response_factory.hpp"
|
2017-04-21 22:10:16 +00:00
|
|
|
#include "settings_bag.hpp"
|
2017-05-06 18:48:44 +00:00
|
|
|
#include "cgi_env.hpp"
|
2017-04-15 02:18:33 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <boost/container/flat_map.hpp>
|
|
|
|
|
|
|
|
namespace tawashi {
|
|
|
|
namespace {
|
|
|
|
} //unnamed namespace
|
|
|
|
|
|
|
|
struct ResponseFactory::LocalData {
|
2017-04-21 22:10:16 +00:00
|
|
|
Kakoune::SafePtr<SettingsBag> settings;
|
2017-04-15 02:18:33 +00:00
|
|
|
boost::container::flat_map<std::string, ResponseMakerFunc> makers;
|
|
|
|
ResponseMakerFunc jolly_maker;
|
2017-05-06 18:48:44 +00:00
|
|
|
Kakoune::SafePtr<cgi::Env> cgi_env;
|
2017-04-15 02:18:33 +00:00
|
|
|
};
|
|
|
|
|
2017-05-06 18:48:44 +00:00
|
|
|
ResponseFactory::ResponseFactory (const Kakoune::SafePtr<SettingsBag>& parSettings, const Kakoune::SafePtr<cgi::Env>& parCgiEnv) :
|
2017-04-15 02:18:33 +00:00
|
|
|
m_local_data(std::make_unique<LocalData>())
|
|
|
|
{
|
|
|
|
m_local_data->settings = parSettings;
|
2017-05-06 18:48:44 +00:00
|
|
|
m_local_data->cgi_env = parCgiEnv;
|
2017-04-15 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResponseFactory::~ResponseFactory() noexcept = default;
|
|
|
|
|
|
|
|
std::unique_ptr<Response> ResponseFactory::make_response (const boost::string_ref& parName) {
|
2017-05-04 09:00:49 +00:00
|
|
|
//spdlog::get("statuslog")->info("making response object for \"{}\"", parName);
|
|
|
|
|
2017-04-15 02:18:33 +00:00
|
|
|
auto maker_it = m_local_data->makers.find(std::string(parName.data(), parName.size()));
|
|
|
|
if (m_local_data->makers.end() != maker_it) {
|
2017-05-12 21:18:03 +00:00
|
|
|
return maker_it->second(m_local_data->settings, m_local_data->cgi_env);
|
2017-04-15 02:18:33 +00:00
|
|
|
}
|
|
|
|
else if (m_local_data->jolly_maker) {
|
2017-05-12 21:18:03 +00:00
|
|
|
return m_local_data->jolly_maker(m_local_data->settings, m_local_data->cgi_env);
|
2017-04-15 02:18:33 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(false);
|
|
|
|
return std::unique_ptr<Response>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResponseFactory::register_maker (std::string&& parName, ResponseMakerFunc parMaker) {
|
|
|
|
m_local_data->makers[std::move(parName)] = parMaker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResponseFactory::register_jolly_maker (ResponseMakerFunc parMaker) {
|
|
|
|
m_local_data->jolly_maker = parMaker;
|
|
|
|
}
|
|
|
|
} //namespace tawashi
|