Begin removing several dependencies on Config object

This commit is contained in:
Albert S. 2019-09-29 17:12:36 +02:00
parent 3b312c7d9e
commit 083f7ff842
9 changed files with 135 additions and 85 deletions

View File

@ -75,27 +75,27 @@ Config::Config(const std::map<std::string, std::string> &map)
this->wikiname = required("wikiname"); this->wikiname = required("wikiname");
this->logfile = required("logfile"); this->logfile = required("logfile");
this->templatepath = required("templatepath"); this->templatepath = required("templatepath");
this->linkallcats = required("linkallcats"); this->urls.linkallcats = required("linkallcats");
this->linkallpages = required("linkallpages"); this->urls.linkallpages = required("linkallpages");
this->linkcategory = required("linkcategory"); this->urls.linkcategory = required("linkcategory");
this->linkdelete = required("linkdelete"); this->urls.linkdelete = required("linkdelete");
this->linkedit = required("linkedit"); this->urls.linkedit = required("linkedit");
this->linkhistory = required("linkhistory"); this->urls.linkhistory = required("linkhistory");
this->linkindex = required("linkindex"); this->urls.linkindex = required("linkindex");
this->linklogout = required("linklogout"); this->urls.linklogout = required("linklogout");
this->linkpage = required("linkpage"); this->urls.linkpage = required("linkpage");
this->linkrecent = required("linkrecent"); this->urls.linkrecent = required("linkrecent");
this->linkrevision = required("linkrevision"); this->urls.linkrevision = required("linkrevision");
this->linksettings = required("linksettings"); this->urls.linksettings = required("linksettings");
this->linkshere = required("linkshere"); this->urls.linkshere = required("linkshere");
this->loginurl = required("loginurl"); this->urls.loginurl = required("loginurl");
this->linkrecentsort = required("linkrecentsort"); this->urls.linkrecentsort = required("linkrecentsort");
this->linkhistorysort = required("linkhistorysort"); this->urls.linkhistorysort = required("linkhistorysort");
this->actionurl = required("actionurl"); this->urls.actionurl = required("actionurl");
this->settingsurl = required("settingsurl"); this->urls.settingsurl = required("settingsurl");
this->deletionurl = required("deletionurl"); this->urls.deletionurl = required("deletionurl");
this->adminregisterurl = required("adminregisterurl"); this->urls.adminregisterurl = required("adminregisterurl");
this->userchangepwurl = required("userchangepwurl"); this->urls.userchangepwurl = required("userchangepwurl");
this->connectionstring = required("connectionstring"); this->connectionstring = required("connectionstring");
@ -110,6 +110,8 @@ Config::Config(const std::map<std::string, std::string> &map)
this->max_payload_length = optional("max_payload_length", 10 *1024*1024); this->max_payload_length = optional("max_payload_length", 10 *1024*1024);
ConfigVariableResolver resolver { this->configmap };
this->configVarResolver = resolver;
} }

View File

@ -1,4 +1,4 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
#include <string> #include <string>
#include <fstream> #include <fstream>
@ -6,31 +6,23 @@
#include <map> #include <map>
#include "permissions.h" #include "permissions.h"
#include "utils.h" #include "utils.h"
class Config
class WikiGeneralConfig
{ {
private:
std::map<std::string, std::string> configmap;
std::string required(const std::string &key);
std::string optional(const std::string &key, std::string defaultvalue = "");
int optional(const std::string &key, int defaulvalue);
uint64_t optional(const std::string &key, uint64_t defaultvalue);
public:
Config(const std::map<std::string, std::string> &map );
//TODO: these could be references!?
std::string wikiname; std::string wikiname;
std::string wikipath;
std::string templatepath;
std::string templateprefix;
std::string logfile;
std::string anon_username; std::string anon_username;
std::string linkindex ; int max_pagename_length;
std::string linkrecent ; int query_limit;
std::string linkallpages ; };
std::string linkallcats ;
std::string linkshere ; struct ConfigUrls
std::string linkpage ; {
std::string linkindex;
std::string linkrecent;
std::string linkallpages;
std::string linkallcats;
std::string linkshere;
std::string linkpage;
std::string linkrevision ; std::string linkrevision ;
std::string linkhistory ; std::string linkhistory ;
std::string linkedit ; std::string linkedit ;
@ -46,6 +38,56 @@ public:
std::string linkhistorysort; std::string linkhistorysort;
std::string adminregisterurl; std::string adminregisterurl;
std::string userchangepwurl; std::string userchangepwurl;
};
class ConfigVariableResolver
{
private:
const std::map<std::string, std::string> *configmap;
public:
ConfigVariableResolver()
{
}
ConfigVariableResolver(const std::map<std::string, std::string> &configmap)
{
this->configmap = &configmap;
}
std::string getConfig(const std::string &key) const
{
return utils::getKeyOrEmpty(*configmap, key);
}
};
class Config
{
private:
std::map<std::string, std::string> configmap;
std::string required(const std::string &key);
std::string optional(const std::string &key, std::string defaultvalue = "");
int optional(const std::string &key, int defaulvalue);
uint64_t optional(const std::string &key, uint64_t defaultvalue);
public:
ConfigUrls urls;
ConfigVariableResolver configVarResolver;
Config(const std::map<std::string, std::string> &map );
//TODO: these could be references!?
std::string wikiname;
std::string wikipath;
std::string templatepath;
std::string templateprefix;
std::string logfile;
std::string anon_username;
std::string connectionstring; std::string connectionstring;
int query_limit; int query_limit;
@ -57,10 +99,7 @@ public:
Permissions anon_permissions; Permissions anon_permissions;
std::string getConfig(const std::string &key) const
{
return utils::getKeyOrEmpty(configmap, key);
}
}; };

