AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
endpoint.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2026 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-rest-server-export.hpp"
23
24#include <cstddef>
25#include <functional>
26#include <istream>
27#include <optional>
28#include <ostream>
29#include <sstream>
30#include <string>
31#include <tuple>
32#include <type_traits>
33#include <utility>
34#include <variant>
35#include <vector>
36
37#include "codec/codec.hpp"
38#include "codec/mediaType.hpp"
39
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"
45
47namespace anch::rest::detail {
48
54 template<typename F>
55 struct FunctionTraits: FunctionTraits<decltype(&F::operator())> {};
56
58 template<typename R, typename... A>
59 struct FunctionTraits<R(A...)> {
61 using Result = R;
63 using Args = std::tuple<A...>;
64 };
65
67 template<typename R, typename... A>
68 struct FunctionTraits<R(A...) noexcept>: FunctionTraits<R(A...)> {};
69
71 template<typename R, typename... A>
72 struct FunctionTraits<R(*)(A...)>: FunctionTraits<R(A...)> {};
73
75 template<typename R, typename... A>
76 struct FunctionTraits<R(*)(A...) noexcept>: FunctionTraits<R(A...)> {};
77
79 template<typename C, typename R, typename... A>
80 struct FunctionTraits<R(C::*)(A...)>: FunctionTraits<R(A...)> {};
81
83 template<typename C, typename R, typename... A>
84 struct FunctionTraits<R(C::*)(A...) const>: FunctionTraits<R(A...)> {};
85
87 template<typename C, typename R, typename... A>
88 struct FunctionTraits<R(C::*)(A...) noexcept>: FunctionTraits<R(A...)> {};
89
91 template<typename C, typename R, typename... A>
92 struct FunctionTraits<R(C::*)(A...) const noexcept>: FunctionTraits<R(A...)> {};
93
100 template<std::size_t I, typename T, bool = (I < std::tuple_size_v<T>)>
101 struct ElementOr {
103 using Type = void;
104 };
105
107 template<std::size_t I, typename T>
108 struct ElementOr<I, T, true> {
110 using Type = std::tuple_element_t<I, T>;
111 };
112
114 template<typename T>
115 struct IsBody: std::false_type {
117 using Type = std::monostate;
118 };
119
121 template<typename T>
122 struct IsBody<anch::rest::Body<T>>: std::true_type {
124 using Type = T;
125 };
126
128 template<typename T>
129 struct IsOptional: std::false_type {
131 using Type = T;
132 };
133
135 template<typename T>
136 struct IsOptional<std::optional<T>>: std::true_type {
138 using Type = T;
139 };
140
146 template<typename F>
147 struct Signature {
149 using Args = typename FunctionTraits<F>::Args;
150
152 using Result = std::remove_cvref_t<typename FunctionTraits<F>::Result>;
153
155 static constexpr std::size_t COUNT = std::tuple_size_v<Args>;
156
158 static constexpr bool HAS_REQUEST =
159 std::is_same_v<std::remove_cvref_t<typename ElementOr<0, Args>::Type>, anch::rest::Request>;
160
162 using Last = std::remove_cvref_t<typename ElementOr<COUNT - 1, Args>::Type>;
163
165 static constexpr bool HAS_BODY = COUNT > 0 && IsBody<Last>::value;
166
168 using BodyType = typename IsBody<Last>::Type;
169
171 static constexpr bool RAW_BODY = COUNT > 0 && std::is_same_v<typename ElementOr<COUNT - 1, Args>::Type, std::istream&>;
172
174 static constexpr std::size_t PATH_COUNT = COUNT - (HAS_REQUEST ? 1 : 0) - ((HAS_BODY || RAW_BODY) ? 1 : 0);
175
181 template<std::size_t I>
182 using PathArg = std::remove_cvref_t<std::tuple_element_t<I + (HAS_REQUEST ? 1 : 0), Args>>;
183
185 static constexpr bool SERIALIZED = !std::is_void_v<Result> && !std::is_same_v<Result, anch::rest::Response>;
186
188 static constexpr bool OPTIONAL = IsOptional<Result>::value;
189
191 using Value = std::conditional_t<SERIALIZED, typename IsOptional<Result>::Type, std::monostate>;
192 };
193
194}
196
197namespace anch::rest {
198
206 class ANCH_REST_SERVER_EXPORT Route {
207
208 // Attributes +
209 protected:
212
214 std::string _pattern;
215
217 std::vector<std::string> _parameterNames;
218 // Attributes -
219
220 // Constructors +
221 public:
229 Route(anch::rest::Method method, std::string pattern, std::vector<std::string> parameterNames);
230
232 Route(const Route&) = delete;
233 // Constructors -
234
235 // Destructor +
236 public:
240 virtual ~Route();
241 // Destructor -
242
243 // Methods +
244 public:
255 virtual anch::rest::Response handle(anch::rest::Request& request, std::vector<std::string>& parameters) = 0;
256
262 virtual std::size_t getPathParameterCount() const noexcept = 0;
263
269 virtual std::optional<std::string> check() const = 0;
270
272 Route& operator=(const Route&) = delete;
273 // Methods -
274
275 // Accessors +
276 public:
282 anch::rest::Method getMethod() const noexcept;
283
289 const std::string& getPattern() const noexcept;
290
296 const std::vector<std::string>& getParameterNames() const noexcept;
297 // Accessors -
298
299 };
300
323 template<typename F>
324 class Endpoint: public anch::rest::Route {
325
327 using Sig = anch::rest::detail::Signature<F>;
328
330 using Value = typename Sig::Value;
331
333 using BodyType = typename Sig::BodyType;
334
340 template<std::size_t... I>
341 static consteval bool pathParametersSupported(std::index_sequence<I...>);
342
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");
348
349 // Attributes +
350 private:
352 F _function;
353
355 std::vector<anch::codec::MediaType> _produced;
356
358 struct Writer {
360 std::function<anch::codec::MediaType(const Value&)> contentType;
361
363 std::function<void(const Value&, std::ostream&, const anch::codec::MediaType&)> write;
364 };
365
367 std::vector<Writer> _serializers;
368
370 std::vector<anch::codec::MediaType> _consumed;
371
373 std::vector<std::function<void(BodyType&, std::istream&, const anch::codec::MediaType&)>> _deserializers;
374
376 std::optional<anch::rest::Status> _status;
377 // Attributes -
378
379 // Constructors +
380 public:
389 Endpoint(anch::rest::Method method, std::string pattern, std::vector<std::string> parameterNames, F function);
390 // Constructors -
391
392 // Methods +
393 public:
401 template<typename C>
403
412 template<typename C>
414
428
437 virtual anch::rest::Response handle(anch::rest::Request& request, std::vector<std::string>& parameters) override;
438
444 virtual std::size_t getPathParameterCount() const noexcept override;
445
452 virtual std::optional<std::string> check() const override;
453
454 private:
463 template<std::size_t... I>
464 typename Sig::Result invoke(anch::rest::Request& request,
465 std::vector<std::string>& parameters,
466 std::index_sequence<I...>);
467
475 anch::rest::Body<BodyType> readBody(anch::rest::Request& request);
476
485 anch::rest::Response serialize(Value value, std::size_t index) const;
486 // Methods -
487
488 };
489
490}
491
492#include "rest/impl/endpoint.hpp"
Definition mediaType.hpp:41
Definition body.hpp:38
Endpoint & consumes()
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 > &parameters) override
Endpoint & produces()
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 > &parameters)=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