Inject server configuration instead of using a singleton
This commit is contained in:
@@ -1,18 +1,5 @@
|
||||
#include "configuration.hpp"
|
||||
|
||||
bool ServerConfiguration::LoadFromFile(std::string const & filePath)
|
||||
{
|
||||
// TODO implement
|
||||
return false;
|
||||
}
|
||||
|
||||
ServerConfiguration::ServerConfiguration()
|
||||
: wwwRoot("./www"),
|
||||
serverName("http-server"),
|
||||
port(8080)
|
||||
{
|
||||
}
|
||||
|
||||
int ServerConfiguration::GetMajorVersion() const
|
||||
{
|
||||
return 0;
|
||||
@@ -43,9 +30,15 @@ bool ServerConfiguration::IsValid() const
|
||||
return isValid;
|
||||
}
|
||||
|
||||
ServerConfiguration const & ServerConfiguration::GetInstance()
|
||||
bool ServerConfiguration::LoadFromFile(std::string const & filePath)
|
||||
{
|
||||
static ServerConfiguration config;
|
||||
// TODO implement
|
||||
return false;
|
||||
}
|
||||
|
||||
return config;
|
||||
ServerConfiguration::ServerConfiguration()
|
||||
: wwwRoot("./www"),
|
||||
serverName("http-server"),
|
||||
port(8080)
|
||||
{
|
||||
}
|
||||
@@ -9,11 +9,6 @@ private:
|
||||
int port;
|
||||
bool isValid;
|
||||
|
||||
bool LoadFromFile(std::string const & filePath);
|
||||
|
||||
ServerConfiguration();
|
||||
~ServerConfiguration() = default;
|
||||
|
||||
public:
|
||||
int GetMajorVersion() const;
|
||||
int GetMinorVersion() const;
|
||||
@@ -22,7 +17,10 @@ public:
|
||||
int GetPort() const;
|
||||
bool IsValid() const;
|
||||
|
||||
static ServerConfiguration const & GetInstance();
|
||||
bool LoadFromFile(std::string const & filePath);
|
||||
|
||||
ServerConfiguration();
|
||||
~ServerConfiguration() = default;
|
||||
|
||||
ServerConfiguration(ServerConfiguration & other) = delete;
|
||||
ServerConfiguration(ServerConfiguration && other) = delete;
|
||||
|
||||
@@ -65,11 +65,11 @@ void ConnectionOperator::HandleNewConnection(ClientSocket const & newClient)
|
||||
newClient.WriteBytes(bytesToSend);
|
||||
}
|
||||
|
||||
ConnectionOperator::ConnectionOperator(Logger & _logger)
|
||||
ConnectionOperator::ConnectionOperator(Logger & _logger, ServerConfiguration const & serverConfiguration)
|
||||
: logger(_logger)
|
||||
{
|
||||
// Base static file server
|
||||
auto const & staticFileRoot = ServerConfiguration::GetInstance().GetWwwRoot();
|
||||
auto const & staticFileRoot = serverConfiguration.GetWwwRoot();
|
||||
if (staticFileRoot.size() > 0)
|
||||
{
|
||||
middlewares.emplace_back(std::make_unique<Middleware::StaticContent>(_logger, staticFileRoot));
|
||||
|
||||
@@ -17,5 +17,5 @@ private:
|
||||
public:
|
||||
void HandleNewConnection(ClientSocket const & newClient);
|
||||
|
||||
ConnectionOperator(Logger & logger);
|
||||
ConnectionOperator(Logger & logger, ServerConfiguration const & serverConfiguration);
|
||||
};
|
||||
@@ -20,13 +20,13 @@ void HttpServer::Execute()
|
||||
}
|
||||
}
|
||||
|
||||
HttpServer::HttpServer(Logger & _logger)
|
||||
HttpServer::HttpServer(Logger & _logger, ServerConfiguration const & serverConfiguration)
|
||||
: logger(_logger),
|
||||
listeningSocket(ServerConfiguration::GetInstance().GetPort()),
|
||||
connectionOperator(_logger),
|
||||
listeningSocket(serverConfiguration.GetPort()),
|
||||
connectionOperator(_logger, serverConfiguration),
|
||||
isOpen(true)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Listening on port " << ServerConfiguration::GetInstance().GetPort();
|
||||
ss << "Listening on port " << serverConfiguration.GetPort();
|
||||
logger.Info(ss.str());
|
||||
}
|
||||
@@ -14,7 +14,7 @@ private:
|
||||
public:
|
||||
void Execute();
|
||||
|
||||
HttpServer(Logger & logger);
|
||||
HttpServer(Logger & logger, ServerConfiguration const & serverConfiguration);
|
||||
HttpServer(HttpServer & other) = delete;
|
||||
HttpServer & operator=(HttpServer & other) = delete;
|
||||
};
|
||||
Reference in New Issue
Block a user