1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// 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/memory/llm.h>
#include <zencore/string.h>
#include <fstream>
#include <sstream>
ZEN_THIRD_PARTY_INCLUDES_START
#include <spdlog/logger.h>
ZEN_THIRD_PARTY_INCLUDES_END
namespace zen {
const FLLMTag&
GetHealthTag()
{
static FLLMTag CacheHttpTag("health");
return CacheHttpTag;
}
using namespace std::literals;
static bool
ReadLogFile(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 (const std::exception&)
{
Out.Reset();
return false;
}
}
HttpHealthService::HttpHealthService()
{
ZEN_MEMSCOPE(GetHealthTag());
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;
{
RwLock::SharedLockScope _(m_InfoLock);
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().SpdLogger->flush();
std::filesystem::path Path = [&] {
RwLock::SharedLockScope _(m_InfoLock);
return m_HealthInfo.AbsLogPath.empty() ? m_HealthInfo.DataRoot / "logs/zenserver.log" : m_HealthInfo.AbsLogPath;
}();
ExtendableStringBuilder<4096> Sb;
if (ReadLogFile(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)
{
ZEN_MEMSCOPE(GetHealthTag());
RwLock::ExclusiveLockScope _(m_InfoLock);
m_HealthInfo = std::move(Info);
}
const char*
HttpHealthService::BaseUri() const
{
return "/health/";
}
void
HttpHealthService::HandleRequest(HttpServerRequest& Request)
{
ZEN_MEMSCOPE(GetHealthTag());
if (!m_Router.HandleRequest(Request))
{
Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, u8"OK!"sv);
}
}
} // namespace zen
|