Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

feat: add http interface to get a specified config #719

Merged
merged 7 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/dsn/utility/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,7 @@ extern bool has_tag(const std::string &name, const flag_tag &tag);

// list all the flags
extern std::string list_all_flags();

// get the json string of a specified flag
extern error_with<std::string> get_flag_str(const std::string &flag_name);
} // namespace dsn
4 changes: 4 additions & 0 deletions src/http/builtin_http_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ namespace dsn {
[](const http_request &req, http_response &resp) { update_config(req, resp); })
.with_help("Updates the value of a config");

register_http_call("config")
.with_callback([](const http_request &req, http_response &resp) { get_config(req, resp); })
.with_help("get the value of a config");

register_http_call("configs")
.with_callback(
[](const http_request &req, http_response &resp) { list_all_configs(req, resp); })
Expand Down
1 change: 1 addition & 0 deletions src/http/builtin_http_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ extern void update_config(const http_request &req, http_response &resp);

extern void list_all_configs(const http_request &req, http_response &resp);

extern void get_config(const http_request &req, http_response &resp);
} // namespace dsn
21 changes: 21 additions & 0 deletions src/http/config_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,25 @@ void list_all_configs(const http_request &req, http_response &resp)
resp.body = list_all_flags();
resp.status_code = http_status_code::ok;
}

void get_config(const http_request &req, http_response &resp)
{
std::string config_name;
for (const auto &p : req.query_args) {
if ("name" == p.first) {
config_name = p.second;
} else {
resp.status_code = http_status_code::bad_request;
return;
}
}

auto res = get_flag_str(config_name);
if (res.is_ok()) {
resp.body = res.get_value();
} else {
resp.body = res.get_error().description();
}
resp.status_code = http_status_code::ok;
}
} // namespace dsn
15 changes: 15 additions & 0 deletions src/utils/flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ class flag_registry : public utils::singleton<flag_registry>
return it->second.has_tag(tag);
}

error_with<std::string> get_flag_str(const std::string &name) const
{
const auto iter = _flags.find(name);
if (iter == _flags.end()) {
return error_s::make(ERR_OBJECT_NOT_FOUND, fmt::format("{} is not found", name));
}

return iter->second.to_json();
}

std::string list_all_flags() const
{
utils::table_printer tp;
Expand Down Expand Up @@ -272,6 +282,11 @@ flag_tagger::flag_tagger(const char *name, const flag_tag &tag)
return flag_registry::instance().has_tag(name, tag);
}

/*extern*/ error_with<std::string> get_flag_str(const std::string &flag_name)
{
return flag_registry::instance().get_flag_str(flag_name);
}

/*extern*/ std::string list_all_flags() { return flag_registry::instance().list_all_flags(); }

} // namespace dsn