-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d108359
commit d6e4d5b
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "CollectorConfigInspector.h" | ||
|
||
#include <Logging.h> | ||
#include <string> | ||
|
||
#include <google/protobuf/util/json_util.h> | ||
|
||
namespace collector { | ||
|
||
const std::string CollectorConfigInspector::kBaseRoute = "/state/config"; | ||
|
||
CollectorConfigInspector::CollectorConfigInspector(const std::shared_ptr<CollectorConfig> config) : config_(config) { | ||
} | ||
|
||
std::string CollectorConfigInspector::configToJson(bool& isError) { | ||
const auto& runtime_config = config_->GetRuntimeConfig(); | ||
|
||
if (!runtime_config.has_value()) { | ||
return "{}"; | ||
} | ||
|
||
std::string jsonString; | ||
const auto& config = runtime_config.value(); | ||
google::protobuf::util::Status status = google::protobuf::util::MessageToJsonString(config, &jsonString); | ||
|
||
if (!status.ok()) { | ||
isError = true; | ||
CLOG(WARNING) << "Failed to convert protobuf object to JSON: " << status.ToString(); | ||
return R"({"error": "Failed to convert protobuf object to JSON")"; | ||
} | ||
|
||
return jsonString; | ||
} | ||
|
||
bool CollectorConfigInspector::handleGet(CivetServer* server, struct mg_connection* conn) { | ||
const mg_request_info* req_info = mg_get_request_info(conn); | ||
|
||
if (req_info == nullptr) { | ||
return ServerError(conn, "unable to read request"); | ||
} | ||
|
||
bool isError = false; | ||
std::string jsonString = configToJson(isError); | ||
|
||
if (isError) { | ||
mg_printf(conn, "HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); | ||
} else { | ||
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); | ||
} | ||
mg_printf(conn, "%s\r\n", jsonString.c_str()); | ||
|
||
return true; | ||
} | ||
|
||
} // namespace collector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef COLLECTOR_CONFIG_INSPECTOR_H | ||
#define COLLECTOR_CONFIG_INSPECTOR_H | ||
|
||
#include <memory> | ||
|
||
#include <json/writer.h> | ||
|
||
#include "CollectorConfig.h" | ||
#include "IntrospectionEndpoint.h" | ||
|
||
namespace collector { | ||
|
||
class CollectorConfigInspector : public IntrospectionEndpoint { | ||
public: | ||
static const std::string kBaseRoute; | ||
|
||
CollectorConfigInspector(const std::shared_ptr<CollectorConfig> config); | ||
|
||
// implementation of CivetHandler | ||
bool handleGet(CivetServer* server, struct mg_connection* conn) override; | ||
|
||
private: | ||
const std::shared_ptr<CollectorConfig> config_; | ||
std::string configToJson(bool& isError); | ||
}; | ||
|
||
} // namespace collector | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters