Skip to content

Commit

Permalink
refactor: router
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Apr 9, 2024
1 parent 8d6b96c commit 6cfdc95
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
13 changes: 9 additions & 4 deletions include/router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ namespace http = boost::beast::http;

class Router {
private:
using HandlerFunc = std::function<void(const http::request<http::string_body>&, http::response<http::string_body>&)>;
std::map<std::string, HandlerFunc> routes;
using HandlerFunc =
std::function<void(const http::request<http::string_body> &,
http::response<http::string_body> &)>;
// Use a pair of method and path as the key
std::map<std::pair<http::verb, std::string>, HandlerFunc> routes;

public:
void addRoute(const std::string& path, HandlerFunc handler);
bool route(const http::request<http::string_body>& req, http::response<http::string_body>& res);
void addRoute(http::verb method, const std::string &path,
HandlerFunc handler);
bool route(const http::request<http::string_body> &req,
http::response<http::string_body> &res);
};
21 changes: 8 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ int main(void) {
auto personService = std::make_shared<PersonService>();
auto personController = std::make_shared<PersonController>(personService);

router->addRoute("/person", [personController](
const http::request<http::string_body> &req,
http::response<http::string_body> &res) {
if (req.method() == http::verb::get) {
personController->getPersons(req, res);
} else if (req.method() == http::verb::post) {
personController->createPerson(req, res);
} else {
res.result(http::status::method_not_allowed);
res.body() = "Method Not Allowed";
res.prepare_payload();
}
});
router->addRoute(http::verb::get, "/person",
[personController](const auto &req, auto &res) {
personController->getPersons(req, res);
});
router->addRoute(http::verb::post, "/person",
[personController](const auto &req, auto &res) {
personController->createPerson(req, res);
});

std::cout << "Server starting on port " << server.getPort() << std::endl;
server.run();
Expand Down
15 changes: 11 additions & 4 deletions src/router.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#include "../include/router.hpp"

void Router::addRoute(const std::string &path, HandlerFunc handler) {
routes[path] = std::move(handler);
void Router::addRoute(http::verb method, const std::string &path,
HandlerFunc handler) {
routes[{method, path}] = std::move(handler);
}

bool Router::route(const http::request<http::string_body> &req,
http::response<http::string_body> &res) {
const auto &path = req.target();
auto it = routes.find(path);
auto it = routes.find({req.method(), req.target()});
if (it != routes.end()) {
it->second(req, res);
return true;
}

// If no specific method route is found, check for a method-agnostic handler
it = routes.find({http::verb::unknown, req.target()});
if (it != routes.end()) {
it->second(req, res);
return true;
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

curl -X POST -H "Content-Type: application/json" -d '{"name": "John Doe", "age": 30}' http://localhost:6969/person -v

curl -X PUT -H "Content-Type: application/json" -d '{"name": "John Doe", "age": 30}' http://localhost:6969/person -v

curl http://localhost:6969/person -v

curl http://localhost:6969/42 -v

0 comments on commit 6cfdc95

Please sign in to comment.