diff --git a/config.cpp b/config.cpp index 9d1b5f6..0c8082d 100644 --- a/config.cpp +++ b/config.cpp @@ -71,8 +71,8 @@ Config::Config(const std::map &map) this->configmap = map; this->wikipath = optional("wikipath", "/"); - this->anon_username = optional("anon_username", "anonymouse"); - this->wikiname = required("wikiname"); + this->handlersConfig.anon_username = optional("anon_username", "anonymouse"); + this->handlersConfig.wikiname = required("wikiname"); this->logfile = required("logfile"); this->templatepath = required("templatepath"); this->urls.linkallcats = required("linkallcats"); @@ -99,12 +99,12 @@ Config::Config(const std::map &map) this->connectionstring = required("connectionstring"); - this->max_pagename_length = optional("max_pagename_length", 256); + this->handlersConfig.max_pagename_length = optional("max_pagename_length", 256); this->session_max_lifetime = optional("session_max_lifetime", 3600); - this->query_limit = optional("query_limit", 200); + this->handlersConfig.query_limit = optional("query_limit", 200); this->threadscount = optional("threadscount", 1); - this->anon_permissions = Permissions(required("anon_permissions")); + this->handlersConfig.anon_permissions = Permissions(required("anon_permissions")); this->templateprefix = "{qswiki:"; diff --git a/config.h b/config.h index d76241a..66973a1 100644 --- a/config.h +++ b/config.h @@ -7,12 +7,15 @@ #include "permissions.h" #include "utils.h" -class WikiGeneralConfig +/* Stuff handlers/ (may) need to know */ +struct HandlerConfig { + Permissions anon_permissions; std::string wikiname; std::string anon_username; int max_pagename_length; int query_limit; + }; struct ConfigUrls @@ -75,29 +78,22 @@ private: 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 &map ); - //TODO: these could be references!? - std::string wikiname; + ConfigUrls urls; + ConfigVariableResolver configVarResolver; + HandlerConfig handlersConfig; + std::string wikipath; std::string templatepath; std::string templateprefix; std::string logfile; - std::string anon_username; - std::string connectionstring; - - int query_limit; int session_max_lifetime; - int max_pagename_length; int threadscount; uint64_t max_payload_length; - Permissions anon_permissions; diff --git a/handlers/handler.h b/handlers/handler.h index 948984b..4e0d632 100644 --- a/handlers/handler.h +++ b/handlers/handler.h @@ -1,6 +1,6 @@ #ifndef HANDLER_H #define HANDLER_H - +#include "../config.h" #include "../response.h" #include "../request.h" #include "../template.h" @@ -17,13 +17,15 @@ protected: Database *database; Session *userSession; UrlProvider *urlProvider; + HandlerConfig *handlersConfig; //TODO: may not to find a better place for this method Permissions effectivePermissions(std::string page); QueryOption queryOption(const Request &r) const; public: - Handler(Template &templ, Database &db, Session &userSession, UrlProvider &provider, ICache &cache) + Handler(HandlerConfig &handlersConfig, Template &templ, Database &db, Session &userSession, UrlProvider &provider, ICache &cache) { + this->handlersConfig = &handlersConfig; this->templ = &templ; this->database = &db; this->userSession = &userSession; diff --git a/handlers/handlerfactory.cpp b/handlers/handlerfactory.cpp index 8fa7448..581a582 100644 --- a/handlers/handlerfactory.cpp +++ b/handlers/handlerfactory.cpp @@ -38,22 +38,23 @@ class Factory Session &userSession; UrlProvider &urlProvider; ICache &cache; + HandlerConfig &handlerConfig; public: - Factory(Template &templ, Database &db, Session &usersession, UrlProvider &urlprovider, ICache &cache) : templ(templ) ,db(db), userSession(usersession), urlProvider(urlprovider), cache(cache) { } + Factory(HandlerConfig &handlerConfig, Template &templ, Database &db, Session &usersession, UrlProvider &urlprovider, ICache &cache) : handlerConfig(handlerConfig), templ(templ) ,db(db), userSession(usersession), urlProvider(urlprovider), cache(cache) { } template inline std::unique_ptr produce() { - return std::make_unique(templ, db, userSession, urlProvider, cache); + return std::make_unique(handlerConfig, templ, db, userSession, urlProvider, cache); } }; -std::unique_ptr createHandler(const std::string &action, Template &templ, Database +std::unique_ptr createHandler(const std::string &action, HandlerConfig &handlerConfig, Template &templ, Database &db, Session &usersession, UrlProvider &urlprovider, ICache &cache) { - Factory producer(templ, db, usersession, urlprovider, cache); + Factory producer(handlerConfig, templ, db, usersession, urlprovider, cache); if(action == "" || action == "index") { diff --git a/handlers/handlerfactory.h b/handlers/handlerfactory.h index 5281ff7..8590569 100644 --- a/handlers/handlerfactory.h +++ b/handlers/handlerfactory.h @@ -4,5 +4,5 @@ #include "handler.h" #include "../template.h" -std::unique_ptr createHandler(const std::string &action, Template &templ, Database &db, Session &usersession, UrlProvider &urlprovider, ICache &cache); +std::unique_ptr createHandler(const std::string &action, HandlerConfig &handlerConfig, Template &templ, Database &db, Session &usersession, UrlProvider &urlprovider, ICache &cache); #endif // HANDLERFACTORY_H diff --git a/qswiki.cpp b/qswiki.cpp index 5da8968..96b0eb3 100644 --- a/qswiki.cpp +++ b/qswiki.cpp @@ -119,7 +119,7 @@ int main(int argc, char **argv) //TODO: quite ugly, anon-handling must be rethought auto userdao = database->createUserDao(); - std::optional anon = userdao->find(config.anon_username); + std::optional anon = userdao->find(config.handlersConfig.anon_username); if(!anon) { throw std::runtime_error("No such anon user in database"); @@ -128,7 +128,7 @@ int main(int argc, char **argv) { throw std::runtime_error("Anon user cannot be enabled"); } - anon->permissions = config.anon_permissions; + anon->permissions = config.handlersConfig.anon_permissions; userdao->save(anon.value()); User::setAnon(anon.value()); @@ -140,7 +140,7 @@ int main(int argc, char **argv) auto cache = createCache(config.configVarResolver); cache->clear(); - RequestWorker requestWorker (*database, siteTemplate, urlprovider, *cache ); + RequestWorker requestWorker (config.handlersConfig, *database, siteTemplate, urlprovider, *cache ); auto interface = createGateway(config); diff --git a/requestworker.cpp b/requestworker.cpp index 549b27b..6107071 100644 --- a/requestworker.cpp +++ b/requestworker.cpp @@ -68,7 +68,7 @@ Response RequestWorker::processRequest(const Request &r) } } - auto handler = createHandler(r.param("action"), *this->templ, *this->db, session, *this->urlProvider, *this->cache); + auto handler = createHandler(r.param("action"), *this->handlerConfig, *this->templ, *this->db, session, *this->urlProvider, *this->cache); try { diff --git a/requestworker.h b/requestworker.h index 5a67179..cb189df 100644 --- a/requestworker.h +++ b/requestworker.h @@ -15,12 +15,14 @@ class RequestWorker Template *templ; UrlProvider *urlProvider; ICache *cache; + HandlerConfig *handlerConfig; std::unique_ptr sessionDao; private: Session retrieveSession(std::string token) const; public: - RequestWorker(Database &db, Template &templ, UrlProvider &provider, ICache &cache) + RequestWorker(HandlerConfig &handlerConfig, Database &db, Template &templ, UrlProvider &provider, ICache &cache) { + this->handlerConfig = &handlerConfig; this->db = &db; this->templ = &templ; this->urlProvider = &provider;