forked from cpv-project/cpv-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpServerRequestParametersFunctionHandler.hpp
69 lines (63 loc) · 2.38 KB
/
HttpServerRequestParametersFunctionHandler.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <tuple>
#include "../HttpContextExtensions.hpp"
#include "./HttpServerRequestHandlerBase.hpp"
namespace cpv {
/** Custom checker that checks whether Func invocable with given parameters */
template <class Params, class Func, std::size_t... I>
constexpr bool HttpServerRequestParametersFunctionHandlerTypeChecker(std::index_sequence<I...>) {
return std::is_invocable_r_v<seastar::future<>, Func, HttpContext&, decltype(
extensions::getParameter(
std::declval<HttpContext>(),
std::get<I>(std::declval<Params>())))...>;
}
/**
* Request handler that use custom function object and invoke it with given parameters.
*
* Params should be a tuple contains types which supports
* cpv::extensions::getParameter(const HttpContext&, const T&),
* there are some built-in types under namespace cpv::extensions::http_context_parameters,
* you can see them inside HttpContextExtensions.hpp.
*
* For example:
* ```
* using namespace cpv::extensions::http_context_parameters;
* HttpServerRequestParametersFunctionHandler handler(
* std::make_tuple(PathFragment(1), Query("abc")),
* [] (HttpContext& context, SharedString id, SharedString name) {
* ...
* });
* // will invoke func with:
* // func(context, request.getUri().getPathFragment(1), request.getUri().getQueryParameter("abc"));
* handler.handle(context, ...);
* ```
*/
template <class Params, class Func,
std::enable_if_t<
HttpServerRequestParametersFunctionHandlerTypeChecker<Params, Func>(
std::make_index_sequence<std::tuple_size_v<Params>>()), int> = 0>
class HttpServerRequestParametersFunctionHandler : public HttpServerRequestHandlerBase {
public:
/** Invoke custom function object with given parameters */
seastar::future<> handle(
HttpContext& context,
HttpServerRequestHandlerIterator) const override {
return handleImpl(context,
std::make_index_sequence<std::tuple_size_v<Params>>());
}
/** Constructor */
HttpServerRequestParametersFunctionHandler(Params params, Func func) :
params_(std::move(params)), func_(std::move(func)) { }
private:
/** The implementation of handle */
template <std::size_t... I>
seastar::future<> handleImpl(
HttpContext& context, std::index_sequence<I...>) const {
return func_(context,
extensions::getParameter(context, std::get<I>(params_))...);
}
private:
Params params_;
Func func_;
};
}