Skip to content

Commit

Permalink
[core] Add visit() to settings
Browse files Browse the repository at this point in the history
Fix impl
  • Loading branch information
zach2good committed Dec 17, 2023
1 parent 85a8b63 commit 4a92fa0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,12 @@ namespace settings
//
// lua.safe_script("require('settings/main'); require('settings/default/main'); print(xi.settings)");
}

void visit(std::function<void(std::string, SettingsVariant_t)> visitor)
{
for (auto& [key, value] : settingsMap)
{
visitor(key, value);
}
}
} // namespace settings
2 changes: 2 additions & 0 deletions src/common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ namespace settings
ShowError(fmt::format("Settings: Failed to look up key: {}, using default value: \"{}\"", name, out));
return T();
}

void visit(std::function<void(std::string, SettingsVariant_t)> visitor);
} // namespace settings

#endif // _SETTINGS_H
17 changes: 17 additions & 0 deletions src/common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,3 +988,20 @@ void crash()
// cppcheck-suppress nullPointer
*ptr = 0xDEAD;
}

std::unique_ptr<FILE> utils::openFile(std::string const& path, std::string const& mode)
{
return std::unique_ptr<FILE>(fopen(path.c_str(), mode.c_str()));
}

std::string utils::toASCII(std::string const& target, char replacement)
{
std::string out;
out.reserve(target.size());
for (auto ch : target)
{
bool isASCII = ch >= 0 && ch <= 127;
out += isASCII ? ch : replacement;
}
return out;
}
6 changes: 6 additions & 0 deletions src/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,10 @@ std::set<std::filesystem::path> sorted_directory_iterator(std::string path_name)
return sorted_by_name;
}

namespace utils
{
auto openFile(std::string const& path, std::string const& mode) -> std::unique_ptr<FILE>;
auto toASCII(std::string const& target, char replacement = '\0') -> std::string;
} // utils

#endif
55 changes: 49 additions & 6 deletions src/world/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include "common/sql.h"
#include "common/utils.h"

#include <unordered_set>

#include <nlohmann/json.hpp>
using json = nlohmann::json;

Expand Down Expand Up @@ -94,17 +96,58 @@ HTTPServer::HTTPServer()
m_httpServer.Get("/api/settings", [&](httplib::Request const& req, httplib::Response& res)
{
// TODO: Cache these
json j;
j["SERVER_NAME"] = settings::get<std::string>("main.SERVER_NAME");
j["EXP_RATE"] = settings::get<float>("main.EXP_RATE");
j["ENABLE_TRUST_CASTING"] = settings::get<bool>("main.ENABLE_TRUST_CASTING");
j["MAX_LEVEL"] = settings::get<uint8>("main.MAX_LEVEL");
json j{};

// Filter out settings we don't want to expose
std::unordered_set<std::string> textToOmit{
"logging.",
"network.",
"password", // Just in case
};

settings::visit([&](auto const& key, auto const& variant)
{
for (auto const& text : textToOmit)
{
// NOTE: Remember that keys are stored as uppercase
if (key.find(to_upper(text)) != std::string::npos)
{
return;
}
}

std::visit(
settings::overloaded
{
[&](bool const& arg)
{
j[key] = arg;
},
[&](double const& arg)
{
j[key] = arg;
},
[&](std::string const& arg)
{
// JSON can't handle non-ASCII characters, so strip them out
j[key] = utils::toASCII(arg, '?');
},
}, variant);
});

res.set_content(j.dump(), "application/json");
});

m_httpServer.set_error_handler([](httplib::Request const& /*req*/, httplib::Response& res)
{
auto str = fmt::format("<p>Error Status: <span style='color:red;'>{} ({})</span></p>", res.status, httplib::detail::status_message(res.status));
auto str = fmt::format("<p>Error Status: <span style='color:red;'>{} ({})</span></p>",
res.status, httplib::detail::status_message(res.status));

for (auto const& [key, val] : res.headers)
{
str += fmt::format("<p>{}: {}</p>", key, val);
}

res.set_content(str, "text/html");
});

Expand Down

0 comments on commit 4a92fa0

Please sign in to comment.