-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
122 lines (93 loc) · 3.41 KB
/
main.cpp
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
#include "json_response.h"
#include "logging.h"
#include "options.h"
#include "endpoints/endpoint_common.h"
#include <httpserver.hpp>
#include <csignal>
#include <memory>
using namespace httpserver;
static void log_request(const http_request &request)
{
std::ostringstream log_builder;
log_builder << "-----------------------------------------\n";
auto time = std::time(nullptr);
auto localtime = *std::localtime(&time);
log_builder << "Time: " << CYAN << std::put_time(&localtime, "%d-%m-%Y %H:%M:%S") << RESET << "\n";
log_builder << "Received " << RED << request.get_method() << " " << RESET << request.get_path() << " ("
<< request.get_requestor() << ")\n";
std::ostringstream headers_str_builder;
for (const auto &pair : request.get_headers())
headers_str_builder << YELLOW << pair.first << ": " << GREEN << pair.second << RESET << " | ";
auto headers_str = headers_str_builder.str();
if (!headers_str.empty())
{
headers_str = headers_str.substr(0, headers_str.find_last_not_of(" | "));
log_builder << "Headers: " << headers_str << RESET << "\n";
}
std::ostringstream args_str_builder;
for (const auto &pair : request.get_args_flat())
headers_str_builder << MAGENTA << pair.first << ": " << GREEN << pair.second << RESET << " | ";
auto args_str = args_str_builder.str();
if (!args_str.empty())
{
args_str = args_str.substr(0, args_str.find_last_not_of(" | "));
log_builder << "Args: " << args_str << RESET << "\n";
}
if (!request.get_querystring().empty())
{
log_builder << "Query: " << BLUE << request.get_querystring() << RESET << "\n";
}
if (!request.get_content().empty())
{
log_builder << "Content: " << YELLOW << request.get_content() << RESET << "\n";
}
LOG(log_builder.str());
}
class chaosworkshop : public http_resource
{
public:
chaosworkshop()
{
}
std::shared_ptr<http_response> render_GET(const http_request &request) override
{
log_request(request);
const auto &endpoint = request.get_path();
if (endpoint::get_endpoint_handlers.contains(endpoint))
return endpoint::get_endpoint_handlers.at(endpoint)(request);
return make_response<http_response>(404, http::http_utils::text_plain);
}
std::shared_ptr<http_response> render_POST(const http_request &request) override
{
log_request(request);
const auto &endpoint = request.get_path();
if (endpoint::post_endpoint_handlers.contains(endpoint))
return endpoint::post_endpoint_handlers.at(endpoint)(request);
return make_response<http_response>(404, http::http_utils::text_plain);
}
};
int main()
{
// reap children automatically
signal(SIGCHLD, SIG_IGN);
global_options.read();
auto server_builder = create_webserver(global_options.port).connection_timeout(global_options.connection_timeout);
if (global_options.use_tls)
{
if (!file::does_file_exist("cert.pem") || !file::does_file_exist("key.pem"))
{
LOG(RED
<< "No cert.pem or key.pem files found in DATA_ROOT path, aborting (set use_tls to false to disable)\n"
<< WHITE);
exit(EXIT_FAILURE);
}
server_builder = server_builder.use_ssl()
.https_mem_cert(file::get_data_root() + "cert.pem")
.https_mem_key(file::get_data_root() + "key.pem");
}
LOG("\n\n" << GREEN << "Starting http server on port " << global_options.port << "\n" << WHITE);
webserver server = server_builder;
chaosworkshop workshop;
server.register_resource("/", &workshop, true);
server.start(true);
}