From 124c8318004c0ca1d8904bf67f41574a83766bcd Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 26 Sep 2025 18:50:40 +0200 Subject: [PATCH] refactor: improve location path extraction and add getValueAs template method --- webserv/config/ServerConfig.cpp | 2 +- webserv/config/directive/ADirective.hpp | 9 +++++++++ webserv/main.cpp | 16 ++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/webserv/config/ServerConfig.cpp b/webserv/config/ServerConfig.cpp index fa6ac2c..1e89bde 100644 --- a/webserv/config/ServerConfig.cpp +++ b/webserv/config/ServerConfig.cpp @@ -32,7 +32,7 @@ void ServerConfig::parseBlock(const std::string &block) directives += block.substr(pos); break; } - std::string locationPath = utils::trim(block.substr(locationPos, bracePos - (locationPos))); + std::string locationPath = utils::trim(block.substr(locationPos + 9, bracePos - (locationPos + 9))); //TODO magic numbers // Add global declarations before this server block directives += block.substr(pos, locationPos - pos); size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos); diff --git a/webserv/config/directive/ADirective.hpp b/webserv/config/directive/ADirective.hpp index a6ee2e5..5930452 100644 --- a/webserv/config/directive/ADirective.hpp +++ b/webserv/config/directive/ADirective.hpp @@ -26,7 +26,16 @@ class ADirective [[nodiscard]] virtual DirectiveValueType getValueType() const = 0; [[nodiscard]] DirectiveValue getValue() const; [[nodiscard]] std::string getName() const; + // [[nodiscard]] std::string toString() const; + template [[nodiscard]] T getValueAs() const + { + if (getValue().holds()) + { + return getValue().get(); + } + return T(); //TODO: does this work for all types? + } protected: std::string name_; diff --git a/webserv/main.cpp b/webserv/main.cpp index 1f28768..c087787 100644 --- a/webserv/main.cpp +++ b/webserv/main.cpp @@ -5,7 +5,6 @@ #include // for basic_ostream, operator<<, cerr, ios_base #include // for map #include // for basic_string, char_traits, allocator, operator+, operator<=> -#include // for pair int main(int argc, char **argv) { @@ -20,7 +19,20 @@ int main(int argc, char **argv) Log::info("\n======================\nStarting webserv...\n======================\n"); Log::warning("Testing context: " + LOCATION, {{"key1", "value1"}, {"key2", "value2"}}); ConfigManager::getInstance().init(argv[1]); // NOLINT - Server server(ConfigManager::getInstance()); + ConfigManager &configManager = ConfigManager::getInstance(); + Log::info("ConfigManager initialized successfully."); + + + auto serverConfigs = configManager.getServerConfigs(); + auto *firstServer = serverConfigs[0]; + const auto *location = firstServer->getLocation("/"); + const auto *listenDirective = location->getDirective("listen"); + int listenPort = listenDirective->getValueAs(); + Log::warning("Listen port for '/' location: " + std::to_string(listenPort)); + + + + Server server(configManager); server.start(); return 0;