refactor: improve location path extraction and add getValueAs template method

This commit is contained in:
whaffman 2025-09-26 18:50:40 +02:00
parent 6abe64f9b7
commit 124c831800
3 changed files with 24 additions and 3 deletions

View File

@ -32,7 +32,7 @@ void ServerConfig::parseBlock(const std::string &block)
directives += block.substr(pos); directives += block.substr(pos);
break; 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 // Add global declarations before this server block
directives += block.substr(pos, locationPos - pos); directives += block.substr(pos, locationPos - pos);
size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos); size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos);

View File

@ -26,7 +26,16 @@ class ADirective
[[nodiscard]] virtual DirectiveValueType getValueType() const = 0; [[nodiscard]] virtual DirectiveValueType getValueType() const = 0;
[[nodiscard]] DirectiveValue getValue() const; [[nodiscard]] DirectiveValue getValue() const;
[[nodiscard]] std::string getName() const; [[nodiscard]] std::string getName() const;
// [[nodiscard]] std::string toString() const; // [[nodiscard]] std::string toString() const;
template <typename T> [[nodiscard]] T getValueAs() const
{
if (getValue().holds<T>())
{
return getValue().get<T>();
}
return T(); //TODO: does this work for all types?
}
protected: protected:
std::string name_; std::string name_;

View File

@ -5,7 +5,6 @@
#include <iostream> // for basic_ostream, operator<<, cerr, ios_base #include <iostream> // for basic_ostream, operator<<, cerr, ios_base
#include <map> // for map #include <map> // for map
#include <string> // for basic_string, char_traits, allocator, operator+, operator<=> #include <string> // for basic_string, char_traits, allocator, operator+, operator<=>
#include <utility> // for pair
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -20,7 +19,20 @@ int main(int argc, char **argv)
Log::info("\n======================\nStarting webserv...\n======================\n"); Log::info("\n======================\nStarting webserv...\n======================\n");
Log::warning("Testing context: " + LOCATION, {{"key1", "value1"}, {"key2", "value2"}}); Log::warning("Testing context: " + LOCATION, {{"key1", "value1"}, {"key2", "value2"}});
ConfigManager::getInstance().init(argv[1]); // NOLINT 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<int>();
Log::warning("Listen port for '/' location: " + std::to_string(listenPort));
Server server(configManager);
server.start(); server.start();
return 0; return 0;