diff --git a/main.cpp b/main.cpp index 68c7635..6b7707d 100644 --- a/main.cpp +++ b/main.cpp @@ -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 потоках, включая текущий