View File

@ -24,6 +24,18 @@ SOFTWARE.
std::unique_ptr<GatewayInterface> createGateway(const Config &c) std::unique_ptr<GatewayInterface> createGateway(const Config &c)
{ {
return std::make_unique<HttpGateway>(c); std::string listenaddr = c.configVarResolver.getConfig("http.listenaddr");
if(listenaddr.empty())
{
throw new std::runtime_error("No http.listenaddr in config file");
}
std::string listenport = c.configVarResolver.getConfig("http.listenport");
if(listenport.empty())
{
throw new std::runtime_error("No http.listenport in config file");
}
return std::make_unique<HttpGateway>(listenaddr, std::stoi(listenport), c.max_payload_length);
} }

View File

@ -20,21 +20,11 @@ SOFTWARE.
*/ */
#include "httpgateway.h" #include "httpgateway.h"
#include "../logger.h" #include "../logger.h"
HttpGateway::HttpGateway(const Config &config) HttpGateway::HttpGateway(std::string listenaddr, int port, uint64_t maxPayloadLength)
{ {
this->listenaddr = config.getConfig("http.listenaddr"); this->listenaddr = listenaddr;
if(this->listenaddr.empty()) this->listenport = port;
{ this->maxPayloadLength = maxPayloadLength;
throw new std::runtime_error("No http.listenaddr in config file");
}
std::string listenport = config.getConfig("http.listenport");
if(listenport.empty())
{
throw new std::runtime_error("No http.listenport in config file");
}
this->listenport = std::stoi(listenport);
this->maxPayloadLength = config.max_payload_length;
} }
bool HttpGateway::keepReading() bool HttpGateway::keepReading()

View File

@ -18,7 +18,7 @@ private:
int listenport; int listenport;
uint64_t maxPayloadLength; uint64_t maxPayloadLength;
public: public:
HttpGateway(const Config &config); HttpGateway(std::string listenaddr, int port, uint64_t maxPayloadLength);
bool keepReading() override; bool keepReading() override;
void work(RequestWorker &worker) override; void work(RequestWorker &worker) override;
}; };

View File

