Skip to content

Commit

Permalink
feat: Implement Config to read config file and apply some config on S…
Browse files Browse the repository at this point in the history
…erver

- Implemented Config class into singleton instance.
- Implemented Config object to read config file.
- Implemented Config object to store config data as properties.
- Need to implement to parse and store data from Locations.
    - Better to pass data and parsing process to up-coming Location class to do it.
- Updated Server class source code to apply config data into Server object.
- Updated main function to call Config as singleton, load config file, and pass Config instance to Server.
  • Loading branch information
san-ghun committed Jul 18, 2024
1 parent 9e80b49 commit 7d1e48d
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 59 deletions.
54 changes: 41 additions & 13 deletions include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,64 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/12 16:28:59 by sanghupa ### ########.fr */
/* Updated: 2024/07/17 15:36:40 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef CONFIG_HPP
# define CONFIG_HPP

# include <string>
# include <vector>
# include <map>
# include "Location.hpp"

class Location;

class Config
{
public:
// Singleton: Static method to get the single instance of the class
static Config& getInstance();

// Deleted copy constructor and copy assignment operator to prevent copying
// Config(const Config&) = delete;
// void operator=(const Config&) = delete;

void load(const std::string& filename);

std::string get(const std::string key) const;
int getInt(const std::string key) const;
bool getBool(const std::string key) const;
std::string getServerHost() const;
int getPort() const;

std::map<std::string, std::string> getMimeTypeMap() const;
std::map<int, std::string> getErrorPageMap() const;
std::map<std::string, std::string> getServerSettingMap() const;
std::map<std::string, Location> getLocationMap() const;
std::map<std::string, std::string> getConfigMap() const;

void setLocation(const std::string key);

private:
// Singleton: Private constructor and destructor
Config();
~Config();

static void load(const std::string filename);
static std::string get(const std::string key);
static int getInt(const std::string key);
static int getPort();
static Location getLocation(const std::string key);
void _parseKeyValuePair(const std::string line);
void _parseBlock(std::istream& stream, const std::string& blockName);
void _parseConfigFile(const std::string& filename);
void _setHostPort();

static std::map<std::string, std::string> getConfigMap();
std::map<std::string, std::string> _mimeTypeMap;
std::map<int, std::string> _errorPageMap;
std::map<std::string, std::string> _serverSettingMap;
std::map<std::string, Location> _locationMap;
std::map<std::string, std::string> _configMap;

private:
static std::map<std::string, Location> _locationsMap;
static std::map<std::string, std::string> _configMap;

static void _parseConfigFile(const std::string filename);
int _port;
std::string _host;
};

#endif
#endif
14 changes: 7 additions & 7 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:46 by sanghupa #+# #+# */
/* Updated: 2024/07/12 15:16:05 by minakim ### ########.fr */
/* Updated: 2024/07/16 22:22:08 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,13 +15,13 @@

// # include "webserv.hpp"
# include <vector>
// # include "Config.hpp"
# include "Config.hpp"
# include "Location.hpp"
# include "Socket.hpp"
# include "Poller.hpp"
# include "RequestHandler.hpp"

// class Config;
class Config;
class Location;
class Socket;
class Poller;
Expand All @@ -30,7 +30,7 @@ class Poller;
class Server
{
public:
// Server(Config config);
Server(Config& config);
Server(int port);
~Server();

Expand All @@ -55,7 +55,7 @@ class Server
// Setters
void setServerNames(std::vector<std::string> serverNames);
void setServerHost(std::string serverHost);
void setServerPort(std::string serverPort);
void setServerPort(int serverPort);
void setMaxBodySize(size_t maxBodySize);
// void setMaxConnection(size_t maxConnection);
// void setMaxHeader(size_t maxHeader);
Expand Down Expand Up @@ -83,7 +83,7 @@ class Server

std::vector<std::string> _serverNames;
std::string _serverHost;
std::string _serverPort;
int _serverPort;
size_t _maxBodySize;
// size_t _maxConnection;
// size_t _maxHeader;
Expand Down
14 changes: 7 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 21:05:14 by sanghupa #+# #+# */
/* Updated: 2024/07/12 15:06:39 by minakim ### ########.fr */
/* Updated: 2024/07/16 22:22:12 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

#include "webserv.hpp"
// #include "util/Config.hpp"
#include "Config.hpp"
// #include "util/Logger.hpp"
#include "Server.hpp"

Expand Down Expand Up @@ -50,15 +50,15 @@ int main(int argc, char *argv[])
// Set the signal handler for SIGINT signal
signal(SIGINT, ft_sigint_handler);

(void)argv;
// try-catch block for error handling
// Create a Config object with the provided configuration file
// Config config(argv[1]);
Config& config = Config::getInstance();
config.load(argv[1]);
// Create a Logger object with the Config object
// Logger logger(config);
// Create a Server object with the Config object
// Server server(config);
Server server(8080);
Server server(config);
// Server server(8080);
// Start the server
server.start();

Expand Down
32 changes: 16 additions & 16 deletions src/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:46 by sanghupa #+# #+# */
/* Updated: 2024/07/14 21:52:18 by sanghupa ### ########.fr */
/* Updated: 2024/07/16 22:31:24 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -77,18 +77,18 @@ static std::string handle_request(const std::string& request)
// ----------------------------------------------------

// TODO: Implement Config
// Server::Server(Config config)
// : _serverPort(std::to_string(config.getPort()))
// , _maxBodySize(0)
// {}

Server::Server(int port)
: _serverPort(std::string("8080"))
Server::Server(Config& config)
: _serverPort(config.getPort())
, _maxBodySize(1024)
{
(void)port;
// TODO: Apply Config
}

Server::Server(int port)
: _serverPort(port)
, _maxBodySize(1024)
{}

Server::~Server()
{
stop();
Expand Down Expand Up @@ -127,8 +127,8 @@ void Server::start()
{
try {
// TODO: Implement and replace stoi() function
// _listenSocket.bind(stoi(_serverPort));
_listenSocket.bind(8080);
_listenSocket.bind(_serverPort);
// _listenSocket.bind(8080);
_listenSocket.listen();
_listenSocket.set_nonblocking();
_pollfds.push_back((struct pollfd){_listenSocket.getFd(), POLLIN, 0});
Expand Down Expand Up @@ -298,10 +298,10 @@ std::string Server::getServerHost() const
return (_serverHost);
}

std::string Server::getServerPort() const
{
return (_serverPort);
}
// std::string Server::getServerPort() const
// {
// return (std::to_string_serverPort));
// }

size_t Server::getMaxBodySize() const
{
Expand Down Expand Up @@ -357,7 +357,7 @@ void Server::setServerHost(std::string serverHost)
_serverHost = serverHost;
}

void Server::setServerPort(std::string serverPort)
void Server::setServerPort(int serverPort)
{
_serverPort = serverPort;
}
Expand Down
Loading

0 comments on commit 7d1e48d

Please sign in to comment.