Refactoring

This commit is contained in:
Антон 2024-01-20 12:24:00 +03:00
parent 3d1ded8573
commit 42382261a7
1 changed files with 8 additions and 11 deletions

View File

@ -22,12 +22,11 @@ using StringRequest = http::request<http::string_body>;
// Ответ, тело которого представлено в виде строки // Ответ, тело которого представлено в виде строки
using StringResponse = http::response<http::string_body>; using StringResponse = http::response<http::string_body>;
struct ContentType struct content_type
{ {
ContentType() = delete; content_type() = delete;
constexpr static std::string_view constexpr static std::string_view k_TextHTML = "text/html"sv;
TEXT_HTML = "text/html"sv; // При необходимости внутрь content_type можно добавить и другие типы контента
// При необходимости внутрь ContentType можно добавить и другие типы контента
}; };
// Создаёт StringResponse с заданными параметрами // Создаёт StringResponse с заданными параметрами
@ -35,7 +34,7 @@ StringResponse MakeStringResponse(http::status status,
std::string_view body, std::string_view body,
unsigned http_version, unsigned http_version,
bool keep_alive, bool keep_alive,
std::string_view content_type = ContentType::TEXT_HTML) std::string_view content_type = content_type::k_TextHTML)
{ {
StringResponse response(status, http_version); StringResponse response(status, http_version);
response.set(http::field::content_type, content_type); response.set(http::field::content_type, content_type);
@ -49,7 +48,7 @@ StringResponse HandleRequest(StringRequest&& req)
{ {
// Подставьте сюда код из синхронной версии HTTP-сервера // Подставьте сюда код из синхронной версии HTTP-сервера
return MakeStringResponse(http::status::ok, return MakeStringResponse(http::status::ok,
"OK"sv, "Hello, World!"sv,
req.version(), req.version(),
req.keep_alive()); req.keep_alive());
} }
@ -91,9 +90,8 @@ int main()
} }
}); });
const auto address = net::ip::make_address("0.0.0.0"); const auto address = net::ip::make_address("0.0.0.0");
constexpr constexpr net::ip::port_type port = 8080;
net::ip::port_type port = 8080;
http_server::ServeHttp(ioc, {address, port}, [](auto&& req, auto&& sender) http_server::ServeHttp(ioc, {address, port}, [](auto&& req, auto&& sender)
{ {
sender(HandleRequest(std::forward<decltype(req)>(req))); sender(HandleRequest(std::forward<decltype(req)>(req)));
@ -106,7 +104,6 @@ int main()
cout << "Timer expired"sv << endl; cout << "Timer expired"sv << endl;
}); });
// Эта надпись сообщает тестам о том, что сервер запущен и готов обрабатывать запросы
std::cout << "Server has started..."sv << std::endl; std::cout << "Server has started..."sv << std::endl;
RunWorkers(num_threads, [&ioc] RunWorkers(num_threads, [&ioc]