From 3745a778e5a4ef3aa87f4171ae927aeea3c71c0b Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 25 Apr 2021 12:06:37 +0200 Subject: [PATCH] Read parser from config and inject it to handlers --- config.cpp | 1 + config.h | 1 + handlers/handler.h | 6 +++++- handlers/handlerfactory.h | 7 ++++--- handlers/handlerpageview.cpp | 10 +++++----- iparser.h | 4 ++++ qswiki.cpp | 20 +++++++++++++++++++- 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/config.cpp b/config.cpp index 6bf8c3e..7d74f89 100644 --- a/config.cpp +++ b/config.cpp @@ -71,6 +71,7 @@ Config::Config(const std::map &map) this->configmap = map; this->wikipath = optional("wikipath", "/"); + this->parser = optional("parser", "markdown"); this->handlersConfig.anon_username = optional("anon_username", "anonymouse"); this->handlersConfig.wikiname = required("wikiname"); this->logfile = required("logfile"); diff --git a/config.h b/config.h index 6780d83..c682340 100644 --- a/config.h +++ b/config.h @@ -86,6 +86,7 @@ class Config std::string templateprefix; std::string logfile; std::string connectionstring; + std::string parser; int session_max_lifetime; int threadscount; diff --git a/handlers/handler.h b/handlers/handler.h index 8569c0e..7d22f4b 100644 --- a/handlers/handler.h +++ b/handlers/handler.h @@ -9,10 +9,13 @@ #include "../database/queryoption.h" #include "../logger.h" #include "../cache/icache.h" +#include "../iparser.h" + class Handler { protected: ICache *cache; + IParser *parser; Template *templ; Database *database; Session *userSession; @@ -25,7 +28,7 @@ class Handler public: Handler(HandlerConfig &handlersConfig, Template &templ, Database &db, Session &userSession, UrlProvider &provider, - ICache &cache) + ICache &cache, IParser &parser) { this->handlersConfig = &handlersConfig; this->templ = &templ; @@ -33,6 +36,7 @@ class Handler this->userSession = &userSession; this->urlProvider = &provider; this->cache = &cache; + this->parser = &parser; } virtual Response handle(const Request &r); diff --git a/handlers/handlerfactory.h b/handlers/handlerfactory.h index 7120b89..e08649f 100644 --- a/handlers/handlerfactory.h +++ b/handlers/handlerfactory.h @@ -10,15 +10,16 @@ class HandlerFactory Database &db; UrlProvider &urlProvider; ICache &cache; + IParser &parser; template inline std::unique_ptr produce(Session &userSession) { - return std::make_unique(handlerConfig, templ, db, userSession, urlProvider, cache); + return std::make_unique(handlerConfig, templ, db, userSession, urlProvider, cache, parser); } public: - HandlerFactory(HandlerConfig &handlerConfig, Template &templ, Database &db, UrlProvider &urlprovider, ICache &cache) - : handlerConfig(handlerConfig), templ(templ), db(db), urlProvider(urlprovider), cache(cache) + HandlerFactory(HandlerConfig &handlerConfig, Template &templ, Database &db, UrlProvider &urlprovider, ICache &cache, IParser &parser) + : handlerConfig(handlerConfig), templ(templ), db(db), urlProvider(urlprovider), cache(cache), parser(parser) { } std::unique_ptr createHandler(const std::string &action, Session &userSession); diff --git a/handlers/handlerpageview.cpp b/handlers/handlerpageview.cpp index b80caac..fc97fdc 100644 --- a/handlers/handlerpageview.cpp +++ b/handlers/handlerpageview.cpp @@ -130,7 +130,7 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename, TemplatePage &page = this->templ->getPage(templatepartname); - ParserLegacy parser; + Response result; result.setStatus(200); std::string indexcontent; @@ -138,8 +138,8 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename, if(revisionid > 0) { - indexcontent = createIndexContent(parser, revision->content); - parsedcontent = parser.parse(pageDao, *this->urlProvider, revision->content); + indexcontent = createIndexContent(*parser, revision->content); + parsedcontent = parser->parse(pageDao, *this->urlProvider, revision->content); } else { @@ -153,7 +153,7 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename, } else { - indexcontent = createIndexContent(parser, revision->content); + indexcontent = createIndexContent(*parser, revision->content); this->cache->put(cachekeyindexcontent, indexcontent); } if(cachedparsedcontent) @@ -162,7 +162,7 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename, } else { - parsedcontent = parser.parse(pageDao, *this->urlProvider, revision->content); + parsedcontent = parser->parse(pageDao, *this->urlProvider, revision->content); this->cache->put(cachekeyparsedcontent, parsedcontent); } } diff --git a/iparser.h b/iparser.h index 3835e86..973d516 100644 --- a/iparser.h +++ b/iparser.h @@ -5,6 +5,8 @@ #include "headline.h" #include "database/pagedao.h" #include "urlprovider.h" + + class IParser { public: @@ -16,4 +18,6 @@ class IParser virtual ~IParser(){}; }; + + #endif // PARSER_H diff --git a/qswiki.cpp b/qswiki.cpp index 46143cb..395b52c 100644 --- a/qswiki.cpp +++ b/qswiki.cpp @@ -37,6 +37,10 @@ SOFTWARE. #include "requestworker.h" #include "cache/fscache.h" #include "sandbox/sandboxfactory.h" +#include "iparser.h" +#include "parserlegacy.h" +#include "parsermarkdown.h" + void sigterm_handler(int arg) { // TODO: proper shutdown. @@ -63,6 +67,17 @@ std::unique_ptr createCache(const ConfigVariableResolver &resolver) return std::make_unique(path); } + +std::unique_ptr createParser(const ConfigVariableResolver &resolver) +{ + std::string parser = resolver.getConfig("parser"); + if(parser == "legacy") + { + return std::make_unique(); + } + return std::make_unique(); +} + int main(int argc, char **argv) { if(geteuid() == 0) @@ -135,7 +150,10 @@ int main(int argc, char **argv) auto cache = createCache(config.configVarResolver); 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}; auto interface = createGateway(config);