@ -57,12 +57,12 @@ void setup_signal_handlers()
} }
} }
std::unique_ptr<ICache> createCache(const Config &config) std::unique_ptr<ICache> createCache(const ConfigVariableResolver &resolver)
{ {
std::string path = config.getConfig("cache_fs_dir"); std::string path = resolver.getConfig("cache_fs_dir");
return std::make_unique<FsCache>(config.getConfig("cache_fs_dir")); return std::make_unique<FsCache>(path);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -99,7 +99,7 @@ int main(int argc, char **argv)
if(!sandbox->enablePreWorker({ if(!sandbox->enablePreWorker({
config.getConfig("cache_fs_dir"), config.configVarResolver.getConfig("cache_fs_dir"),
config.templatepath, config.templatepath,
std::filesystem::path(config.logfile).parent_path(), std::filesystem::path(config.logfile).parent_path(),
std::filesystem::path(config.connectionstring).parent_path(), std::filesystem::path(config.connectionstring).parent_path(),
@ -132,13 +132,13 @@ int main(int argc, char **argv)
userdao->save(anon.value()); userdao->save(anon.value());
User::setAnon(anon.value()); User::setAnon(anon.value());
Template siteTemplate { config }; Template siteTemplate { config.templateprefix, config.templatepath, config.urls, config.configVarResolver };
UrlProvider urlprovider { config }; UrlProvider urlprovider { config.urls };
auto cache = createCache(config); auto cache = createCache(config.configVarResolver);
cache->clear(); cache->clear();
RequestWorker requestWorker (*database, siteTemplate, urlprovider, *cache ); RequestWorker requestWorker (*database, siteTemplate, urlprovider, *cache );

View File

@ -23,22 +23,24 @@ SOFTWARE.
#include "urlprovider.h" #include "urlprovider.h"
#include "htmllink.h" #include "htmllink.h"
#include "logger.h" #include "logger.h"
Template::Template(const Config &config) Template::Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls, ConfigVariableResolver &configVarsResolver)
{ {
this->config = &config; this->templateprefix = templateprefix;
this->templatepath = templatepath;
this->configUrls = &configUrls;
this->configVarResolver = &configVarsResolver;
} }
std::string Template::getPartPath(std::string_view partname) std::string Template::getPartPath(std::string_view partname)
{ {
//TODO: utils::concatPath? C++17 paths? //TODO: utils::concatPath? C++17 paths?
return this->config->templatepath + "/" + std::string(partname); return this->templatepath + "/" + std::string(partname);
} }
std::string Template::loadPartContent(std::string_view partname) std::string Template::loadPartContent(std::string_view partname)
{ {
std::string partpath = getPartPath(partname); std::string partpath = getPartPath(partname);
return utils::readCompleteFile(partpath); return utils::readCompleteFile(partpath);
} }
std::string Template::loadResolvedPart(std::string_view partname) std::string Template::loadResolvedPart(std::string_view partname)
{ {
return resolveIncludes(loadPartContent(partname)); return resolveIncludes(loadPartContent(partname));
@ -47,7 +49,7 @@ std::string Template::loadResolvedPart(std::string_view partname)
std::string Template::resolveIncludes(std::string_view content) std::string Template::resolveIncludes(std::string_view content)
{ {
Varreplacer replacer(this->config->templateprefix); Varreplacer replacer(this->templateprefix);
replacer.addResolver("include", [&](std::string_view key) { return loadResolvedPart(key); }); replacer.addResolver("include", [&](std::string_view key) { return loadResolvedPart(key); });
return replacer.parse(content); return replacer.parse(content);
} }
@ -55,8 +57,8 @@ std::string Template::resolveIncludes(std::string_view content)
TemplatePage Template::createPage(std::string name) TemplatePage Template::createPage(std::string name)
{ {
std::string content = loadResolvedPart(name); std::string content = loadResolvedPart(name);
Varreplacer replacer(this->config->templateprefix); Varreplacer replacer(this->templateprefix);
replacer.addResolver("config", [&](std::string_view key) { return this->config->getConfig(std::string(key)); }); replacer.addResolver("config", [&](std::string_view key) { return this->configVarResolver->getConfig(std::string(key)); });
//TODO: Varreplacer is not recursive, but since includes might add new vars, it may not be this bad anyway. //TODO: Varreplacer is not recursive, but since includes might add new vars, it may not be this bad anyway.
// //
return TemplatePage(replacer.parse(content)); return TemplatePage(replacer.parse(content));
@ -99,13 +101,13 @@ std::string Template::renderSearch(const std::vector<std::string> &results, std:
} }
std::string Template::renderSearch(const std::vector<std::string> &results) const std::string Template::renderSearch(const std::vector<std::string> &results) const
{ {
UrlProvider urlprovider(*this->config); UrlProvider urlprovider(*this->configUrls);
return renderSearch(results, [&urlprovider](std::string s) { return urlprovider.page(s);}); return renderSearch(results, [&urlprovider](std::string s) { return urlprovider.page(s);});
} }
std::string Template::renderSearch(const std::vector<SearchResult> &results) const std::string Template::renderSearch(const std::vector<SearchResult> &results) const
{ {
UrlProvider urlprovider(*this->config); UrlProvider urlprovider(*this->configUrls);
HtmlLink link; HtmlLink link;
char lastchar = 0; char lastchar = 0;
std::string result; std::string result;
@ -131,7 +133,7 @@ std::string Template::renderSearch(const std::vector<SearchResult> &results) con
std::string Template::renderRevisionList(const std::vector<Revision> &revisions, bool withpage) const std::string Template::renderRevisionList(const std::vector<Revision> &revisions, bool withpage) const
{ {
std::stringstream stream; std::stringstream stream;
UrlProvider urlprovider(*this->config); UrlProvider urlprovider(*this->configUrls);
auto genwithoutpage = [&] { auto genwithoutpage = [&] {
for(const Revision &revision : revisions) for(const Revision &revision : revisions)

View File

@ -11,7 +11,12 @@
class Template class Template
{ {
private: private:
const Config *config; ConfigVariableResolver *configVarResolver;
ConfigUrls *configUrls;
std::string templateprefix;
std::string templatepath;
std::map<std::string, TemplatePage> pagesMap; std::map<std::string, TemplatePage> pagesMap;
std::string resolveIncludes(std::string_view content); std::string resolveIncludes(std::string_view content);
@ -22,7 +27,7 @@ class Template
public: public:
Template(const Config &config); Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls, ConfigVariableResolver &configVarsResolver);
/* TODO: returning this as a reference is by no means a risk free business, /* TODO: returning this as a reference is by no means a risk free business,
because between requests, different vars can be set conditionally, because between requests, different vars can be set conditionally,
thus creating a mess thus creating a mess

View File

@ -5,11 +5,11 @@
class UrlProvider class UrlProvider
{ {
private: private:
const Config *config; const ConfigUrls *config;
std::string replaceOnlyPage(std::string templatepart, std::string page); std::string replaceOnlyPage(std::string templatepart, std::string page);
public: public:
UrlProvider(const Config &config) { this->config = &config; } UrlProvider(const ConfigUrls &config) { this->config = &config; }
std::string index(); std::string index();