🏗️」 wip: new config should be working

This commit is contained in:
2025-04-14 13:25:20 +02:00
parent c21612d873
commit 81b837eceb
6 changed files with 104 additions and 87 deletions

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Config.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */
/* Updated: 2025/04/14 12:55:46 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "config/default.hpp"
namespace webserv {
namespace config {
class Config {
public:
Config(std::string &);
~Config();
Logger *getLogger(void) { return _log; }
std::vector<Server *> *getServers(void) { return _servers; }
private:
toml::ANode *_table;
Logger *_log;
std::vector<Server *> *_servers;
};
}; // namespace config
}; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
/* Updated: 2025/03/25 17:56:34 by adjoly ### ########.fr */
/* Updated: 2025/04/14 12:39:17 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ namespace config {
class Server {
public:
Server(std::string);
Server(toml::ANode *, Logger *);
~Server();
/**
@ -48,11 +48,6 @@ class Server {
return not_nullptr;
}
/**
* @brief Can be used to get the Logger pointer
*/
Logger *getLogger(void) { return _log; }
// @brief Can be used to get a server name
std::vector<std::string> *getServerNames(void) { return _server_names; }
// @brief Can be used to get the host specified in the config file
@ -79,13 +74,6 @@ class Server {
std::map<int, std::string> *
_parseErrPages(std::map<std::string, toml::ANode *> *table);
/**
* @brief Can be used to get the [server] table in _table
*
* @return A pointer to the [server] table as an ANode
*/
toml::ANode *_getServerTable(void);
};
} // namespace config

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */
/* Updated: 2025/03/26 08:39:08 by adjoly ### ########.fr */
/* Updated: 2025/04/14 12:54:29 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#include "Route.hpp"
#include "Server.hpp"
#include "Config.hpp"
#include "cppeleven.hpp"
#include "node/Table.hpp"
#include "node/default.hpp"

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
/* Updated: 2025/04/11 15:49:38 by adjoly ### ########.fr */
/* Updated: 2025/04/14 13:03:31 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,8 +25,7 @@ namespace webserv {
*
* @note Only work if VERBOSE mode is active
*/
static inline void log(std::string emoji, std::string who,
std::string str) {
static inline void log(std::string emoji, std::string who, std::string str) {
#ifdef VERBOSE
if (who.empty())
std::cout << "" << emoji << "」debug: " << str << std::endl;
@ -40,11 +39,11 @@ static inline void log(std::string emoji, std::string who,
class Logger {
public:
Logger(void) : _ttyOnly(true) {
log("", "Logger", "default constructor called");
}
Logger(const std::string &fileName) : _fileName(fileName) {
log("", "Logger", "filename constructor called");
if (fileName.empty()) {
_ttyOnly = true;
} else {
_file.open(fileName.c_str(), std::ios::app);
if (!_file.is_open() && !_ttyOnly) {
_ttyOnly = true;
@ -52,7 +51,6 @@ class Logger {
} else
_ttyOnly = false;
}
}
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
log("", "Logger", "copy constructor called");

45
src/config/Config.cpp Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Config.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */
/* Updated: 2025/04/14 13:25:09 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "cppeleven.hpp"
#include "node/ANode.hpp"
#include <config/default.hpp>
#include <exception>
using namespace webserv::config;
Config::Config(std::string &filename) {
toml::Toml file(filename);
try {
file.parse();
} catch (std::exception &e) {
throw e;
}
_table = file.getParsedFile();
bool found = false;
void *logFile = _table->access("log_file", toml::STRING, found);
if (found == true && logFile != not_nullptr) {
_log = new Logger(*static_cast<std::string *>(logFile));
} else {
_log = new Logger();
}
std::map<std::string, toml::ANode *> *node = _table->getTable();
for (std::map<std::string, toml::ANode *>::iterator it = node->begin();
it != node->end(); it++) {
Server *srv = new Server(it->second, _log);
_servers->push_back(srv);
}
}

View File

@ -6,84 +6,33 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
/* Updated: 2025/04/11 15:44:20 by adjoly ### ########.fr */
/* Updated: 2025/04/14 12:57:30 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "config/Server.hpp"
#include "config/Route.hpp"
#include "cppeleven.hpp"
#include "log.hpp"
#include "node/ANode.hpp"
#include "node/Table.hpp"
#include "node/default.hpp"
#include "tomlpp.hpp"
#include <config/default.hpp>
#include <exception>
#include <stdexcept>
#include <string>
#include <sys/types.h>
using namespace webserv::config;
toml::ANode *Server::_getServerTable(void) {
toml::ANode *serverT;
std::map<std::string, toml::ANode *>::iterator table =
_table->getTable()->find("server");
if (table == _table->getTable()->end())
throw std::runtime_error(
"could not find any [server] table in config file :(");
else
serverT = table->second;
return serverT;
}
Server::Server(std::string file_name) {
toml::Toml *tomlFile = new toml::Toml(file_name);
try {
tomlFile->parse();
} catch (std::runtime_error &e) {
throw e;
}
Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) {
bool found;
std::map<std::string, toml::ANode *> *map;
_table = tomlFile->getParsedFile();
try {
_table = _getServerTable();
} catch(std::runtime_error &e) {
delete _table;
delete tomlFile;
delete _log;
throw e;
}
void *val = _table->access("log_file", toml::STRING, found);
std::string log_file = "";
if (found == true && val != not_nullptr) {
log_file = *static_cast<std::string *>(val);
}
_log = new Logger(log_file);
// host and port parsing
// host parsing
void *host = accessValue("host", toml::STRING, _table, _log);
if (host != not_nullptr) {
_host = *static_cast<std::string *>(host);
} else {
delete _table;
delete tomlFile;
delete _log;
throw std::runtime_error(
"no host specified - please specify one in server.host");
}
// port parsing
void *port = accessValue("port", toml::INT, _table, _log);
if (host != not_nullptr) {
_port = *static_cast<unsigned short *>(port);
} else {
delete _table;
delete tomlFile;
delete _log;
throw std::runtime_error(
"no port specified - please specify one in server.port");
@ -107,7 +56,8 @@ Server::Server(std::string file_name) {
}
// error_pages parsing
map = static_cast<std::map<std::string, toml::ANode *> *>(
std::map<std::string, toml::ANode *> *map =
static_cast<std::map<std::string, toml::ANode *> *>(
accessValue("error_pages", toml::TABLE, _table, _log));
if (map != not_nullptr) {
_err_pages = _parseErrPages(map);
@ -126,8 +76,7 @@ Server::Server(std::string file_name) {
(*_routes)[it->first] = new Route(it->second, _log);
}
}
delete tomlFile->getParsedFile();
delete tomlFile;
delete _table;
}
Server::~Server(void) {