2017-04-06 23:35:06 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
2017-04-11 08:30:22 +01:00
|
|
|
#include "tawashiConfig.h"
|
2017-04-06 22:45:44 +01:00
|
|
|
#include "submit_paste_response.hpp"
|
2017-04-06 23:28:57 +01:00
|
|
|
#include "pastie_response.hpp"
|
2017-04-06 21:27:38 +01:00
|
|
|
#include "index_response.hpp"
|
2017-05-12 22:46:54 +01:00
|
|
|
#include "error_response.hpp"
|
2017-04-15 03:18:33 +01:00
|
|
|
#include "response_factory.hpp"
|
2017-04-05 08:41:49 +01:00
|
|
|
#include "cgi_env.hpp"
|
2017-04-11 18:19:19 +01:00
|
|
|
#include "ini_file.hpp"
|
2017-04-15 03:18:33 +01:00
|
|
|
#include "safe_stack_object.hpp"
|
2017-04-11 18:19:19 +01:00
|
|
|
#include "pathname/pathname.hpp"
|
|
|
|
#include "duckhandy/compatibility.h"
|
2017-04-21 23:10:16 +01:00
|
|
|
#include "settings_bag.hpp"
|
2017-05-04 09:59:49 +01:00
|
|
|
#include "logging_levels.hpp"
|
2017-05-03 09:31:41 +01:00
|
|
|
#include <spdlog/spdlog.h>
|
2017-04-04 20:58:40 +01:00
|
|
|
#include <string>
|
2017-04-11 18:19:19 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <ciso646>
|
2017-05-06 19:48:44 +01:00
|
|
|
#include <iostream>
|
2017-04-04 20:58:40 +01:00
|
|
|
|
|
|
|
//www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4150.pdf
|
|
|
|
|
|
|
|
namespace {
|
2017-04-11 18:19:19 +01:00
|
|
|
std::string config_file_path() a_pure;
|
|
|
|
|
|
|
|
std::string config_file_path() {
|
|
|
|
mchlib::PathName config_path(TAWASHI_CONFIG_PATH);
|
|
|
|
mchlib::PathName full_path("");
|
|
|
|
if (config_path.is_absolute()) {
|
|
|
|
full_path = std::move(config_path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
full_path = mchlib::PathName(TAWASHI_PATH_PREFIX);
|
|
|
|
full_path.join(config_path);
|
|
|
|
}
|
|
|
|
full_path.join(TAWASHI_CONFIG_FILE);
|
|
|
|
return full_path.path();
|
|
|
|
}
|
2017-04-14 00:50:50 +01:00
|
|
|
|
2017-04-15 03:18:33 +01:00
|
|
|
template <typename T>
|
2017-05-12 22:46:54 +01:00
|
|
|
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)
|
|
|
|
);
|
2017-04-21 23:10:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void fill_defaults (tawashi::SettingsBag& parSettings) {
|
|
|
|
parSettings.add_default("redis_server", "127.0.0.1");
|
|
|
|
parSettings.add_default("redis_port", "6379");
|
|
|
|
parSettings.add_default("redis_mode", "sock");
|
|
|
|
parSettings.add_default("redis_sock", "/tmp/redis.sock");
|
2017-04-21 23:26:01 +01:00
|
|
|
parSettings.add_default("redis_db", "0");
|
2017-04-21 23:10:16 +01:00
|
|
|
parSettings.add_default("base_uri", "http://127.0.0.1");
|
2017-04-23 13:58:06 +01:00
|
|
|
parSettings.add_default("website_root", "");
|
2017-04-23 13:57:49 +01:00
|
|
|
parSettings.add_default("langmap_dir", "/usr/share/source-highlight");
|
2017-04-24 19:09:43 +01:00
|
|
|
parSettings.add_default("min_pastie_size", "10");
|
|
|
|
parSettings.add_default("max_pastie_size", "10000");
|
|
|
|
parSettings.add_default("truncate_long_pasties", "false");
|
2017-05-04 09:59:49 +01:00
|
|
|
parSettings.add_default("logging_level", "err");
|
2017-05-11 18:50:56 +01:00
|
|
|
parSettings.add_default("resubmit_wait", "10");
|
2017-04-14 00:50:50 +01:00
|
|
|
}
|
2017-05-15 09:26:29 +01:00
|
|
|
|
|
|
|
void print_buildtime_info() {
|
|
|
|
std::cout << "NDEBUG defined: ";
|
|
|
|
#if defined(NDEBUG)
|
|
|
|
std::cout << "yes (Release build)";
|
|
|
|
#else
|
|
|
|
std::cout << "no (Debug build)";
|
|
|
|
#endif
|
|
|
|
std::cout << '\n';
|
|
|
|
std::cout << "TAWASHI_CONFIG_FILE: \"" << TAWASHI_CONFIG_FILE << "\"\n";
|
|
|
|
std::cout << "TAWASHI_CONFIG_PATH: \"" << TAWASHI_CONFIG_PATH << "\"\n";
|
|
|
|
std::cout << "TAWASHI_PATH_PREFIX: \"" << TAWASHI_PATH_PREFIX << "\"\n";
|
|
|
|
std::cout << "VERSION_MAJOR: " << VERSION_MAJOR << '\n';
|
|
|
|
std::cout << "VERSION_MINOR: " << VERSION_MINOR << '\n';
|
|
|
|
std::cout << "VERSION_PATCH: " << VERSION_PATCH << '\n';
|
|
|
|
std::cout << "config_file_path(): \"" << config_file_path() << "\"\n";
|
|
|
|
}
|
2017-05-16 18:52:34 +01:00
|
|
|
|
|
|
|
curry::SafeStackObject<tawashi::IniFile> load_ini() {
|
|
|
|
using curry::SafeStackObject;
|
|
|
|
using tawashi::IniFile;
|
|
|
|
using std::istream_iterator;
|
|
|
|
|
|
|
|
std::ifstream conf(config_file_path());
|
|
|
|
conf >> std::noskipws;
|
|
|
|
return SafeStackObject<IniFile>(istream_iterator<char>(conf), istream_iterator<char>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_logging_level (const tawashi::SettingsBag& parSettings) {
|
|
|
|
auto logging_level = tawashi::LoggingLevels::_from_string_nocase(parSettings.as<std::string>("logging_level").c_str());
|
|
|
|
spdlog::set_level(static_cast<decltype(spdlog::level::trace)>(logging_level._to_integral()));
|
|
|
|
}
|
2017-04-04 20:58:40 +01:00
|
|
|
} //unnamed namespace
|
|
|
|
|
2017-05-08 19:46:14 +01:00
|
|
|
int main (int parArgc, char* parArgv[], char* parEnvp[]) {
|
2017-04-15 03:18:33 +01:00
|
|
|
using curry::SafeStackObject;
|
|
|
|
using tawashi::IndexResponse;
|
|
|
|
using tawashi::SubmitPasteResponse;
|
|
|
|
using tawashi::PastieResponse;
|
2017-05-12 22:46:54 +01:00
|
|
|
using tawashi::ErrorResponse;
|
2017-04-15 03:18:33 +01:00
|
|
|
using tawashi::Response;
|
|
|
|
|
2017-05-15 09:26:29 +01:00
|
|
|
if (2 == parArgc and boost::string_ref(parArgv[1]) == "--show-paths") {
|
|
|
|
print_buildtime_info();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-03 09:31:41 +01:00
|
|
|
//Prepare the logger
|
|
|
|
spdlog::set_pattern("[%Y-%m-%d %T %z] - %v");
|
2017-05-04 09:59:49 +01:00
|
|
|
spdlog::set_level(spdlog::level::trace); //set to maximum possible here
|
2017-05-03 09:31:41 +01:00
|
|
|
auto statuslog = spdlog::stderr_logger_st("statuslog");
|
|
|
|
|
2017-05-04 10:00:49 +01:00
|
|
|
statuslog->info("Loading config: \"{}\"", config_file_path());
|
2017-05-03 09:31:41 +01:00
|
|
|
|
2017-05-16 18:52:34 +01:00
|
|
|
SafeStackObject<tawashi::IniFile> ini = load_ini();
|
2017-04-21 23:10:16 +01:00
|
|
|
auto settings = SafeStackObject<tawashi::SettingsBag>(ini);
|
|
|
|
fill_defaults(*settings);
|
|
|
|
|
2017-05-16 18:52:34 +01:00
|
|
|
set_logging_level(*settings);
|
2017-05-04 09:59:49 +01:00
|
|
|
|
2017-05-08 19:46:14 +01:00
|
|
|
auto cgi_env = SafeStackObject<tawashi::cgi::Env>(parEnvp);
|
2017-05-06 19:48:44 +01:00
|
|
|
tawashi::ResponseFactory resp_factory(settings, cgi_env);
|
2017-05-04 10:00:49 +01:00
|
|
|
SPDLOG_TRACE(statuslog, "Registering makers in the response factory");
|
2017-04-15 03:18:33 +01:00
|
|
|
resp_factory.register_maker("index.cgi", &make_response<IndexResponse>);
|
|
|
|
resp_factory.register_maker("", &make_response<IndexResponse>);
|
|
|
|
resp_factory.register_maker("paste.cgi", &make_response<SubmitPasteResponse>);
|
2017-05-12 22:46:54 +01:00
|
|
|
resp_factory.register_maker("error.cgi", &make_response<ErrorResponse>);
|
2017-04-15 03:18:33 +01:00
|
|
|
resp_factory.register_jolly_maker(&make_response<PastieResponse>);
|
|
|
|
|
2017-05-06 19:48:44 +01:00
|
|
|
std::unique_ptr<Response> response = resp_factory.make_response(cgi_env->path_info().substr(1));
|
2017-04-15 03:18:33 +01:00
|
|
|
response->send();
|
2017-04-04 20:58:40 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|