Skip to content

Commit

Permalink
Fixed a problem that occurs when converting JSON to Config, vice versa.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Oct 26, 2021
1 parent fdce909 commit f97a741
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
43 changes: 31 additions & 12 deletions src/projects/config/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,45 @@ namespace cfg

// Try to read configurations from CFG_LAST_CONFIG_FILE_NAME
ov::String last_config_path = ov::PathManager::Combine(config_path, CFG_LAST_CONFIG_FILE_NAME);
ov::String legacy_last_config_path = ov::PathManager::Combine(config_path, CFG_LAST_CONFIG_FILE_NAME_LEGACY);

if (ignore_last_config == false)
{
if (ov::PathManager::IsFile(last_config_path))
{
// Read from last config
logti("Trying to load configurations from last config... (%s)", last_config_path.CStr());

try
DataSource data_source(DataType::Xml, config_path, last_config_path, ROOT_NAME);
_server = std::make_shared<cfg::Server>();
_server->FromDataSource("Server", ROOT_NAME, data_source);

read_from_main_file = false;
}
else
{
if (ov::PathManager::IsFile(legacy_last_config_path))
{
DataSource data_source(DataType::Xml, config_path, last_config_path, ROOT_NAME);
// Read from last config
logti("Trying to load configurations from legacy last config... (%s)", legacy_last_config_path.CStr());

DataSource data_source(DataType::Json, config_path, legacy_last_config_path, ROOT_NAME);
_server = std::make_shared<cfg::Server>();
_server->FromDataSource("Server", ROOT_NAME, data_source);

read_from_main_file = false;
}
catch (const std::shared_ptr<ConfigError> &error)
{
logte("Could not load configurations from last config. (Is file corrupted?): %s", error->ToString().CStr());

logti("Saving migrated config to %s", last_config_path.CStr());

auto xml = _server->ToXml();

SaveCurrentConfig(xml, last_config_path);
}
}
}
else
{
if (ov::PathManager::IsFile(last_config_path))
if (ov::PathManager::IsFile(last_config_path) || ov::PathManager::IsFile(legacy_last_config_path))
{
logtw("Last config is ignored by option");
}
Expand Down Expand Up @@ -136,10 +151,8 @@ namespace cfg
return config;
}

bool ConfigManager::SaveCurrentConfig()
bool ConfigManager::SaveCurrentConfig(pugi::xml_document &config, const ov::String &last_config_path)
{
auto config = GetCurrentConfigAsXml();

auto comment_node = config.prepend_child(pugi::node_comment);
ov::String comment;

Expand Down Expand Up @@ -169,8 +182,6 @@ namespace cfg
declaration.append_attribute("version") = "1.0";
declaration.append_attribute("encoding") = "utf-8";

ov::String last_config_path = ov::PathManager::Combine(_config_path, CFG_LAST_CONFIG_FILE_NAME);

XmlWriter writer;
config.print(writer);

Expand All @@ -187,6 +198,14 @@ namespace cfg
return true;
}

bool ConfigManager::SaveCurrentConfig()
{
auto config = GetCurrentConfigAsXml();
ov::String last_config_path = ov::PathManager::Combine(_config_path, CFG_LAST_CONFIG_FILE_NAME);

return SaveCurrentConfig(config, last_config_path);
}

void ConfigManager::LoadServerID(const ov::String &config_path)
{
{
Expand Down
3 changes: 3 additions & 0 deletions src/projects/config/config_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#define CFG_LOG_FILE_NAME "Logger.xml"
#define CFG_MAIN_FILE_NAME "Server.xml"
#define CFG_LAST_CONFIG_FILE_NAME_LEGACY "LastConfig.json"
#define CFG_LAST_CONFIG_FILE_NAME "LastConfig.xml"
#define SERVER_ID_STORAGE_FILE "Server.id"

Expand Down Expand Up @@ -61,6 +62,8 @@ namespace cfg
MAY_THROWS(std::shared_ptr<ConfigError>)
void CheckValidVersion(const ov::String &name, int version);

bool SaveCurrentConfig(pugi::xml_document &config, const ov::String &last_config_path);

ov::String _version;
ov::String _git_extra;

Expand Down
2 changes: 1 addition & 1 deletion src/projects/config/items/common/cross_domains.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace cfg
protected:
void MakeList() override
{
Register("Url", &_url_list);
Register({"Url", OmitRule::Omit}, &_url_list);
}
};
} // namespace cmn
Expand Down
2 changes: 1 addition & 1 deletion src/projects/config/items/common/host/names.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace cfg
protected:
void MakeList() override
{
Register("Name", &_name_list);
Register({"Name", OmitRule::Omit}, &_name_list);
}
};
} // namespace cmn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace cfg
protected:
void MakeList() override
{
Register<Optional>("Application", &_application_list);
Register<Optional>({"Application", OmitRule::Omit}, &_application_list);
}

std::vector<Application> _application_list;
Expand Down
2 changes: 1 addition & 1 deletion src/projects/config/items/virtual_hosts/virtual_hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace cfg
protected:
void MakeList() override
{
Register("VirtualHost", &_virtual_host_list);
Register({"VirtualHost", OmitRule::Omit}, &_virtual_host_list);
}

std::vector<VirtualHost> _virtual_host_list;
Expand Down

0 comments on commit f97a741

Please sign in to comment.