add first route

This commit is contained in:
Антон 2024-03-20 16:55:57 +03:00
parent 16ca6ebf08
commit ac9c40058b
1 changed files with 18 additions and 19 deletions

View File

@ -26,32 +26,31 @@ struct content_type
{
content_type() = delete;
constexpr static std::string_view k_TextHTML = "text/html"sv;
constexpr static std::string_view k_JSON = "application/json"sv;
// При необходимости внутрь content_type можно добавить и другие типы контента
};
// Создаёт StringResponse с заданными параметрами
StringResponse MakeStringResponse(http::status status,
std::string_view body,
unsigned http_version,
bool keep_alive,
std::string_view content_type = content_type::k_TextHTML)
{
StringResponse response(status, http_version);
response.set(http::field::content_type, content_type);
response.body() = body;
response.content_length(body.size());
response.keep_alive(keep_alive);
return response;
}
StringResponse HandleRequest(StringRequest&& req)
{
auto route = req.target();
return MakeStringResponse(http::status::ok,
"Hello, World!"sv,
req.version(),
req.keep_alive());
switch (true)
{
default:
{
StringResponse res(http::status::bad_request, 1);
res.set(http::field::content_type, content_type::k_JSON);
string body = "{\n"
" \"code\": \"badRequest\",\n"
" \"message\": \"Bad request\"\n"
"} "s;
res.body() = body;
res.content_length(body.size());
res.keep_alive(true);
return res;
}
}
}
// Запускает функцию fn на n потоках, включая текущий