Sfoglia il codice sorgente

feat(config): minimal bootstrap config with defaults and file load

Fszontagh 2 mesi fa
parent
commit
2300762f13
4 ha cambiato i file con 84 aggiunte e 1 eliminazioni
  1. 29 1
      src/config.cpp
  2. 19 0
      src/config.hpp
  3. 4 0
      tests/CMakeLists.txt
  4. 32 0
      tests/test_config.cpp

+ 29 - 1
src/config.cpp

@@ -1 +1,29 @@
-// placeholder until Task 2
+#include "config.hpp"
+#include <fstream>
+#include <stdexcept>
+
+namespace svapi {
+
+Config Config::fromJson(const nlohmann::json& j) {
+    Config c;
+    c.logLevel = j.value("log_level", c.logLevel);
+    if (j.contains("http") && j["http"].is_object()) {
+        const auto& h = j["http"];
+        c.httpBind = h.value("bind_address", c.httpBind);
+        c.httpPort = h.value("port", c.httpPort);
+    }
+    if (j.contains("database") && j["database"].is_object()) {
+        c.dbAddress = j["database"].value("address", c.dbAddress);
+    }
+    return c;
+}
+
+Config Config::load(const std::string& path) {
+    std::ifstream f(path);
+    if (!f) throw std::runtime_error("cannot open config file: " + path);
+    nlohmann::json j;
+    f >> j;
+    return fromJson(j);
+}
+
+} // namespace svapi

+ 19 - 0
src/config.hpp

@@ -0,0 +1,19 @@
+#pragma once
+#include <cstdint>
+#include <string>
+#include <nlohmann/json.hpp>
+
+namespace svapi {
+
+struct Config {
+    std::string logLevel  = "info";
+    std::string httpBind  = "0.0.0.0";
+    uint16_t    httpPort  = 8080;
+    std::string dbAddress = "localhost:9004";
+    std::string webuiDir  = "/usr/share/smartbotic-vectorapi/webui";
+
+    static Config fromJson(const nlohmann::json& j);
+    static Config load(const std::string& path);  // reads file then fromJson
+};
+
+} // namespace svapi

+ 4 - 0
tests/CMakeLists.txt

@@ -4,3 +4,7 @@ include(GoogleTest)
 add_executable(test_smoke test_smoke.cpp)
 target_link_libraries(test_smoke PRIVATE vectorapi_core GTest::gtest_main)
 gtest_discover_tests(test_smoke)
+
+add_executable(test_config test_config.cpp)
+target_link_libraries(test_config PRIVATE vectorapi_core GTest::gtest_main)
+gtest_discover_tests(test_config)

+ 32 - 0
tests/test_config.cpp

@@ -0,0 +1,32 @@
+#include <gtest/gtest.h>
+#include "config.hpp"
+
+using svapi::Config;
+
+TEST(Config, DefaultsWhenEmpty) {
+    Config c = Config::fromJson(nlohmann::json::object());
+    EXPECT_EQ(c.logLevel, "info");
+    EXPECT_EQ(c.httpBind, "0.0.0.0");
+    EXPECT_EQ(c.httpPort, 8080);
+    EXPECT_EQ(c.dbAddress, "localhost:9004");
+}
+
+TEST(Config, ParsesProvidedValues) {
+    auto j = nlohmann::json::parse(R"({
+        "log_level":"debug",
+        "http":{"bind_address":"127.0.0.1","port":9090},
+        "database":{"address":"db:9004"}
+    })");
+    Config c = Config::fromJson(j);
+    EXPECT_EQ(c.logLevel, "debug");
+    EXPECT_EQ(c.httpBind, "127.0.0.1");
+    EXPECT_EQ(c.httpPort, 9090);
+    EXPECT_EQ(c.dbAddress, "db:9004");
+}
+
+TEST(Config, IgnoresUnknownKeys) {
+    auto j = nlohmann::json::parse(R"({"nonsense":true,"http":{"port":1234}})");
+    Config c = Config::fromJson(j);
+    EXPECT_EQ(c.httpPort, 1234);
+    EXPECT_EQ(c.httpBind, "0.0.0.0");
+}