Introducing HandlerConfig class to give handlers config values they need

This commit is contained in:
Albert S. 2019-09-29 20:27:53 +02:00
parent 364d82a99f
commit 0ccc20454b
8 changed files with 36 additions and 33 deletions

View File

@ -69,8 +69,8 @@ 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->anon_username = optional("anon_username", "anonymouse"); this->handlersConfig.anon_username = optional("anon_username", "anonymouse");
this->wikiname = required("wikiname"); this->handlersConfig.wikiname = required("wikiname");
this->logfile = required("logfile"); this->logfile = required("logfile");
this->templatepath = required("templatepath"); this->templatepath = required("templatepath");
this->urls.linkallcats = required("linkallcats"); this->urls.linkallcats = required("linkallcats");
@ -96,12 +96,12 @@ Config::Config(const std::map<std::string, std::string> &map)
this->urls.userchangepwurl = required("userchangepwurl"); this->urls.userchangepwurl = required("userchangepwurl");
this->connectionstring = required("connectionstring"); 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->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->threadscount = optional("threadscount", 1);
this->anon_permissions = Permissions(required("anon_permissions")); this->handlersConfig.anon_permissions = Permissions(required("anon_permissions"));
this->templateprefix = "{qswiki:"; this->templateprefix = "{qswiki:";

View File

@ -7,8 +7,10 @@
#include "permissions.h" #include "permissions.h"
#include "utils.h" #include "utils.h"
class WikiGeneralConfig /* Stuff handlers/ (may) need to know */
struct HandlerConfig
{ {
Permissions anon_permissions;
std::string wikiname; std::string wikiname;
std::string anon_username; std::string anon_username;
int max_pagename_length; int max_pagename_length;
@ -72,29 +74,21 @@ class Config
uint64_t optional(const std::string &key, uint64_t defaultvalue); uint64_t optional(const std::string &key, uint64_t defaultvalue);
public: public:
ConfigUrls urls;
ConfigVariableResolver configVarResolver;
Config(const std::map<std::string, std::string> &map); Config(const std::map<std::string, std::string> &map);
// TODO: these could be references!? ConfigUrls urls;
std::string wikiname; ConfigVariableResolver configVarResolver;
HandlerConfig handlersConfig;
std::string wikipath; std::string wikipath;
std::string templatepath; std::string templatepath;
std::string templateprefix; std::string templateprefix;
std::string logfile; std::string logfile;
std::string anon_username;
std::string connectionstring; std::string connectionstring;
int query_limit;
int session_max_lifetime; int session_max_lifetime;
int max_pagename_length;
int threadscount; int threadscount;
uint64_t max_payload_length; uint64_t max_payload_length;
Permissions anon_permissions;
}; };
class ConfigReader class ConfigReader

View File

@ -1,6 +1,6 @@
#ifndef HANDLER_H #ifndef HANDLER_H
#define HANDLER_H #define HANDLER_H
#include "../config.h"
#include "../response.h" #include "../response.h"
#include "../request.h" #include "../request.h"
#include "../template.h" #include "../template.h"
@ -17,14 +17,17 @@ class Handler
Database *database; Database *database;
Session *userSession; Session *userSession;
UrlProvider *urlProvider; UrlProvider *urlProvider;
HandlerConfig *handlersConfig;
// TODO: may not to find a better place for this method // TODO: may not to find a better place for this method
Permissions effectivePermissions(std::string page); Permissions effectivePermissions(std::string page);
QueryOption queryOption(const Request &r) const; QueryOption queryOption(const Request &r) const;
public: 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->templ = &templ;
this->database = &db; this->database = &db;
this->userSession = &userSession; this->userSession = &userSession;

View File

@ -38,24 +38,27 @@ class Factory
Session &userSession; Session &userSession;
UrlProvider &urlProvider; UrlProvider &urlProvider;
ICache &cache; ICache &cache;
HandlerConfig &handlerConfig;
public: public:
Factory(Template &templ, Database &db, Session &usersession, UrlProvider &urlprovider, ICache &cache) Factory(HandlerConfig &handlerConfig, Template &templ, Database &db, Session &usersession, UrlProvider &urlprovider,
: templ(templ), db(db), userSession(usersession), urlProvider(urlprovider), cache(cache) ICache &cache)
: handlerConfig(handlerConfig), templ(templ), db(db), userSession(usersession), urlProvider(urlprovider),
cache(cache)
{ {
} }
template <class T> inline std::unique_ptr<T> produce() template <class T> inline std::unique_ptr<T> produce()
{ {
return std::make_unique<T>(templ, db, userSession, urlProvider, cache); return std::make_unique<T>(handlerConfig, templ, db, userSession, urlProvider, cache);
} }
}; };
std::unique_ptr<Handler> createHandler(const std::string &action, Template &templ, Database &db, Session &usersession, std::unique_ptr<Handler> createHandler(const std::string &action, HandlerConfig &handlerConfig, Template &templ,
UrlProvider &urlprovider, ICache &cache) 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") if(action == "" || action == "index")
{ {

View File

@ -4,6 +4,6 @@
#include "handler.h" #include "handler.h"
#include "../template.h" #include "../template.h"
std::unique_ptr<Handler> createHandler(const std::string &action, Template &templ, Database &db, Session &usersession, std::unique_ptr<Handler> createHandler(const std::string &action, HandlerConfig &handlerConfig, Template &templ,
UrlProvider &urlprovider, ICache &cache); Database &db, Session &usersession, UrlProvider &urlprovider, ICache &cache);
#endif // HANDLERFACTORY_H #endif // HANDLERFACTORY_H

View File

@ -117,7 +117,7 @@ int main(int argc, char **argv)
// TODO: quite ugly, anon-handling must be rethought // TODO: quite ugly, anon-handling must be rethought
auto userdao = database->createUserDao(); auto userdao = database->createUserDao();
std::optional<User> anon = userdao->find(config.anon_username); std::optional<User> anon = userdao->find(config.handlersConfig.anon_username);
if(!anon) if(!anon)
{ {
throw std::runtime_error("No such anon user in database"); throw std::runtime_error("No such anon user in database");
@ -126,7 +126,7 @@ int main(int argc, char **argv)
{ {
throw std::runtime_error("Anon user cannot be enabled"); throw std::runtime_error("Anon user cannot be enabled");
} }
anon->permissions = config.anon_permissions; anon->permissions = config.handlersConfig.anon_permissions;
userdao->save(anon.value()); userdao->save(anon.value());
User::setAnon(anon.value()); User::setAnon(anon.value());
@ -135,7 +135,7 @@ int main(int argc, char **argv)
auto cache = createCache(config.configVarResolver); auto cache = createCache(config.configVarResolver);
cache->clear(); cache->clear();
RequestWorker requestWorker(*database, siteTemplate, urlprovider, *cache); RequestWorker requestWorker(config.handlersConfig, *database, siteTemplate, urlprovider, *cache);
auto interface = createGateway(config); auto interface = createGateway(config);

View File

@ -66,7 +66,8 @@ 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 try
{ {

View File

@ -15,14 +15,16 @@ class RequestWorker
Template *templ; Template *templ;
UrlProvider *urlProvider; UrlProvider *urlProvider;
ICache *cache; ICache *cache;
HandlerConfig *handlerConfig;
std::unique_ptr<SessionDao> sessionDao; std::unique_ptr<SessionDao> sessionDao;
private: private:
Session retrieveSession(std::string token) const; Session retrieveSession(std::string token) const;
public: 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->db = &db;
this->templ = &templ; this->templ = &templ;
this->urlProvider = &provider; this->urlProvider = &provider;