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
父節點 083f7ff842
當前提交 7c7b8bf5ed
共有 8 個檔案被更改,包括 30 行新增29 行删除

查看文件

@ -71,8 +71,8 @@ Config::Config(const std::map<std::string, std::string> &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<std::string, std::string> &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:";

查看文件

@ -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<std::string, std::string> &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;

查看文件

@ -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;

查看文件

@ -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<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
std::unique_ptr<Handler> 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")
{

查看文件

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

查看文件

@ -119,7 +119,7 @@ int main(int argc, char **argv)
//TODO: quite ugly, anon-handling must be rethought
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)
{
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);

查看文件

@ -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
{

查看文件

@ -15,12 +15,14 @@ class RequestWorker
Template *templ;
UrlProvider *urlProvider;
ICache *cache;
HandlerConfig *handlerConfig;
std::unique_ptr<SessionDao> 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;