Introducing HandlerConfig class to give handlers config values they need
This commit is contained in:
parent
083f7ff842
commit
7c7b8bf5ed
10
config.cpp
10
config.cpp
@ -71,8 +71,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");
|
||||||
@ -99,12 +99,12 @@ Config::Config(const std::map<std::string, std::string> &map)
|
|||||||
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:";
|
||||||
|
|
||||||
|
20
config.h
20
config.h
@ -7,12 +7,15 @@
|
|||||||
#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;
|
||||||
int query_limit;
|
int query_limit;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ConfigUrls
|
struct ConfigUrls
|
||||||
@ -75,29 +78,22 @@ private:
|
|||||||
int optional(const std::string &key, int defaulvalue);
|
int optional(const std::string &key, int defaulvalue);
|
||||||
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;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -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,13 +17,15 @@ protected:
|
|||||||
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;
|
||||||
|
@ -38,22 +38,23 @@ 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) : 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>
|
template<class T>
|
||||||
inline std::unique_ptr<T> produce()
|
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)
|
&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")
|
||||||
{
|
{
|
||||||
|
@ -4,5 +4,5 @@
|
|||||||
#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, 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
|
#endif // HANDLERFACTORY_H
|
||||||
|
@ -119,7 +119,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");
|
||||||
@ -128,7 +128,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());
|
||||||
|
|
||||||
@ -140,7 +140,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);
|
||||||
|
|
||||||
|
@ -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
|
try
|
||||||
{
|
{
|
||||||
|
@ -15,12 +15,14 @@ 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user