Read parser from config and inject it to handlers

This commit is contained in:
Albert S. 2021-04-25 12:06:37 +02:00
parent cec56d5a73
commit 3745a778e5
7 changed files with 39 additions and 10 deletions

View File

@ -71,6 +71,7 @@ Config::Config(const std::map<std::string, std::string> &map)
this->configmap = map; this->configmap = map;
this->wikipath = optional("wikipath", "/"); this->wikipath = optional("wikipath", "/");
this->parser = optional("parser", "markdown");
this->handlersConfig.anon_username = optional("anon_username", "anonymouse"); this->handlersConfig.anon_username = optional("anon_username", "anonymouse");
this->handlersConfig.wikiname = required("wikiname"); this->handlersConfig.wikiname = required("wikiname");
this->logfile = required("logfile"); this->logfile = required("logfile");

View File

@ -86,6 +86,7 @@ class Config
std::string templateprefix; std::string templateprefix;
std::string logfile; std::string logfile;
std::string connectionstring; std::string connectionstring;
std::string parser;
int session_max_lifetime; int session_max_lifetime;
int threadscount; int threadscount;

View File

@ -9,10 +9,13 @@
#include "../database/queryoption.h" #include "../database/queryoption.h"
#include "../logger.h" #include "../logger.h"
#include "../cache/icache.h" #include "../cache/icache.h"
#include "../iparser.h"
class Handler class Handler
{ {
protected: protected:
ICache *cache; ICache *cache;
IParser *parser;
Template *templ; Template *templ;
Database *database; Database *database;
Session *userSession; Session *userSession;
@ -25,7 +28,7 @@ class Handler
public: public:
Handler(HandlerConfig &handlersConfig, Template &templ, Database &db, Session &userSession, UrlProvider &provider, Handler(HandlerConfig &handlersConfig, Template &templ, Database &db, Session &userSession, UrlProvider &provider,
ICache &cache) ICache &cache, IParser &parser)
{ {
this->handlersConfig = &handlersConfig; this->handlersConfig = &handlersConfig;
this->templ = &templ; this->templ = &templ;
@ -33,6 +36,7 @@ class Handler
this->userSession = &userSession; this->userSession = &userSession;
this->urlProvider = &provider; this->urlProvider = &provider;
this->cache = &cache; this->cache = &cache;
this->parser = &parser;
} }
virtual Response handle(const Request &r); virtual Response handle(const Request &r);

View File

@ -10,15 +10,16 @@ class HandlerFactory
Database &db; Database &db;
UrlProvider &urlProvider; UrlProvider &urlProvider;
ICache &cache; ICache &cache;
IParser &parser;
template <class T> inline std::unique_ptr<T> produce(Session &userSession) template <class T> inline std::unique_ptr<T> produce(Session &userSession)
{ {
return std::make_unique<T>(handlerConfig, templ, db, userSession, urlProvider, cache); return std::make_unique<T>(handlerConfig, templ, db, userSession, urlProvider, cache, parser);
} }
public: public:
HandlerFactory(HandlerConfig &handlerConfig, Template &templ, Database &db, UrlProvider &urlprovider, ICache &cache) HandlerFactory(HandlerConfig &handlerConfig, Template &templ, Database &db, UrlProvider &urlprovider, ICache &cache, IParser &parser)
: handlerConfig(handlerConfig), templ(templ), db(db), urlProvider(urlprovider), cache(cache) : handlerConfig(handlerConfig), templ(templ), db(db), urlProvider(urlprovider), cache(cache), parser(parser)
{ {
} }
std::unique_ptr<Handler> createHandler(const std::string &action, Session &userSession); std::unique_ptr<Handler> createHandler(const std::string &action, Session &userSession);

View File

@ -130,7 +130,7 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename,
TemplatePage &page = this->templ->getPage(templatepartname); TemplatePage &page = this->templ->getPage(templatepartname);
ParserLegacy parser;
Response result; Response result;
result.setStatus(200); result.setStatus(200);
std::string indexcontent; std::string indexcontent;
@ -138,8 +138,8 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename,
if(revisionid > 0) if(revisionid > 0)
{ {
indexcontent = createIndexContent(parser, revision->content); indexcontent = createIndexContent(*parser, revision->content);
parsedcontent = parser.parse(pageDao, *this->urlProvider, revision->content); parsedcontent = parser->parse(pageDao, *this->urlProvider, revision->content);
} }
else else
{ {
@ -153,7 +153,7 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename,
} }
else else
{ {
indexcontent = createIndexContent(parser, revision->content); indexcontent = createIndexContent(*parser, revision->content);
this->cache->put(cachekeyindexcontent, indexcontent); this->cache->put(cachekeyindexcontent, indexcontent);
} }
if(cachedparsedcontent) if(cachedparsedcontent)
@ -162,7 +162,7 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename,
} }
else else
{ {
parsedcontent = parser.parse(pageDao, *this->urlProvider, revision->content); parsedcontent = parser->parse(pageDao, *this->urlProvider, revision->content);
this->cache->put(cachekeyparsedcontent, parsedcontent); this->cache->put(cachekeyparsedcontent, parsedcontent);
} }
} }

View File

@ -5,6 +5,8 @@
#include "headline.h" #include "headline.h"
#include "database/pagedao.h" #include "database/pagedao.h"
#include "urlprovider.h" #include "urlprovider.h"
class IParser class IParser
{ {
public: public:
@ -16,4 +18,6 @@ class IParser
virtual ~IParser(){}; virtual ~IParser(){};
}; };
#endif // PARSER_H #endif // PARSER_H

View File

@ -37,6 +37,10 @@ SOFTWARE.
#include "requestworker.h" #include "requestworker.h"
#include "cache/fscache.h" #include "cache/fscache.h"
#include "sandbox/sandboxfactory.h" #include "sandbox/sandboxfactory.h"
#include "iparser.h"
#include "parserlegacy.h"
#include "parsermarkdown.h"
void sigterm_handler(int arg) void sigterm_handler(int arg)
{ {
// TODO: proper shutdown. // TODO: proper shutdown.
@ -63,6 +67,17 @@ std::unique_ptr<ICache> createCache(const ConfigVariableResolver &resolver)
return std::make_unique<FsCache>(path); return std::make_unique<FsCache>(path);
} }
std::unique_ptr<IParser> createParser(const ConfigVariableResolver &resolver)
{
std::string parser = resolver.getConfig("parser");
if(parser == "legacy")
{
return std::make_unique<ParserLegacy>();
}
return std::make_unique<ParserMarkdown>();
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if(geteuid() == 0) if(geteuid() == 0)
@ -135,7 +150,10 @@ int main(int argc, char **argv)
auto cache = createCache(config.configVarResolver); auto cache = createCache(config.configVarResolver);
cache->clear(); cache->clear();
HandlerFactory handlerFactory{config.handlersConfig, siteTemplate, *database.get(), urlProvider, *cache.get()}; auto parser = createParser(config.configVarResolver);
HandlerFactory handlerFactory{config.handlersConfig, siteTemplate, *database.get(), urlProvider, *cache.get(), *parser.get()};
RequestWorker requestWorker{handlerFactory, database->createSessionDao(), siteTemplate}; RequestWorker requestWorker{handlerFactory, database->createSessionDao(), siteTemplate};
auto interface = createGateway(config); auto interface = createGateway(config);