Inject logger instead of using a singleton

This commit is contained in:
2019-06-16 11:33:23 +02:00
parent 14740e4a64
commit 9ba225cbde
14 changed files with 76 additions and 56 deletions

View File

@@ -15,7 +15,7 @@ void ConnectionOperator::SendResponse(Connection const & connection, Http::Respo
}
catch(std::runtime_error & e)
{
Logger::GetInstance().Error("Error writing data to connection");
logger.Error("Error writing data to connection");
}
}
@@ -34,7 +34,7 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
ss << "Error during parsing of request <";
ss << e.what();
ss << '>';
Logger::GetInstance().Error(ss.str());
logger.Error(ss.str());
response.code = HttpResponse::Code::BAD_REQUEST;
SendResponse(newConnection, response);
@@ -54,7 +54,7 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
ss << " request for file <";
ss << request.url.GetPath();
ss << '>';
Logger::GetInstance().Error(ss.str());
logger.Error(ss.str());
response.code = HttpResponse::Code::NOT_IMPLEMENTED;
SendResponse(newConnection, response);
@@ -65,15 +65,16 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
newConnection.WriteBytes(bytesToSend);
}
ConnectionOperator::ConnectionOperator()
ConnectionOperator::ConnectionOperator(Logger & _logger)
: logger(_logger)
{
// Base static file server
auto const & staticFileRoot = ServerConfiguration::GetInstance().GetWwwRoot();
if (staticFileRoot.size() > 0)
{
middlewares.emplace_back(std::make_unique<Middleware::StaticContent>(staticFileRoot));
middlewares.emplace_back(std::make_unique<Middleware::StaticContent>(_logger, staticFileRoot));
}
// ALWAYS LAST!
middlewares.emplace_back(std::make_unique<Middleware::NotFound>());
middlewares.emplace_back(std::make_unique<Middleware::NotFound>(_logger));
}

View File

@@ -1,4 +1,5 @@
#pragma once
#include "../logger.hpp"
#include "../middleware/base.hpp"
#include "connection.hpp"
#include <memory>
@@ -8,6 +9,7 @@
class ConnectionOperator
{
private:
Logger & logger;
std::vector<std::unique_ptr<Middleware::BaseMiddleware>> middlewares;
void SendResponse(Connection const & connection, Http::Response const & response) const;
@@ -15,5 +17,5 @@ private:
public:
void HandleNewConnection(Connection const & newConnection);
ConnectionOperator();
ConnectionOperator(Logger & logger);
};

View File

@@ -1,4 +1,5 @@
#pragma once
#include "../logger.hpp"
#include "connection.hpp"
#include <sys/socket.h>
#include <netinet/in.h>

View File

@@ -15,17 +15,18 @@ void HttpServer::Execute()
}
catch (std::runtime_error & e)
{
Logger::GetInstance().Info("Connection dropped on accept");
logger.Info("Connection dropped on accept");
}
}
}
HttpServer::HttpServer()
: listeningSocket(ServerConfiguration::GetInstance().GetPort()),
connectionOperator(),
HttpServer::HttpServer(Logger & _logger)
: logger(_logger),
listeningSocket(ServerConfiguration::GetInstance().GetPort()),
connectionOperator(_logger),
isOpen(true)
{
std::stringstream ss;
ss << "Listening on port " << ServerConfiguration::GetInstance().GetPort();
Logger::GetInstance().Info(ss.str());
logger.Info(ss.str());
}

View File

@@ -1,10 +1,12 @@
#pragma once
#include "../logger.hpp"
#include "connectionoperator.hpp"
#include "listeningsocket.hpp"
class HttpServer
{
private:
Logger & logger;
ListeningSocket listeningSocket;
ConnectionOperator connectionOperator;
bool isOpen;
@@ -12,7 +14,7 @@ private:
public:
void Execute();
HttpServer();
HttpServer(Logger & logger);
HttpServer(HttpServer & other) = delete;
HttpServer & operator=(HttpServer & other) = delete;
};