#include "Config.h" #include #include "common/JsonN.h" #include "common/Spdlogger.h" #include "AppData.h" AppOption Config::option; bool Config::init(std::string filename) { NJsonNode jsonroot; bool ret = NJson::load(filename, jsonroot); if (!ret) { spdlog::error("[config] load config file failed, filename={}", filename); return false; } spdlog::info("[config] load config file success, filename={}", filename); if (jsonroot.contains("database")) { NJsonNode json = jsonroot.at("database"); option.database.host = json.contains("host") ? json.at("host") : ""; option.database.port = json.contains("port") ? json.at("port") : 0; option.database.user = json.contains("user") ? json.at("user") : ""; option.database.passwd = json.contains("passwd") ? json.at("passwd") : ""; option.database.dbname = json.contains("dbname") ? json.at("dbname") : ""; spdlog::info("[config] parse database success. host={}", option.database.host); } else { spdlog::info("[config] parse database failed: not found."); } if (jsonroot.contains("http")) { NJsonNode json = jsonroot.at("http"); std:string token; NJson::read(json, "token", token); option.http.useToken = !token.empty(); NJson::read(json, "port", option.http.port); } else { spdlog::info("[config] parse http failed: not found."); } if (jsonroot.contains("mqtt")) { NJsonNode json = jsonroot.at("mqtt"); NJson::read(json, "host", option.mqtt.host); NJson::read(json, "username", option.mqtt.username); NJson::read(json, "password", option.mqtt.password); } else { spdlog::info("[config] parse mqtt failed: not found."); } return true; }