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:
parent
0ae78577e7
commit
3ee0a607a1
@ -38,7 +38,7 @@ server {
|
||||
cgi_pass /cgi-bin/;
|
||||
cgi_ext .py .php;
|
||||
}
|
||||
|
||||
asdfasdfasdf
|
||||
server {
|
||||
listen 80;
|
||||
server_name mylocal;
|
||||
|
||||
@ -15,6 +15,7 @@ class ConfigManager
|
||||
|
||||
void init(const std::string &filePath);
|
||||
static ConfigManager &getInstance();
|
||||
const std::vector<ServerConfig> &getServerConfigs() const { return serverConfigs; }
|
||||
|
||||
private:
|
||||
bool _initialized;
|
||||
|
||||
@ -12,16 +12,10 @@ LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(fa
|
||||
|
||||
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);
|
||||
}
|
||||
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::string line;
|
||||
while (std::getline(stream, line))
|
||||
@ -31,8 +25,6 @@ void LocationConfig::parseDirectives(const std::string &declarations)
|
||||
lineStream >> directive;
|
||||
if (!directive.empty())
|
||||
{
|
||||
std::cout << "Directive: " << directive << '\n';
|
||||
// Implement the parsing logic here
|
||||
std::string value;
|
||||
lineStream >> value;
|
||||
if (directive == "autoindex")
|
||||
|
||||
@ -41,8 +41,9 @@ void ServerConfig::parseServerBlock(const std::string &block)
|
||||
throw std::runtime_error("Malformed block in config file.");
|
||||
}
|
||||
// Optionally parse the server block here
|
||||
std::string serverBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1);
|
||||
locations.emplace(locationPath, LocationConfig(serverBlock));
|
||||
std::string locationBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1);
|
||||
std::cout << "Added location: " << locationPath << '\n';
|
||||
locations.emplace(locationPath, LocationConfig(locationBlock));
|
||||
pos = closeBrace + 1;
|
||||
}
|
||||
|
||||
@ -52,8 +53,7 @@ void ServerConfig::parseServerBlock(const std::string &block)
|
||||
|
||||
void ServerConfig::parseDirectives(const std::string &declarations)
|
||||
{
|
||||
// Placeholder for actual directives parsing logic
|
||||
std::cout << "Parsing directives:\n" << declarations << '\n';
|
||||
std::cout << "Parsing server directives:\n";
|
||||
std::string line;
|
||||
std::istringstream stream(declarations);
|
||||
while (std::getline(stream, line))
|
||||
@ -63,8 +63,6 @@ void ServerConfig::parseDirectives(const std::string &declarations)
|
||||
lineStream >> directive;
|
||||
if (!directive.empty())
|
||||
{
|
||||
std::cout << "Directive: " << directive << '\n';
|
||||
// Implement the parsing logic here
|
||||
std::string value;
|
||||
lineStream >> value;
|
||||
if (directive == "listen")
|
||||
@ -115,6 +113,30 @@ void ServerConfig::parseDirectives(const std::string &declarations)
|
||||
error_page[statusCode] = errorPagePath;
|
||||
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;
|
||||
}
|
||||
@ -11,7 +11,15 @@ class ServerConfig
|
||||
public:
|
||||
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:
|
||||
std::string host;
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
#include <webserv/config/ConfigManager.hpp>
|
||||
#include <webserv/config/LocationConfig.hpp>
|
||||
#include <webserv/config/ServerConfig.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
@ -13,5 +15,16 @@ int main(int argc, char **argv)
|
||||
}
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user