22#include "anch-rest-server-export.hpp"
37#include "codec/codec.hpp"
38#include "codec/mediaType.hpp"
40#include "rest/body.hpp"
41#include "rest/method.hpp"
42#include "rest/paramConverter.hpp"
43#include "rest/request.hpp"
44#include "rest/response.hpp"
47namespace anch::rest::detail {
55 struct FunctionTraits: FunctionTraits<decltype(&F::operator())> {};
58 template<
typename R,
typename... A>
59 struct FunctionTraits<R(A...)> {
63 using Args = std::tuple<A...>;
67 template<
typename R,
typename... A>
68 struct FunctionTraits<R(A...) noexcept>: FunctionTraits<R(A...)> {};
71 template<
typename R,
typename... A>
72 struct FunctionTraits<R(*)(A...)>: FunctionTraits<R(A...)> {};
75 template<
typename R,
typename... A>
76 struct FunctionTraits<R(*)(A...) noexcept>: FunctionTraits<R(A...)> {};
79 template<
typename C,
typename R,
typename... A>
80 struct FunctionTraits<R(C::*)(A...)>: FunctionTraits<R(A...)> {};
83 template<
typename C,
typename R,
typename... A>
84 struct FunctionTraits<R(C::*)(A...) const>: FunctionTraits<R(A...)> {};
87 template<
typename C,
typename R,
typename... A>
88 struct FunctionTraits<R(C::*)(A...) noexcept>: FunctionTraits<R(A...)> {};
91 template<
typename C,
typename R,
typename... A>
92 struct FunctionTraits<R(C::*)(A...) const noexcept>: FunctionTraits<R(A...)> {};
100 template<std::
size_t I,
typename T,
bool = (I < std::tuple_size_v<T>)>
107 template<std::
size_t I,
typename T>
108 struct ElementOr<I, T, true> {
110 using Type = std::tuple_element_t<I, T>;
115 struct IsBody: std::false_type {
117 using Type = std::monostate;
122 struct IsBody<anch::rest::Body<T>>: std::true_type {
129 struct IsOptional: std::false_type {
136 struct IsOptional<std::optional<T>>: std::true_type {
149 using Args =
typename FunctionTraits<F>::Args;
152 using Result = std::remove_cvref_t<typename FunctionTraits<F>::Result>;
155 static constexpr std::size_t COUNT = std::tuple_size_v<Args>;
158 static constexpr bool HAS_REQUEST =
159 std::is_same_v<std::remove_cvref_t<typename ElementOr<0, Args>::Type>, anch::rest::Request>;
162 using Last = std::remove_cvref_t<
typename ElementOr<COUNT - 1, Args>::Type>;
165 static constexpr bool HAS_BODY = COUNT > 0 && IsBody<Last>::value;
168 using BodyType =
typename IsBody<Last>::Type;
171 static constexpr bool RAW_BODY = COUNT > 0 && std::is_same_v<
typename ElementOr<COUNT - 1, Args>::Type, std::istream&>;
174 static constexpr std::size_t PATH_COUNT = COUNT - (HAS_REQUEST ? 1 : 0) - ((HAS_BODY || RAW_BODY) ? 1 : 0);
181 template<std::
size_t I>
182 using PathArg = std::remove_cvref_t<std::tuple_element_t<I + (HAS_REQUEST ? 1 : 0), Args>>;
185 static constexpr bool SERIALIZED = !std::is_void_v<Result> && !std::is_same_v<Result, anch::rest::Response>;
188 static constexpr bool OPTIONAL = IsOptional<Result>::value;
191 using Value = std::conditional_t<SERIALIZED, typename IsOptional<Result>::Type, std::monostate>;
206 class ANCH_REST_SERVER_EXPORT
Route {
269 virtual std::optional<std::
string>
check() const = 0;
327 using Sig = anch::rest::detail::Signature<F>;
330 using Value =
typename Sig::Value;
333 using BodyType =
typename Sig::BodyType;
340 template<std::size_t... I>
341 static consteval bool pathParametersSupported(std::index_sequence<I...>);
343 static_assert(pathParametersSupported(std::make_index_sequence<Sig::PATH_COUNT>()),
344 "Endpoint arguments are ([const anch::rest::Request&], path parameters..., [anch::rest::Body<T>]) "
345 "and path parameters have to be supported by anch::rest::ParamConverter");
346 static_assert(!Sig::HAS_BODY || std::is_default_constructible_v<BodyType>,
347 "Body type has to be default constructible");
355 std::vector<anch::codec::MediaType> _produced;
367 std::vector<Writer> _serializers;
370 std::vector<anch::codec::MediaType> _consumed;
376 std::optional<anch::rest::Status> _status;
452 virtual std::optional<std::
string>
check() const override;
463 template<std::
size_t... I>
465 std::vector<std::
string>& parameters,
466 std::index_sequence<I...>);
492#include "rest/impl/endpoint.hpp"
virtual std::size_t getPathParameterCount() const noexcept override
virtual std::optional< std::string > check() const override
virtual anch::rest::Response handle(anch::rest::Request &request, std::vector< std::string > ¶meters) override
Endpoint & status(anch::rest::Status status)
Endpoint(anch::rest::Method method, std::string pattern, std::vector< std::string > parameterNames, F function)
Definition request.hpp:50
Definition response.hpp:47
virtual std::size_t getPathParameterCount() const noexcept=0
Route(anch::rest::Method method, std::string pattern, std::vector< std::string > parameterNames)
const std::vector< std::string > & getParameterNames() const noexcept
virtual std::optional< std::string > check() const =0
anch::rest::Method _method
Definition endpoint.hpp:211
Route(const Route &)=delete
std::vector< std::string > _parameterNames
Definition endpoint.hpp:217
std::string _pattern
Definition endpoint.hpp:214
virtual anch::rest::Response handle(anch::rest::Request &request, std::vector< std::string > ¶meters)=0
const std::string & getPattern() const noexcept
anch::rest::Method getMethod() const noexcept
AnCH REST library namespace.
Definition auth.hpp:29
Method
Definition method.hpp:36
Status
Definition status.hpp:36
AnCH framework base namespace.
Definition app.hpp:30