Begin removing several dependencies on Config object
This commit is contained in:
parent
3b312c7d9e
commit
083f7ff842
44
config.cpp
44
config.cpp
@ -75,27 +75,27 @@ Config::Config(const std::map<std::string, std::string> &map)
|
||||
this->wikiname = required("wikiname");
|
||||
this->logfile = required("logfile");
|
||||
this->templatepath = required("templatepath");
|
||||
this->linkallcats = required("linkallcats");
|
||||
this->linkallpages = required("linkallpages");
|
||||
this->linkcategory = required("linkcategory");
|
||||
this->linkdelete = required("linkdelete");
|
||||
this->linkedit = required("linkedit");
|
||||
this->linkhistory = required("linkhistory");
|
||||
this->linkindex = required("linkindex");
|
||||
this->linklogout = required("linklogout");
|
||||
this->linkpage = required("linkpage");
|
||||
this->linkrecent = required("linkrecent");
|
||||
this->linkrevision = required("linkrevision");
|
||||
this->linksettings = required("linksettings");
|
||||
this->linkshere = required("linkshere");
|
||||
this->loginurl = required("loginurl");
|
||||
this->linkrecentsort = required("linkrecentsort");
|
||||
this->linkhistorysort = required("linkhistorysort");
|
||||
this->actionurl = required("actionurl");
|
||||
this->settingsurl = required("settingsurl");
|
||||
this->deletionurl = required("deletionurl");
|
||||
this->adminregisterurl = required("adminregisterurl");
|
||||
this->userchangepwurl = required("userchangepwurl");
|
||||
this->urls.linkallcats = required("linkallcats");
|
||||
this->urls.linkallpages = required("linkallpages");
|
||||
this->urls.linkcategory = required("linkcategory");
|
||||
this->urls.linkdelete = required("linkdelete");
|
||||
this->urls.linkedit = required("linkedit");
|
||||
this->urls.linkhistory = required("linkhistory");
|
||||
this->urls.linkindex = required("linkindex");
|
||||
this->urls.linklogout = required("linklogout");
|
||||
this->urls.linkpage = required("linkpage");
|
||||
this->urls.linkrecent = required("linkrecent");
|
||||
this->urls.linkrevision = required("linkrevision");
|
||||
this->urls.linksettings = required("linksettings");
|
||||
this->urls.linkshere = required("linkshere");
|
||||
this->urls.loginurl = required("loginurl");
|
||||
this->urls.linkrecentsort = required("linkrecentsort");
|
||||
this->urls.linkhistorysort = required("linkhistorysort");
|
||||
this->urls.actionurl = required("actionurl");
|
||||
this->urls.settingsurl = required("settingsurl");
|
||||
this->urls.deletionurl = required("deletionurl");
|
||||
this->urls.adminregisterurl = required("adminregisterurl");
|
||||
this->urls.userchangepwurl = required("userchangepwurl");
|
||||
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);
|
||||
|
||||
ConfigVariableResolver resolver { this->configmap };
|
||||
this->configVarResolver = resolver;
|
||||
|
||||
|
||||
}
|
||||
|
93
config.h
93
config.h
@ -1,4 +1,4 @@
|
||||
#ifndef CONFIG_H
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
@ -6,31 +6,23 @@
|
||||
#include <map>
|
||||
#include "permissions.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 wikipath;
|
||||
std::string templatepath;
|
||||
std::string templateprefix;
|
||||
std::string logfile;
|
||||
std::string anon_username;
|
||||
std::string linkindex ;
|
||||
std::string linkrecent ;
|
||||
std::string linkallpages ;
|
||||
std::string linkallcats ;
|
||||
std::string linkshere ;
|
||||
std::string linkpage ;
|
||||
int max_pagename_length;
|
||||
int query_limit;
|
||||
};
|
||||
|
||||
struct ConfigUrls
|
||||
{
|
||||
std::string linkindex;
|
||||
std::string linkrecent;
|
||||
std::string linkallpages;
|
||||
std::string linkallcats;
|
||||
std::string linkshere;
|
||||
std::string linkpage;
|
||||
std::string linkrevision ;
|
||||
std::string linkhistory ;
|
||||
std::string linkedit ;
|
||||
@ -46,6 +38,56 @@ public:
|
||||
std::string linkhistorysort;
|
||||
std::string adminregisterurl;
|
||||
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;
|
||||
|
||||
int query_limit;
|
||||
@ -57,10 +99,7 @@ public:
|
||||
|
||||
Permissions anon_permissions;
|
||||
|
||||
std::string getConfig(const std::string &key) const
|
||||
{
|
||||
return utils::getKeyOrEmpty(configmap, key);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
@ -24,6 +24,18 @@ SOFTWARE.
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -20,21 +20,11 @@ SOFTWARE.
|
||||
*/
|
||||
#include "httpgateway.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");
|
||||
if(this->listenaddr.empty())
|
||||
{
|
||||
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;
|
||||
this->listenaddr = listenaddr;
|
||||
this->listenport = port;
|
||||
this->maxPayloadLength = maxPayloadLength;
|
||||
}
|
||||
|
||||
bool HttpGateway::keepReading()
|
||||
|
@ -18,7 +18,7 @@ private:
|
||||
int listenport;
|
||||
uint64_t maxPayloadLength;
|
||||
public:
|
||||
HttpGateway(const Config &config);
|
||||
HttpGateway(std::string listenaddr, int port, uint64_t maxPayloadLength);
|
||||
bool keepReading() override;
|
||||
void work(RequestWorker &worker) override;
|
||||
};
|
||||
|
14
qswiki.cpp
14
qswiki.cpp
@ -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)
|
||||
{
|
||||
@ -99,7 +99,7 @@ int main(int argc, char **argv)
|
||||
|
||||
|
||||
if(!sandbox->enablePreWorker({
|
||||
config.getConfig("cache_fs_dir"),
|
||||
config.configVarResolver.getConfig("cache_fs_dir"),
|
||||
config.templatepath,
|
||||
std::filesystem::path(config.logfile).parent_path(),
|
||||
std::filesystem::path(config.connectionstring).parent_path(),
|
||||
@ -132,13 +132,13 @@ int main(int argc, char **argv)
|
||||
userdao->save(anon.value());
|
||||
User::setAnon(anon.value());
|
||||
|
||||
Template siteTemplate { config };
|
||||
UrlProvider urlprovider { config };
|
||||
Template siteTemplate { config.templateprefix, config.templatepath, config.urls, config.configVarResolver };
|
||||
UrlProvider urlprovider { config.urls };
|
||||
|
||||
|
||||
|
||||
|
||||
auto cache = createCache(config);
|
||||
auto cache = createCache(config.configVarResolver);
|
||||
cache->clear();
|
||||
RequestWorker requestWorker (*database, siteTemplate, urlprovider, *cache );
|
||||
|
||||
|
22
template.cpp
22
template.cpp
@ -23,22 +23,24 @@ SOFTWARE.
|
||||
#include "urlprovider.h"
|
||||
#include "htmllink.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)
|
||||
{
|
||||
//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 partpath = getPartPath(partname);
|
||||
return utils::readCompleteFile(partpath);
|
||||
}
|
||||
|
||||
std::string Template::loadResolvedPart(std::string_view 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)
|
||||
{
|
||||
Varreplacer replacer(this->config->templateprefix);
|
||||
Varreplacer replacer(this->templateprefix);
|
||||
replacer.addResolver("include", [&](std::string_view key) { return loadResolvedPart(key); });
|
||||
return replacer.parse(content);
|
||||
}
|
||||
@ -55,8 +57,8 @@ std::string Template::resolveIncludes(std::string_view content)
|
||||
TemplatePage Template::createPage(std::string name)
|
||||
{
|
||||
std::string content = loadResolvedPart(name);
|
||||
Varreplacer replacer(this->config->templateprefix);
|
||||
replacer.addResolver("config", [&](std::string_view key) { return this->config->getConfig(std::string(key)); });
|
||||
Varreplacer replacer(this->templateprefix);
|
||||
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.
|
||||
//
|
||||
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
|
||||
{
|
||||
UrlProvider urlprovider(*this->config);
|
||||
UrlProvider urlprovider(*this->configUrls);
|
||||
return renderSearch(results, [&urlprovider](std::string s) { return urlprovider.page(s);});
|
||||
|
||||
}
|
||||
std::string Template::renderSearch(const std::vector<SearchResult> &results) const
|
||||
{
|
||||
UrlProvider urlprovider(*this->config);
|
||||
UrlProvider urlprovider(*this->configUrls);
|
||||
HtmlLink link;
|
||||
char lastchar = 0;
|
||||
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::stringstream stream;
|
||||
UrlProvider urlprovider(*this->config);
|
||||
UrlProvider urlprovider(*this->configUrls);
|
||||
|
||||
auto genwithoutpage = [&] {
|
||||
for(const Revision &revision : revisions)
|
||||
|
@ -11,7 +11,12 @@
|
||||
class Template
|
||||
{
|
||||
private:
|
||||
const Config *config;
|
||||
ConfigVariableResolver *configVarResolver;
|
||||
ConfigUrls *configUrls;
|
||||
|
||||
std::string templateprefix;
|
||||
std::string templatepath;
|
||||
|
||||
std::map<std::string, TemplatePage> pagesMap;
|
||||
std::string resolveIncludes(std::string_view content);
|
||||
|
||||
@ -22,7 +27,7 @@ class Template
|
||||
|
||||
|
||||
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,
|
||||
because between requests, different vars can be set conditionally,
|
||||
thus creating a mess
|
||||
|
@ -5,11 +5,11 @@
|
||||
class UrlProvider
|
||||
{
|
||||
private:
|
||||
const Config *config;
|
||||
const ConfigUrls *config;
|
||||
|
||||
std::string replaceOnlyPage(std::string templatepart, std::string page);
|
||||
public:
|
||||
UrlProvider(const Config &config) { this->config = &config; }
|
||||
UrlProvider(const ConfigUrls &config) { this->config = &config; }
|
||||
|
||||
std::string index();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user