Introduce proper HandlerFactory and reduce dependencies of RequestWorker

This commit is contained in:
Albert S. 2019-09-29 20:57:46 +02:00
vanhempi 7c7b8bf5ed
commit bae6ae73bf
5 muutettua tiedostoa jossa 79 lisäystä ja 79 poistoa

Näytä tiedosto

@ -18,6 +18,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include "handlerfactory.h" #include "handlerfactory.h"
#include "handler.h" #include "handler.h"
#include "handlerdefault.h" #include "handlerdefault.h"
@ -31,72 +32,51 @@ SOFTWARE.
#include "handlercategory.h" #include "handlercategory.h"
#include "handlerhistory.h" #include "handlerhistory.h"
#include "handlerpagedelete.h" #include "handlerpagedelete.h"
class Factory
std::unique_ptr<Handler> HandlerFactory::createHandler(const std::string &action, Session &userSession)
{ {
Template &templ; if(action == "" || action == "index")
Database &db; {
Session &userSession; return produce<HandlerDefault>(userSession);
UrlProvider &urlProvider; }
ICache &cache; if(action == "show")
HandlerConfig &handlerConfig; {
public: return produce<HandlerPageView>(userSession);
}
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) { } if(action == "edit")
{
template<class T> return produce<HandlerPageEdit>(userSession);
inline std::unique_ptr<T> produce() }
{ if(action == "login")
return std::make_unique<T>(handlerConfig, templ, db, userSession, urlProvider, cache); {
} return produce<HandlerLogin>(userSession);
}; }
if(action == "search")
std::unique_ptr<Handler> createHandler(const std::string &action, HandlerConfig &handlerConfig, Template &templ, Database {
&db, Session &usersession, UrlProvider &urlprovider, ICache &cache) return produce<HandlerSearch>(userSession);
{ }
if(action == "delete")
Factory producer(handlerConfig, templ, db, usersession, urlprovider, cache); {
return produce<HandlerPageDelete>(userSession);
if(action == "" || action == "index") }
{ if(action == "allpages")
return producer.produce<HandlerDefault>(); {
} return produce<HandlerAllPages>(userSession);
if(action == "show") }
{ if(action == "allcategories")
return producer.produce<HandlerPageView>(); {
} return produce<HandlerAllCategories>(userSession);
if(action == "edit") }
{ if(action == "showcat")
return producer.produce<HandlerPageEdit>(); {
} return produce<HandlerCategory>(userSession);
if(action == "login") }
{ if(action == "recent")
return producer.produce<HandlerLogin>(); {
} return produce<HandlerHistory>(userSession);
if(action == "search") }
{
return producer.produce<HandlerSearch>();
}
if(action == "delete")
{
return producer.produce<HandlerPageDelete>();
}
if(action == "allpages")
{
return producer.produce<HandlerAllPages>();
}
if(action == "allcategories")
{
return producer.produce<HandlerAllCategories>();
}
if(action == "showcat")
{
return producer.produce<HandlerCategory>();
}
if(action == "recent")
{
return producer.produce<HandlerHistory>();
}
return producer.produce<HandlerInvalidAction>(); return produce<HandlerInvalidAction>(userSession);
} }

Näytä tiedosto

@ -3,6 +3,26 @@
#include <memory> #include <memory>
#include "handler.h" #include "handler.h"
#include "../template.h" #include "../template.h"
class HandlerFactory
{
HandlerConfig &handlerConfig;
Template &templ;
Database &db;
UrlProvider &urlProvider;
ICache &cache;
template<class T>
inline std::unique_ptr<T> produce( Session &userSession)
{
return std::make_unique<T>(handlerConfig, templ, db, userSession, urlProvider, cache);
}
public:
HandlerFactory(HandlerConfig &handlerConfig, Template &templ, Database &db, UrlProvider &urlprovider, ICache &cache) : handlerConfig(handlerConfig), templ(templ) ,db(db), urlProvider(urlprovider), cache(cache) { }
std::unique_ptr<Handler> createHandler(const std::string &action, Session &userSession);
};
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

Näytä tiedosto

@ -133,14 +133,16 @@ int main(int argc, char **argv)
User::setAnon(anon.value()); User::setAnon(anon.value());
Template siteTemplate { config.templateprefix, config.templatepath, config.urls, config.configVarResolver }; Template siteTemplate { config.templateprefix, config.templatepath, config.urls, config.configVarResolver };
UrlProvider urlprovider { config.urls }; UrlProvider urlProvider { config.urls };
auto cache = createCache(config.configVarResolver); auto cache = createCache(config.configVarResolver);
cache->clear(); cache->clear();
RequestWorker requestWorker (config.handlersConfig, *database, siteTemplate, urlprovider, *cache );
HandlerFactory handlerFactory { config.handlersConfig, siteTemplate, *database.get(), urlProvider, *cache.get()};
RequestWorker requestWorker { handlerFactory, *database->createSessionDao().get(), siteTemplate };
auto interface = createGateway(config); auto interface = createGateway(config);

Näytä tiedosto

@ -68,8 +68,7 @@ Response RequestWorker::processRequest(const Request &r)
} }
} }
auto handler = createHandler(r.param("action"), *this->handlerConfig, *this->templ, *this->db, session, *this->urlProvider, *this->cache); auto handler = handlerFactory->createHandler(r.param("action"), session);
try try
{ {
Response response = handler->handle(r); Response response = handler->handle(r);

Näytä tiedosto

@ -9,25 +9,24 @@
#include "urlprovider.h" #include "urlprovider.h"
#include "database/sessiondao.h" #include "database/sessiondao.h"
#include "cache/fscache.h" #include "cache/fscache.h"
#include "handlers/handlerfactory.h"
class RequestWorker class RequestWorker
{ {
Database *db;
Template *templ; Template *templ;
UrlProvider *urlProvider; UrlProvider *urlProvider;
ICache *cache; ICache *cache;
HandlerConfig *handlerConfig; HandlerFactory *handlerFactory;
std::unique_ptr<SessionDao> sessionDao; SessionDao *sessionDao;
private: private:
Session retrieveSession(std::string token) const; Session retrieveSession(std::string token) const;
public: public:
RequestWorker(HandlerConfig &handlerConfig, Database &db, Template &templ, UrlProvider &provider, ICache &cache) RequestWorker(HandlerFactory &handlerFactory, SessionDao &sessionDao, Template &templ)
{ {
this->handlerConfig = &handlerConfig; this->handlerFactory = &handlerFactory;
this->db = &db;
this->templ = &templ; this->templ = &templ;
this->urlProvider = &provider; this->sessionDao = &sessionDao;
this->sessionDao = db.createSessionDao();
this->cache = &cache;
} }