Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRUD allow custom matchers #107

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions CRUD/handlers/crud_dispatcher.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* File: crud_dispathcher.hpp
* File: crud_dispatcher.hpp
* Author: vvenedict@gmail.com
*
* dispatcher<response,request> d;
Expand Down Expand Up @@ -40,8 +40,8 @@
* Created on September 29, 2015, 5:59 PM
*/

#ifndef __HTTP_CRUD_DISPATHCHER_HPP__
#define __HTTP_CRUD_DISPATHCHER_HPP__
#ifndef HTTP_CRUD_DISPATCHER_HPP
#define HTTP_CRUD_DISPATCHER_HPP

#include "crud_matcher.hpp"
#include <boost/regex.hpp>
Expand All @@ -53,18 +53,19 @@
#include <boost/container/flat_map.hpp>
#endif

namespace http { namespace crud {
template<typename Request, typename Response, typename Match=boost::cmatch, typename Expression=boost::regex>
class crud_dispatcher {
typedef crud_matcher<Response, Expression, Match> crud_matcher_type ;
typedef std::shared_ptr<crud_matcher_type> crud_matcher_type_p ;
namespace http::crud {
template <typename Request, typename Response, typename Match = boost::cmatch, typename Expression = boost::regex,
typename CrudMatcher = crud_matcher<Response, Expression, Match>>
class crud_dispatcher {
using crud_matcher_type = CrudMatcher;
using crud_matcher_type_p = std::shared_ptr<crud_matcher_type>;
public :
crud_dispatcher() : _base_path() {}
crud_dispatcher(const std::string &base_path) : _base_path(base_path) {}
explicit crud_dispatcher(const std::string &base_path) : _base_path(base_path) {}
crud_matcher_type & crud_match(const Expression &expression) {
crud_matcher_type_p & p = _crud_matchers[expression] ;
if(!p) {
p = std::make_shared<crud_matcher_type>(expression) ;
p = std::make_shared<crud_matcher_type>() ;
}
return *p;
}
Expand Down Expand Up @@ -98,8 +99,7 @@ namespace http { namespace crud {
#endif
};

}}


#endif /* __HTTP_CRUD_DISPATCHER__ */
}

#endif /* HTTP_CRUD_DISPATCHER_HPP */

63 changes: 17 additions & 46 deletions CRUD/handlers/crud_matcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,28 @@
*
* Created on September 29, 2015, 6:06 PM
*/
#ifndef _HTTP_CRUD_MATCHER_HPP__
#define _HTTP_CRUD_MATCHER_HPP__
#pragma once
#ifndef HTTP_CRUD_MATCHER_HPP
#define HTTP_CRUD_MATCHER_HPP

#include <functional>
#include <map>
#include <string>

namespace http { namespace crud {

#include "crud_matcher_base.hpp"


namespace http::crud {

template<typename Matched>
struct crud_match : Matched {
crud_match(const Matched &m, const std::string &d) : Matched(m) , data(d) {}
template<typename Request>
crud_match(const Matched &m, const Request &request) : Matched(m) , data(request.data) {}
std::string data;
};

template<typename Response, typename Regex, typename Matched>
struct crud_matcher {
typedef std::function<void(Response &, const crud_match<Matched> &)> request_handler_type;
typedef crud_matcher<Response, Regex, Matched> self_type ;
explicit crud_matcher(const Regex &expression) : _expression(expression) {}
self_type & get(request_handler_type handler) {
_handlers["GET"] = handler;
return *this ;
}
self_type & post(request_handler_type handler) {
_handlers["POST"] = handler;
return *this;
}
self_type & del(request_handler_type handler) {
_handlers["DELETE"] = handler;
return *this;
}
self_type & put(request_handler_type handler) {
_handlers["PUT"] = handler;
return *this;
}
template<typename Request>
void handle_request(const Request& request, Response& response, const Matched &what) {
//dispatching to matching based on CRUD handler
crud_match<Matched> match(what, request.data) ;
auto &handler = _handlers[request.method];
if (handler) {
handler(response, match);
}
}
private:
Regex _expression;
std::map<std::string, request_handler_type> _handlers;
};

}}

#endif /* __HTTP_CRUD_MATCHER__ */
struct crud_matcher : crud_matcher_base<Response, crud_match<Matched>> {};

}

#endif /* HTTP_CRUD_MATCHER_HPP */
51 changes: 51 additions & 0 deletions CRUD/handlers/crud_matcher_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* File: crud_matcher_base.hpp
* Author: vvenedict@gmail.com
*
* Created on September 29, 2015, 6:06 PM
*/
#pragma once
#ifndef HTTP_CRUD_MATCHER_BASE_HPP
#define HTTP_CRUD_MATCHER_BASE_HPP

#include <functional>
#include <map>
#include <string>

namespace http::crud {

template<typename Response, typename Matcher>
struct crud_matcher_base {
using request_handler_type = std::function<void(Response&, Matcher const&)>;
explicit crud_matcher_base() = default;
crud_matcher_base & get(request_handler_type handler) {
_handlers["GET"] = handler;
return *this;
}
crud_matcher_base & post(request_handler_type handler) {
_handlers["POST"] = handler;
return *this;
}
crud_matcher_base & del(request_handler_type handler) {
_handlers["DELETE"] = handler;
return *this;
}
crud_matcher_base & put(request_handler_type handler) {
_handlers["PUT"] = handler;
return *this;
}
template<typename Request, typename Matched>
void handle_request(const Request& request, Response& response, Matched const& what) {
auto &handler = _handlers[request.method];
Matcher matcher(what, request);
if (handler) {
handler(response, matcher);
}
}
private:
std::map<std::string, request_handler_type> _handlers;
};

}

#endif /* HTTP_CRUD_MATCHER_BASE_HPP */
30 changes: 30 additions & 0 deletions CRUD/handlers/crud_matcher_with_request.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* File: crud_matcher_with_request.hpp
* Author: vvenedict@gmail.com
*
* Created on September 29, 2015, 6:06 PM
*/
#pragma once
#ifndef HTTP_CRUD_MATCHER_WITH_REQUEST_HPP
#define HTTP_CRUD_MATCHER_WITH_REQUEST_HPP

#include <functional>
#include <map>
#include <string>

#include "crud_matcher_base.hpp"

namespace http::crud {

template<typename Matched, typename Request>
struct crud_matched_request : Matched {
crud_matched_request(const Matched &m, const Request &request) : Matched(m) , request(request) {}
Request request;
};

template<typename Request, typename Response, typename Regex, typename Matched>
struct crud_matcher_with_request : crud_matcher_base<Response, crud_matched_request<Matched, Request>> {};

}

#endif /* HTTP_CRUD_MATCHER_WITH_REQUEST_HPP */