diff options
| author | Stefan Boberg <[email protected]> | 2023-05-02 13:23:42 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-02 13:23:42 +0200 |
| commit | fc53dd4bd6737f4e1c406f24cd66b4255f383e60 (patch) | |
| tree | 56bf06028ddae6ed2ff445a78db6a781538949f4 /src/zenserver/diag | |
| parent | move auth code from zenserver into zenhttp (#265) (diff) | |
| download | zen-fc53dd4bd6737f4e1c406f24cd66b4255f383e60.tar.xz zen-fc53dd4bd6737f4e1c406f24cd66b4255f383e60.zip | |
move testing and observability code to zenhttp (#266)
Diffstat (limited to 'src/zenserver/diag')
| -rw-r--r-- | src/zenserver/diag/diagsvcs.cpp | 127 | ||||
| -rw-r--r-- | src/zenserver/diag/diagsvcs.h | 111 | ||||
| -rw-r--r-- | src/zenserver/diag/formatters.h | 71 |
3 files changed, 0 insertions, 309 deletions
diff --git a/src/zenserver/diag/diagsvcs.cpp b/src/zenserver/diag/diagsvcs.cpp deleted file mode 100644 index 29ad5c3dd..000000000 --- a/src/zenserver/diag/diagsvcs.cpp +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#include "diagsvcs.h" - -#include <zencore/compactbinary.h> -#include <zencore/compactbinarybuilder.h> -#include <zencore/config.h> -#include <zencore/filesystem.h> -#include <zencore/logging.h> -#include <zencore/string.h> -#include <fstream> -#include <sstream> - -#include <json11.hpp> - -namespace zen { - -using namespace std::literals; - -bool -ReadFile(const std::string& Path, StringBuilderBase& Out) -{ - try - { - constexpr auto ReadSize = std::size_t{4096}; - auto FileStream = std::ifstream{Path}; - - std::string Buf(ReadSize, '\0'); - while (FileStream.read(&Buf[0], ReadSize)) - { - Out.Append(std::string_view(&Buf[0], FileStream.gcount())); - } - Out.Append(std::string_view(&Buf[0], FileStream.gcount())); - - return true; - } - catch (std::exception&) - { - Out.Reset(); - return false; - } -} - -HttpHealthService::HttpHealthService() -{ - m_Router.RegisterRoute( - "", - [](HttpRouterRequest& RoutedReq) { - HttpServerRequest& HttpReq = RoutedReq.ServerRequest(); - HttpReq.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, u8"OK!"sv); - }, - HttpVerb::kGet); - - m_Router.RegisterRoute( - "info", - [this](HttpRouterRequest& RoutedReq) { - HttpServerRequest& HttpReq = RoutedReq.ServerRequest(); - - CbObjectWriter Writer; - Writer << "DataRoot"sv << m_HealthInfo.DataRoot.string(); - Writer << "AbsLogPath"sv << m_HealthInfo.AbsLogPath.string(); - Writer << "BuildVersion"sv << m_HealthInfo.BuildVersion; - Writer << "HttpServerClass"sv << m_HealthInfo.HttpServerClass; - - HttpReq.WriteResponse(HttpResponseCode::OK, Writer.Save()); - }, - HttpVerb::kGet); - - m_Router.RegisterRoute( - "log", - [this](HttpRouterRequest& RoutedReq) { - HttpServerRequest& HttpReq = RoutedReq.ServerRequest(); - - zen::Log().flush(); - - std::filesystem::path Path = - m_HealthInfo.AbsLogPath.empty() ? m_HealthInfo.DataRoot / "logs/zenserver.log" : m_HealthInfo.AbsLogPath; - - ExtendableStringBuilder<4096> Sb; - if (ReadFile(Path.string(), Sb) && Sb.Size() > 0) - { - HttpReq.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, Sb.ToView()); - } - else - { - HttpReq.WriteResponse(HttpResponseCode::NotFound); - } - }, - HttpVerb::kGet); - m_Router.RegisterRoute( - "version", - [this](HttpRouterRequest& RoutedReq) { - HttpServerRequest& HttpReq = RoutedReq.ServerRequest(); - if (HttpReq.GetQueryParams().GetValue("detailed") == "true") - { - HttpReq.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, ZEN_CFG_VERSION_BUILD_STRING_FULL); - } - else - { - HttpReq.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, ZEN_CFG_VERSION); - } - }, - HttpVerb::kGet); -} - -void -HttpHealthService::SetHealthInfo(HealthServiceInfo&& Info) -{ - m_HealthInfo = std::move(Info); -} - -const char* -HttpHealthService::BaseUri() const -{ - return "/health/"; -} - -void -HttpHealthService::HandleRequest(HttpServerRequest& Request) -{ - if (!m_Router.HandleRequest(Request)) - { - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, u8"OK!"sv); - } -} - -} // namespace zen diff --git a/src/zenserver/diag/diagsvcs.h b/src/zenserver/diag/diagsvcs.h deleted file mode 100644 index bd03f8023..000000000 --- a/src/zenserver/diag/diagsvcs.h +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#pragma once - -#include <zencore/iobuffer.h> -#include <zenhttp/httpserver.h> - -#include <filesystem> - -////////////////////////////////////////////////////////////////////////// - -namespace zen { - -class HttpTestService : public HttpService -{ - uint32_t LogPoint = 0; - -public: - HttpTestService() {} - ~HttpTestService() = default; - - virtual const char* BaseUri() const override { return "/test/"; } - - virtual void HandleRequest(HttpServerRequest& Request) override - { - using namespace std::literals; - - auto Uri = Request.RelativeUri(); - - if (Uri == "hello"sv) - { - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, u8"hello world!"sv); - - // OutputLogMessageInternal(&LogPoint, 0, 0); - } - else if (Uri == "1K"sv) - { - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, m_1k); - } - else if (Uri == "1M"sv) - { - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, m_1m); - } - else if (Uri == "1M_1k"sv) - { - std::vector<IoBuffer> Buffers; - Buffers.reserve(1024); - - for (int i = 0; i < 1024; ++i) - { - Buffers.push_back(m_1k); - } - - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Buffers); - } - else if (Uri == "1G"sv) - { - std::vector<IoBuffer> Buffers; - Buffers.reserve(1024); - - for (int i = 0; i < 1024; ++i) - { - Buffers.push_back(m_1m); - } - - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Buffers); - } - else if (Uri == "1G_1k"sv) - { - std::vector<IoBuffer> Buffers; - Buffers.reserve(1024 * 1024); - - for (int i = 0; i < 1024 * 1024; ++i) - { - Buffers.push_back(m_1k); - } - - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Buffers); - } - } - -private: - IoBuffer m_1m{1024 * 1024}; - IoBuffer m_1k{m_1m, 0u, 1024}; -}; - -struct HealthServiceInfo -{ - std::filesystem::path DataRoot; - std::filesystem::path AbsLogPath; - std::string HttpServerClass; - std::string BuildVersion; -}; - -class HttpHealthService : public HttpService -{ -public: - HttpHealthService(); - ~HttpHealthService() = default; - - void SetHealthInfo(HealthServiceInfo&& Info); - - virtual const char* BaseUri() const override; - virtual void HandleRequest(HttpServerRequest& Request) override final; - -private: - HttpRequestRouter m_Router; - HealthServiceInfo m_HealthInfo; -}; - -} // namespace zen diff --git a/src/zenserver/diag/formatters.h b/src/zenserver/diag/formatters.h deleted file mode 100644 index 759df58d3..000000000 --- a/src/zenserver/diag/formatters.h +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#pragma once - -#include <zencore/compactbinary.h> -#include <zencore/compactbinaryvalidation.h> -#include <zencore/iobuffer.h> -#include <zencore/string.h> - -ZEN_THIRD_PARTY_INCLUDES_START -#include <cpr/cpr.h> -#include <fmt/format.h> -ZEN_THIRD_PARTY_INCLUDES_END - -template<> -struct fmt::formatter<cpr::Response> -{ - constexpr auto parse(format_parse_context& Ctx) -> decltype(Ctx.begin()) { return Ctx.end(); } - - template<typename FormatContext> - auto format(const cpr::Response& Response, FormatContext& Ctx) -> decltype(Ctx.out()) - { - using namespace std::literals; - - if (Response.status_code == 200 || Response.status_code == 201) - { - return fmt::format_to(Ctx.out(), - "Url: {}, Status: {}, Bytes: {}/{} (Up/Down), Elapsed: {}s", - Response.url.str(), - Response.status_code, - Response.uploaded_bytes, - Response.downloaded_bytes, - Response.elapsed); - } - else - { - const auto It = Response.header.find("Content-Type"); - const std::string_view ContentType = It != Response.header.end() ? It->second : "<None>"sv; - - if (ContentType == "application/x-ue-cb"sv) - { - zen::IoBuffer Body(zen::IoBuffer::Wrap, Response.text.data(), Response.text.size()); - zen::CbObjectView Obj(Body.Data()); - zen::ExtendableStringBuilder<256> Sb; - std::string_view Json = Obj.ToJson(Sb).ToView(); - - return fmt::format_to(Ctx.out(), - "Url: {}, Status: {}, Bytes: {}/{} (Up/Down), Elapsed: {}s, Response: '{}', Reason: '{}'", - Response.url.str(), - Response.status_code, - Response.uploaded_bytes, - Response.downloaded_bytes, - Response.elapsed, - Json, - Response.reason); - } - else - { - return fmt::format_to(Ctx.out(), - "Url: {}, Status: {}, Bytes: {}/{} (Up/Down), Elapsed: {}s, Reponse: '{}', Reason: '{}'", - Response.url.str(), - Response.status_code, - Response.uploaded_bytes, - Response.downloaded_bytes, - Response.elapsed, - Response.text, - Response.reason); - } - } - } -}; |