Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (132 loc) · 4.66 KB
/
Copy pathmain.cpp
File metadata and controls
164 lines (132 loc) · 4.66 KB
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
// main.cpp
#include <boost/beast.hpp>
#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <iostream>
#include <unordered_map>
#include <sstream>
#include <thread>
#include <csignal>
#include <atomic>
#include <vector>
#include <mutex>
#include "Web/views.h"
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
namespace json = boost::json;
using tcp = boost::asio::ip::tcp;
std::atomic g_should_exit = false;
std::unique_ptr<tcp::acceptor> global_acceptor;
std::vector<std::thread> session_threads;
std::mutex session_mutex;
void handle_signal(const int signal) {
if (signal == SIGTERM || signal == SIGINT) {
if (global_acceptor) {
boost::system::error_code ec;
auto err = global_acceptor->cancel(ec);
(void) err;
}
}
}
void cleanup() {
// Used to clean up existing connections
}
std::unordered_map<std::string, views::HandlerFunc> build_route_map() {
std::unordered_map<std::string, views::HandlerFunc> map;
for (const auto &[name, func]: views::function_map) {
map["/" + name] = func;
}
return map;
}
void handle_request(
const std::unordered_map<std::string, views::HandlerFunc> &route_map,
const http::request<http::string_body> &req,
http::response<http::string_body> &res) {
res.version(req.version());
res.keep_alive(req.keep_alive());
const std::string target = std::string(req.target());
auto query_pos = target.find('?');
std::string route = query_pos == std::string::npos ? target : target.substr(0, query_pos);
if (auto it = route_map.find(route); it != route_map.end()) {
http_util::Response hres;
try {
it->second(req, hres);
} catch (const std::exception& e) {
http_util::set_json(hres, {{"error", e.what()}}, 400);
}
res = std::move(hres);
} else {
http_util::set_text(res, "404 Not Found: " + route, 404);
}
}
void do_session(tcp::socket socket,
const std::shared_ptr<const std::unordered_map<std::string, views::HandlerFunc>> &route_map) {
try {
beast::flat_buffer buffer;
http::request<http::string_body> req;
http::read(socket, buffer, req);
http::response<http::string_body> res;
handle_request(*route_map, req, res);
http::write(socket, res);
} catch (const beast::system_error &e) {
if (!g_should_exit && e.code() != http::error::partial_message) {
std::cerr << "Session error: " << e.what() << std::endl;
}
// Force shutdown silently
} catch (const std::exception &e) {
if (!g_should_exit) {
std::cerr << "Session exception: " << e.what() << std::endl;
}
}
}
int main() {
std::signal(SIGINT, handle_signal);
std::signal(SIGTERM, handle_signal);
std::atexit(cleanup);
auto route_map = std::make_shared<std::unordered_map<std::string, views::HandlerFunc>>(build_route_map());
std::cout << "Registered routes:" << std::endl;
for (const auto &[name, _]: *route_map) {
std::cout << name << std::endl;
}
try {
net::io_context ioc{1};
global_acceptor = std::make_unique<tcp::acceptor>(ioc, tcp::endpoint{tcp::v4(), 8080});
auto io_runner = [&ioc]() {
ioc.run();
};
std::thread(std::move(io_runner)).detach();
std::cout << "HTTP server running on port 8080..." << std::endl;
while (!g_should_exit) {
tcp::socket socket{ioc};
boost::system::error_code ec;
auto err = global_acceptor->accept(socket, ec);
(void) err;
if (ec == boost::asio::error::operation_aborted) break;
if (ec) {
std::cerr << "Accept error: " << ec.message() << std::endl;
continue;
}
auto session = [sock = std::move(socket), route_map]() mutable {
do_session(std::move(sock), route_map);
};
std::lock_guard<std::mutex> lock(session_mutex);
session_threads.emplace_back(std::move(session));
}
ioc.stop();
global_acceptor.reset();
{
std::lock_guard lock(session_mutex);
for (size_t i = 0; i < session_threads.size(); ++i) { // NOLINT ranged-for illegal
if (session_threads.at(i).joinable()) {
session_threads.at(i).join();
}
}
}
std::cout << "\U0001F44B Server exiting, cleaning up...\n";
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}