add first route
This commit is contained in:
parent
16ca6ebf08
commit
ac9c40058b
37
main.cpp
37
main.cpp
@ -26,32 +26,31 @@ struct content_type
|
|||||||
{
|
{
|
||||||
content_type() = delete;
|
content_type() = delete;
|
||||||
constexpr static std::string_view k_TextHTML = "text/html"sv;
|
constexpr static std::string_view k_TextHTML = "text/html"sv;
|
||||||
|
constexpr static std::string_view k_JSON = "application/json"sv;
|
||||||
// При необходимости внутрь content_type можно добавить и другие типы контента
|
// При необходимости внутрь 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)
|
StringResponse HandleRequest(StringRequest&& req)
|
||||||
{
|
{
|
||||||
auto route = req.target();
|
auto route = req.target();
|
||||||
|
|
||||||
return MakeStringResponse(http::status::ok,
|
switch (true)
|
||||||
"Hello, World!"sv,
|
{
|
||||||
req.version(),
|
default:
|
||||||
req.keep_alive());
|
{
|
||||||
|
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 потоках, включая текущий
|
// Запускает функцию fn на n потоках, включая текущий
|
||||||
|
Loading…
x
Reference in New Issue
Block a user