2025-07-18 09:08:09 +08:00
|
|
|
|
#include "Config.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/JsonN.h"
|
|
|
|
|
|
#include "Logger.h"
|
2025-08-31 14:38:53 +08:00
|
|
|
|
#include "AppData.h"
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
|
|
|
|
|
AppOption Config::option;
|
|
|
|
|
|
|
|
|
|
|
|
bool Config::init(std::string filename)
|
|
|
|
|
|
{
|
2025-08-26 18:36:25 +08:00
|
|
|
|
NJsonNode jsonroot;
|
|
|
|
|
|
bool ret = NJson::load(filename, jsonroot);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
if (!ret)
|
|
|
|
|
|
{
|
|
|
|
|
|
XLOGE() << "[APP] load config failed, filename=" << filename;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
XLOGI() << "[APP] load config success, filename=" << filename;
|
|
|
|
|
|
|
|
|
|
|
|
if (jsonroot.contains("database"))
|
|
|
|
|
|
{
|
2025-08-26 18:36:25 +08:00
|
|
|
|
NJsonNode json = jsonroot.at("database");
|
2025-07-18 09:08:09 +08:00
|
|
|
|
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") : "";
|
|
|
|
|
|
|
|
|
|
|
|
XLOGI() << "[APP] load database config end. host=" << option.database.host;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
XLOGI() << "[APP] load database config error: not found. host=" << option.database.host;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
if (jsonroot.contains("token"))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string token = jsonroot["token"];
|
|
|
|
|
|
option.useToken = !token.empty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|