Older/Server/ServiceLogic.cpp
2023-07-21 16:17:01 +08:00

35 lines
1.3 KiB
C++

#include "ServiceLogic.h"
#include <sstream>
namespace ServiceLogic {
boost::beast::http::response<boost::beast::http::string_body>
notFound(const boost::beast::http::request<boost::beast::http::string_body> &request) {
using namespace boost::beast;
http::response<http::string_body> res{http::status::not_found, request.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(request.keep_alive());
std::ostringstream oss;
oss << "The resource '" << request.target() << "' was not found.";
res.body() = oss.str();
res.prepare_payload();
return res;
}
boost::beast::http::response<boost::beast::http::string_body>
serverError(const boost::beast::http::request<boost::beast::http::string_body> &request,
std::string_view errorMessage) {
using namespace boost::beast;
http::response<http::string_body> res{http::status::internal_server_error, request.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(request.keep_alive());
std::ostringstream oss;
oss << "An error occurred: '" << errorMessage << "'";
res.body() = oss.str();
res.prepare_payload();
return res;
}
} // namespace ServiceLogic