Introducing HandlerConfig class to give handlers config values they need
Este cometimento está contido em:
ascendente
364d82a99f
cometimento
0ccc20454b
10
config.cpp
10
config.cpp
@ -69,8 +69,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");
|
||||
@ -96,12 +96,12 @@ Config::Config(const std::map<std::string, std::string> &map)
|
||||
this->urls.userchangepwurl = required("userchangepwurl");
|
||||
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:";
|
||||
|
||||
|
20
config.h
20
config.h
@ -7,8 +7,10 @@
|
||||
#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;
|
||||
@ -72,29 +74,21 @@ class Config
|
||||
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;
|
||||
};
|
||||
|
||||
class ConfigReader
|
||||
|
@ -1,6 +1,6 @@
|
||||
#ifndef HANDLER_H
|
||||
#define HANDLER_H
|
||||
|
||||
#include "../config.h"
|
||||
#include "../response.h"
|
||||
#include "../request.h"
|
||||
#include "../template.h"
|
||||
@ -17,14 +17,17 @@ class Handler
|
||||
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,24 +38,27 @@ 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 &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)
|
||||
{
|
||||
|
||||
Factory producer(templ, db, usersession, urlprovider, cache);
|
||||
Factory producer(handlerConfig, templ, db, usersession, urlprovider, cache);
|
||||
|
||||
if(action == "" || action == "index")
|
||||
{
|
||||
|
@ -4,6 +4,6 @@
|
||||
#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
|
||||
|
@ -117,7 +117,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");
|
||||
@ -126,7 +126,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());
|
||||
|
||||
@ -135,7 +135,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);
|
||||
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -15,14 +15,16 @@ 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;
|
||||
|
Carregando…
Criar uma nova questão referindo esta
Bloquear um utilizador