mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 23:28:46 +02:00
「✨」 feat: new config should be working
This commit is contained in:
35
includes/config/Config.hpp
Normal file
35
includes/config/Config.hpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Config.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2025/04/14 13:36:42 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:
|
||||||
|
Logger *_log;
|
||||||
|
std::vector<Server *> *_servers;
|
||||||
|
};
|
||||||
|
|
||||||
|
}; // namespace config
|
||||||
|
}; // namespace webserv
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
|
/* 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 {
|
class Server {
|
||||||
public:
|
public:
|
||||||
Server(std::string);
|
Server(toml::ANode *, Logger *);
|
||||||
~Server();
|
~Server();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,11 +48,6 @@ class Server {
|
|||||||
return not_nullptr;
|
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
|
// @brief Can be used to get a server name
|
||||||
std::vector<std::string> *getServerNames(void) { return _server_names; }
|
std::vector<std::string> *getServerNames(void) { return _server_names; }
|
||||||
// @brief Can be used to get the host specified in the config file
|
// @brief Can be used to get the host specified in the config file
|
||||||
@ -79,13 +74,6 @@ class Server {
|
|||||||
|
|
||||||
std::map<int, std::string> *
|
std::map<int, std::string> *
|
||||||
_parseErrPages(std::map<std::string, toml::ANode *> *table);
|
_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
|
} // namespace config
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */
|
/* 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 "Route.hpp"
|
||||||
#include "Server.hpp"
|
#include "Server.hpp"
|
||||||
|
#include "Config.hpp"
|
||||||
#include "cppeleven.hpp"
|
#include "cppeleven.hpp"
|
||||||
#include "node/Table.hpp"
|
#include "node/Table.hpp"
|
||||||
#include "node/default.hpp"
|
#include "node/default.hpp"
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
|
/* 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
|
* @note Only work if VERBOSE mode is active
|
||||||
*/
|
*/
|
||||||
static inline void log(std::string emoji, std::string who,
|
static inline void log(std::string emoji, std::string who, std::string str) {
|
||||||
std::string str) {
|
|
||||||
#ifdef VERBOSE
|
#ifdef VERBOSE
|
||||||
if (who.empty())
|
if (who.empty())
|
||||||
std::cout << "「" << emoji << "」debug: " << str << std::endl;
|
std::cout << "「" << emoji << "」debug: " << str << std::endl;
|
||||||
@ -40,18 +39,17 @@ static inline void log(std::string emoji, std::string who,
|
|||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
public:
|
public:
|
||||||
|
Logger(void) : _ttyOnly(true) {
|
||||||
|
log("➕", "Logger", "default constructor called");
|
||||||
|
}
|
||||||
Logger(const std::string &fileName) : _fileName(fileName) {
|
Logger(const std::string &fileName) : _fileName(fileName) {
|
||||||
log("➕", "Logger", "filename constructor called");
|
log("➕", "Logger", "filename constructor called");
|
||||||
if (fileName.empty()) {
|
_file.open(fileName.c_str(), std::ios::app);
|
||||||
|
if (!_file.is_open() && !_ttyOnly) {
|
||||||
_ttyOnly = true;
|
_ttyOnly = true;
|
||||||
} else {
|
warn("could not open log file, going tty only");
|
||||||
_file.open(fileName.c_str(), std::ios::app);
|
} else
|
||||||
if (!_file.is_open() && !_ttyOnly) {
|
_ttyOnly = false;
|
||||||
_ttyOnly = true;
|
|
||||||
warn("could not open log file, going tty only");
|
|
||||||
} else
|
|
||||||
_ttyOnly = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
|
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
|
||||||
|
48
src/config/Config.cpp
Normal file
48
src/config/Config.cpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Config.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2025/04/14 13:36:46 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "node/ANode.hpp"
|
||||||
|
#include <config/default.hpp>
|
||||||
|
|
||||||
|
using namespace webserv::config;
|
||||||
|
|
||||||
|
Config::Config(std::string &filename) {
|
||||||
|
toml::Toml *file = new toml::Toml(filename);
|
||||||
|
|
||||||
|
file->parse();
|
||||||
|
toml::ANode *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);
|
||||||
|
}
|
||||||
|
delete table;
|
||||||
|
delete file;
|
||||||
|
}
|
||||||
|
|
||||||
|
Config::~Config(void) {
|
||||||
|
std::vector<Server *>::iterator it = _servers->begin();
|
||||||
|
for (; it != _servers->end() ; it++) {
|
||||||
|
delete *it;
|
||||||
|
}
|
||||||
|
delete _servers;
|
||||||
|
}
|
@ -6,84 +6,33 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
|
/* 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 <config/default.hpp>
|
||||||
#include <exception>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
using namespace webserv::config;
|
using namespace webserv::config;
|
||||||
|
|
||||||
toml::ANode *Server::_getServerTable(void) {
|
Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) {
|
||||||
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;
|
|
||||||
}
|
|
||||||
bool found;
|
bool found;
|
||||||
|
|
||||||
std::map<std::string, toml::ANode *> *map;
|
// host parsing
|
||||||
_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
|
|
||||||
void *host = accessValue("host", toml::STRING, _table, _log);
|
void *host = accessValue("host", toml::STRING, _table, _log);
|
||||||
if (host != not_nullptr) {
|
if (host != not_nullptr) {
|
||||||
_host = *static_cast<std::string *>(host);
|
_host = *static_cast<std::string *>(host);
|
||||||
} else {
|
} else {
|
||||||
delete _table;
|
delete _table;
|
||||||
delete tomlFile;
|
|
||||||
delete _log;
|
delete _log;
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"no host specified - please specify one in server.host");
|
"no host specified - please specify one in server.host");
|
||||||
}
|
}
|
||||||
|
// port parsing
|
||||||
void *port = accessValue("port", toml::INT, _table, _log);
|
void *port = accessValue("port", toml::INT, _table, _log);
|
||||||
if (host != not_nullptr) {
|
if (host != not_nullptr) {
|
||||||
_port = *static_cast<unsigned short *>(port);
|
_port = *static_cast<unsigned short *>(port);
|
||||||
} else {
|
} else {
|
||||||
delete _table;
|
delete _table;
|
||||||
delete tomlFile;
|
|
||||||
delete _log;
|
delete _log;
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"no port specified - please specify one in server.port");
|
"no port specified - please specify one in server.port");
|
||||||
@ -102,13 +51,14 @@ Server::Server(std::string file_name) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_log->warn(
|
_log->warn(
|
||||||
"no server_names all request will be accepted from any hostname");
|
"no server_names all request will be accepted from any hostname");
|
||||||
_server_names = not_nullptr;
|
_server_names = not_nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// error_pages parsing
|
// error_pages parsing
|
||||||
map = static_cast<std::map<std::string, toml::ANode *> *>(
|
std::map<std::string, toml::ANode *> *map =
|
||||||
accessValue("error_pages", toml::TABLE, _table, _log));
|
static_cast<std::map<std::string, toml::ANode *> *>(
|
||||||
|
accessValue("error_pages", toml::TABLE, _table, _log));
|
||||||
if (map != not_nullptr) {
|
if (map != not_nullptr) {
|
||||||
_err_pages = _parseErrPages(map);
|
_err_pages = _parseErrPages(map);
|
||||||
} else
|
} else
|
||||||
@ -126,8 +76,7 @@ Server::Server(std::string file_name) {
|
|||||||
(*_routes)[it->first] = new Route(it->second, _log);
|
(*_routes)[it->first] = new Route(it->second, _log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete tomlFile->getParsedFile();
|
delete _table;
|
||||||
delete tomlFile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server(void) {
|
Server::~Server(void) {
|
||||||
|
Reference in New Issue
Block a user