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