-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
100 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "../include/person_controller.hpp" | ||
|
||
void PersonController::getPersons( | ||
const http::request<http::string_body> &request, | ||
http::response<http::string_body> &response) { | ||
try { | ||
auto persons = personService->getAllPersons(); | ||
boost::json::array jsonArray; | ||
for (const auto &person : persons) { | ||
jsonArray.push_back(PersonSerializer::toJson(person)); | ||
} | ||
std::string jsonString = boost::json::serialize(jsonArray); | ||
response.result(http::status::ok); | ||
response.body() = jsonString; | ||
response.set(http::field::content_type, "application/json"); | ||
} catch (const std::exception &e) { | ||
response.result(http::status::internal_server_error); | ||
response.body() = "{\"error\": \"Failed to serialize persons.\"}"; | ||
response.set(http::field::content_type, "application/json"); | ||
} | ||
} | ||
|
||
void PersonController::createPerson( | ||
const http::request<http::string_body> &request, | ||
http::response<http::string_body> &response) { | ||
try { | ||
auto json = boost::json::parse(request.body()); | ||
auto person = PersonSerializer::fromJson(json.as_object()); | ||
personService->addPerson(person); | ||
response.result(http::status::created); | ||
response.body() = "Person created"; | ||
} catch (const std::exception &e) { | ||
response.result(http::status::bad_request); | ||
response.body() = "Invalid JSON payload"; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,44 @@ | ||
#include "../include/server.hpp" | ||
|
||
void Server::session(tcp::socket socket, | ||
std::shared_ptr<IRequestHandler> handler) { | ||
void Server::session(tcp::socket socket) { | ||
try { | ||
beast::flat_buffer buffer; | ||
http::request<http::string_body> request; | ||
http::read(socket, buffer, request); | ||
http::response<http::string_body> response; | ||
handler->handleRequest(request, response); | ||
|
||
auto handlerIt = routeMap.find(request.target()); | ||
if (handlerIt != routeMap.end()) { | ||
// Call the function handler directly | ||
handlerIt->second(request, response); | ||
} else { | ||
response.result(http::status::not_found); | ||
response.body() = "Route not found"; | ||
} | ||
|
||
http::write(socket, response); | ||
} catch (std::exception const &e) { | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
std::cerr << "Session error: " << e.what() << std::endl; | ||
} | ||
} | ||
|
||
void Server::run(std::shared_ptr<IRequestHandler> handler) { | ||
void Server::run() { | ||
net::io_context io_context{1}; | ||
tcp::acceptor acceptor{ | ||
io_context, {tcp::v4(), static_cast<boost::asio::ip::port_type>(port)}}; | ||
for (;;) { | ||
tcp::socket socket{io_context}; | ||
acceptor.accept(socket); | ||
std::thread(&Server::session, this, std::move(socket), handler).detach(); | ||
std::thread(&Server::session, this, std::move(socket)).detach(); | ||
} | ||
} | ||
|
||
void Server::addRoute( | ||
const std::string &route, | ||
std::function<void(const http::request<http::string_body> &, | ||
http::response<http::string_body> &)> | ||
handler) { | ||
routeMap[route] = handler; | ||
} | ||
|
||
short Server::getPort() { return port; } |