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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "zenhttp/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;
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 (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;
{
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().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)
{
RwLock::ExclusiveLockScope _(m_InfoLock);
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
|