Enhance configuration management: add getter methods for server configuration details, improve location handling, and update main to display server information

This commit is contained in:
whaffman 2025-09-14 22:55:20 +02:00
parent 0ae78577e7
commit 3ee0a607a1
6 changed files with 55 additions and 19 deletions

View File

@ -38,7 +38,7 @@ server {
cgi_pass /cgi-bin/; cgi_pass /cgi-bin/;
cgi_ext .py .php; cgi_ext .py .php;
} }
asdfasdfasdf
server { server {
listen 80; listen 80;
server_name mylocal; server_name mylocal;

View File

@ -15,6 +15,7 @@ class ConfigManager
void init(const std::string &filePath); void init(const std::string &filePath);
static ConfigManager &getInstance(); static ConfigManager &getInstance();
const std::vector<ServerConfig> &getServerConfigs() const { return serverConfigs; }
private: private:
bool _initialized; bool _initialized;

View File

@ -5,23 +5,17 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(false) LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(false)
{ {
parseLocationBlock(locationBlock); parseLocationBlock(locationBlock);
} }
void LocationConfig::parseLocationBlock(const std::string &block) void LocationConfig::parseLocationBlock(const std::string &block)
{ {
// Placeholder for actual location block parsing logic
std::cout << "Parsing location block:\n" << block << '\n';
// Implement the parsing logic here
parseDirectives(block); parseDirectives(block);
} }
void LocationConfig::parseDirectives(const std::string &declarations) void LocationConfig::parseDirectives(const std::string &declarations)
{ {
// Placeholder for actual directives parsing logic
std::cout << "Parsing location directives:\n" << declarations << '\n';
// Implement the parsing logic here
std::istringstream stream(declarations); std::istringstream stream(declarations);
std::string line; std::string line;
while (std::getline(stream, line)) while (std::getline(stream, line))
@ -31,8 +25,6 @@ void LocationConfig::parseDirectives(const std::string &declarations)
lineStream >> directive; lineStream >> directive;
if (!directive.empty()) if (!directive.empty())
{ {
std::cout << "Directive: " << directive << '\n';
// Implement the parsing logic here
std::string value; std::string value;
lineStream >> value; lineStream >> value;
if (directive == "autoindex") if (directive == "autoindex")

View File

@ -41,8 +41,9 @@ void ServerConfig::parseServerBlock(const std::string &block)
throw std::runtime_error("Malformed block in config file."); throw std::runtime_error("Malformed block in config file.");
} }
// Optionally parse the server block here // Optionally parse the server block here
std::string serverBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1); std::string locationBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1);
locations.emplace(locationPath, LocationConfig(serverBlock)); std::cout << "Added location: " << locationPath << '\n';
locations.emplace(locationPath, LocationConfig(locationBlock));
pos = closeBrace + 1; pos = closeBrace + 1;
} }
@ -52,8 +53,7 @@ void ServerConfig::parseServerBlock(const std::string &block)
void ServerConfig::parseDirectives(const std::string &declarations) void ServerConfig::parseDirectives(const std::string &declarations)
{ {
// Placeholder for actual directives parsing logic std::cout << "Parsing server directives:\n";
std::cout << "Parsing directives:\n" << declarations << '\n';
std::string line; std::string line;
std::istringstream stream(declarations); std::istringstream stream(declarations);
while (std::getline(stream, line)) while (std::getline(stream, line))
@ -63,8 +63,6 @@ void ServerConfig::parseDirectives(const std::string &declarations)
lineStream >> directive; lineStream >> directive;
if (!directive.empty()) if (!directive.empty())
{ {
std::cout << "Directive: " << directive << '\n';
// Implement the parsing logic here
std::string value; std::string value;
lineStream >> value; lineStream >> value;
if (directive == "listen") if (directive == "listen")
@ -115,6 +113,30 @@ void ServerConfig::parseDirectives(const std::string &declarations)
error_page[statusCode] = errorPagePath; error_page[statusCode] = errorPagePath;
std::cout << "Set error_page for status " << statusCode << " to " << errorPagePath << '\n'; std::cout << "Set error_page for status " << statusCode << " to " << errorPagePath << '\n';
} }
else
{
std::cout << "Unknown directive: " << directive << '\n';
}
} }
} }
} }
const LocationConfig &ServerConfig::getLocation(const std::string &path) const
{
if (locations.count(path) > 0 ) // NOLINT
{
return locations.at(path);
}
throw std::runtime_error("Location not found: " + path);
}
std::vector<std::string> ServerConfig::getLocationPaths() const
{
std::vector<std::string> paths;
paths.reserve(locations.size());
for (const auto &pair : locations)
{
paths.push_back(pair.first);
}
return paths;
}

View File

@ -11,7 +11,15 @@ class ServerConfig
public: public:
ServerConfig(const std::string &serverBlock); ServerConfig(const std::string &serverBlock);
[[nodiscard]] LocationConfig getLocation(const std::string &path) const; [[nodiscard]] const std::string &getHost() const { return host; }
[[nodiscard]] int getPort() const { return port; }
[[nodiscard]] const std::string &getRoot() const { return root; }
[[nodiscard]] const std::string &getCgiPass() const { return cgi_pass; }
[[nodiscard]] const std::string &getCgiExt() const { return cgi_ext; }
[[nodiscard]] const std::map<int, std::string> &getErrorPages() const { return error_page; }
[[nodiscard]] const std::vector<std::string> &getIndexFiles() const { return index_files; }
[[nodiscard]] const LocationConfig &getLocation(const std::string &path) const;
[[nodiscard]] std::vector<std::string> getLocationPaths() const;
private: private:
std::string host; std::string host;

View File

@ -1,4 +1,6 @@
#include <webserv/config/ConfigManager.hpp> #include <webserv/config/ConfigManager.hpp>
#include <webserv/config/LocationConfig.hpp>
#include <webserv/config/ServerConfig.hpp>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -8,10 +10,21 @@ int main(int argc, char **argv)
if (argc < 2) if (argc < 2)
{ {
std::cerr << "Usage: " << argv[0] << " <config_file_path>\n"; //NOLINT std::cerr << "Usage: " << argv[0] << " <config_file_path>\n"; // NOLINT
return 1; return 1;
} }
ConfigManager::getInstance().init(argv[1]); //NOLINT ConfigManager::getInstance().init(argv[1]); // NOLINT
const auto &serverConfigs = ConfigManager::getInstance().getServerConfigs();
for (const auto &serverConfig : serverConfigs)
{
std::cout << "Server " << serverConfig.getHost() << " listening on port: " << serverConfig.getPort() << '\n';
for (const auto &path : serverConfig.getLocationPaths())
{
std::cout << " Location: " << path << '\n';
}
}
return 0; return 0;
} }