aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/httphubservice.cpp
blob: 67ed0cfd8976beade3faeb7004ad979cc262b0e8 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright Epic Games, Inc. All Rights Reserved.

#include "httphubservice.h"

#include "hub.h"
#include "storageserverinstance.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>

namespace zen {

HttpHubService::HttpHubService(Hub& Hub) : m_Hub(Hub)
{
	using namespace std::literals;

	m_Router.AddMatcher("moduleid", [](std::string_view Str) -> bool {
		for (const auto C : Str)
		{
			if (std::isalnum(C) || C == '-')
			{
				// fine
			}
			else
			{
				// not fine
				return false;
			}
		}

		return true;
	});

	m_Router.RegisterRoute(
		"status",
		[this](HttpRouterRequest& Req) {
			CbObjectWriter Obj;
			Obj.BeginArray("modules");
			m_Hub.EnumerateModules([&Obj](StorageServerInstance& Instance) {
				Obj.BeginObject();
				Obj << "moduleId" << Instance.GetModuleId();
				Obj << "provisioned" << Instance.IsProvisioned();
				Obj.EndObject();
			});
			Obj.EndArray();
			Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());
		},
		HttpVerb::kGet);

	m_Router.RegisterRoute(
		"modules/{moduleid}",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId = Req.GetCapture(1);

			if (Req.ServerRequest().RequestVerb() == HttpVerb::kDelete)
			{
				HandleModuleDelete(Req.ServerRequest(), ModuleId);
			}
			else
			{
				HandleModuleGet(Req.ServerRequest(), ModuleId);
			}
		},
		HttpVerb::kGet | HttpVerb::kDelete);

	m_Router.RegisterRoute(
		"modules/{moduleid}/provision",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId = Req.GetCapture(1);

			std::string		 FailureReason = "unknown";
			HttpResponseCode ResponseCode  = HttpResponseCode::OK;

			try
			{
				HubProvisionedInstanceInfo Info;
				if (m_Hub.Provision(ModuleId, /* out */ Info, /* out */ FailureReason))
				{
					CbObjectWriter Obj;
					Obj << "moduleId" << ModuleId;
					Obj << "baseUri" << Info.BaseUri;
					Obj << "port" << Info.Port;
					Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());

					return;
				}
				else
				{
					ResponseCode = HttpResponseCode::BadRequest;
				}
			}
			catch (const std::exception& Ex)
			{
				ZEN_ERROR("Exception while provisioning module '{}': {}", ModuleId, Ex.what());

				FailureReason = Ex.what();
				ResponseCode  = HttpResponseCode::InternalServerError;
			}

			Req.ServerRequest().WriteResponse(ResponseCode, HttpContentType::kText, FailureReason);
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"modules/{moduleid}/deprovision",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId	   = Req.GetCapture(1);
			std::string		 FailureReason = "unknown";

			try
			{
				if (!m_Hub.Deprovision(std::string(ModuleId), /* out */ FailureReason))
				{
					if (FailureReason.empty())
					{
						return Req.ServerRequest().WriteResponse(HttpResponseCode::NotFound);
					}
					else
					{
						return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, FailureReason);
					}
				}

				CbObjectWriter Obj;
				Obj << "moduleId" << ModuleId;

				return Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());
			}
			catch (const std::exception& Ex)
			{
				ZEN_ERROR("Exception while deprovisioning module '{}': {}", ModuleId, Ex.what());

				FailureReason = Ex.what();
			}

			Req.ServerRequest().WriteResponse(HttpResponseCode::InternalServerError, HttpContentType::kText, FailureReason);
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"stats",
		[this](HttpRouterRequest& Req) {
			CbObjectWriter Obj;
			Obj << "currentInstanceCount" << m_Hub.GetInstanceCount();
			Obj << "maxInstanceCount" << m_Hub.GetMaxInstanceCount();
			Obj << "instanceLimit" << m_Hub.GetConfig().InstanceLimit;
			Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());
		},
		HttpVerb::kGet);
}

HttpHubService::~HttpHubService()
{
}

const char*
HttpHubService::BaseUri() const
{
	return "/hub/";
}

void
HttpHubService::SetNotificationEndpoint(std::string_view UpstreamNotificationEndpoint, std::string_view InstanceId)
{
	ZEN_UNUSED(UpstreamNotificationEndpoint, InstanceId);
	// TODO: store these for use in notifications, on some interval/criteria which is currently TBD
}

void
HttpHubService::HandleRequest(zen::HttpServerRequest& Request)
{
	m_Router.HandleRequest(Request);
}

void
HttpHubService::HandleModuleGet(HttpServerRequest& Request, std::string_view ModuleId)
{
	StorageServerInstance* Instance = nullptr;
	if (!m_Hub.Find(ModuleId, &Instance))
	{
		Request.WriteResponse(HttpResponseCode::NotFound);
		return;
	}

	// TODO: A separate http request for the modules/{moduleid}/deprovision endpoint can be called and deprovision the instance leaving us
	// with a dangling pointer...

	CbObjectWriter Obj;
	Obj << "moduleId" << Instance->GetModuleId();
	Obj << "provisioned" << Instance->IsProvisioned();
	Request.WriteResponse(HttpResponseCode::OK, Obj.Save());
}

void
HttpHubService::HandleModuleDelete(HttpServerRequest& Request, std::string_view ModuleId)
{
	StorageServerInstance* Instance = nullptr;
	if (!m_Hub.Find(ModuleId, &Instance))
	{
		Request.WriteResponse(HttpResponseCode::NotFound);
		return;
	}

	// TODO: deprovision and nuke all related storage

	CbObjectWriter Obj;
	Obj << "moduleId" << Instance->GetModuleId();
	Obj << "provisioned" << Instance->IsProvisioned();
	Request.WriteResponse(HttpResponseCode::OK, Obj.Save());
}

}  // namespace zen