aboutsummaryrefslogtreecommitdiff
path: root/zenserver/experimental/frontend.cpp
blob: 79fcf0a1738e4c0cb52062b7cf237d4073dcd939 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "frontend.h"

#include <zencore/filesystem.h>
#include <zencore/string.h>

namespace zen {

namespace html {

	constexpr std::string_view Index = R"(
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-skAcpIdS7UcVUC05LJ9Dxay8AXcDYfBJqt1CJ85S/CFujBsIzCIv+l9liuYLaMQ/" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<style type="text/css">
body {
	background-color: #fafafa;
}
</style>
<script type="text/javascript">
	const getCacheStats = () => {
		const opts = { headers: { "Accept": "application/json" } };
		fetch("/z$", opts)
			.then(response => {
				if (!response.ok) {
					throw Error(response.statusText);
				}
				return response.json();
			})
			.then(json => {
				document.getElementById("status").innerHTML = "connected"
				document.getElementById("stats").innerHTML = JSON.stringify(json, null, 4);
			})
			.catch(error => {
				document.getElementById("status").innerHTML = "disconnected"
				document.getElementById("stats").innerHTML = ""
				console.log(error);
			})
			.finally(() => {
				window.setTimeout(getCacheStats, 1000);
			});
	};
	getCacheStats();
</script>
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="text-center mt-5">
				<pre>
__________                  _________  __                           
\____    /  ____    ____   /   _____/_/  |_   ____  _______   ____  
  /     / _/ __ \  /    \  \_____  \ \   __\ /  _ \ \_  __ \_/ __ \ 
 /     /_ \  ___/ |   |  \ /        \ |  |  (  <_> ) |  | \/\  ___/ 
/_______ \ \___  >|___|  //_______  / |__|   \____/  |__|    \___  >
        \/     \/      \/         \/                             \/ 
				</pre>
				<pre id="status"/>
			</div>
		</div>
		<div class="row">
			<pre class="mb-0">Z$:</pre>
			<pre id="stats"></pre>
		<div>
	</div>
</body>
</html>
)";

}  // namespace html

HttpFrontendService::HttpFrontendService(std::filesystem::path Directory) : m_Directory(Directory)
{
}

HttpFrontendService::~HttpFrontendService()
{
}

const char*
HttpFrontendService::BaseUri() const
{
	return "/dashboard";  // in order to use the root path we need to remove HttpAddUrlToUrlGroup in HttpSys.cpp
}

void
HttpFrontendService::HandleRequest(zen::HttpServerRequest& Request)
{
	using namespace std::literals;

	if (m_Directory.empty())
	{
		Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kHTML, html::Index);
	}
	else
	{
		std::string_view	  Uri = Request.RelativeUri();
		std::filesystem::path RelPath{Uri.empty() ? "index.html" : Uri};
		std::filesystem::path AbsPath = m_Directory / RelPath;

		FileContents File = ReadFile(AbsPath);

		if (!File.ErrorCode)
		{
			// TODO: Map file extension to MIME type
			Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kHTML, File.Data[0]);
		}
		else
		{
			return Request.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, "Ooops!"sv);
		}
	}
}

}  // namespace zen