AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
api.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 <functional>
25#include <memory>
26#include <optional>
27#include <set>
28#include <string>
29#include <string_view>
30#include <type_traits>
31#include <vector>
32
33#include "rest/cors.hpp"
34#include "rest/endpoint.hpp"
35#include "rest/filter.hpp"
36#include "rest/method.hpp"
37#include "rest/request.hpp"
38#include "rest/response.hpp"
39
40namespace anch::rest {
41
68 class ANCH_REST_SERVER_EXPORT Api {
69
71 struct Node;
72
74 struct Match;
75
76 // Attributes +
77 private:
79 std::unique_ptr<Node> _root;
80
82 std::vector<anch::rest::Filter> _filters;
83
85 std::optional<anch::rest::CorsOptions> _cors;
86 // Attributes -
87
88 // Constructors +
89 public:
93 Api();
94
96 Api(const Api&) = delete;
97
103 Api(Api&& other) noexcept;
104 // Constructors -
105
106 // Destructor +
107 public:
111 virtual ~Api();
112 // Destructor -
113
114 // Methods +
115 public:
128 template<typename F>
129 anch::rest::Endpoint<std::decay_t<F>>& route(anch::rest::Method method, std::string_view pattern, F&& function);
130
139 template<typename F>
140 anch::rest::Endpoint<std::decay_t<F>>& get(std::string_view pattern, F&& function);
141
150 template<typename F>
151 anch::rest::Endpoint<std::decay_t<F>>& post(std::string_view pattern, F&& function);
152
161 template<typename F>
162 anch::rest::Endpoint<std::decay_t<F>>& put(std::string_view pattern, F&& function);
163
172 template<typename F>
173 anch::rest::Endpoint<std::decay_t<F>>& patch(std::string_view pattern, F&& function);
174
183 template<typename F>
184 anch::rest::Endpoint<std::decay_t<F>>& del(std::string_view pattern, F&& function);
185
195
212
218 void validate() const;
219
228
230 Api& operator=(const Api&) = delete;
231
239 Api& operator=(Api&& other) noexcept;
240
241 private:
251 static std::vector<std::string> parsePattern(std::string_view pattern);
252
260 void addRoute(std::unique_ptr<anch::rest::Route> route);
261
271 std::vector<Match> match(const anch::rest::Request& request) const;
272
280 static std::set<anch::rest::Method> allowedMethods(const std::vector<Match>& matches);
281
291 anch::rest::Response preflight(const anch::rest::Request& request) const;
292
299 void addCorsHeaders(const anch::rest::Request& request, anch::rest::Response& response) const;
300
310 anch::rest::Response error(const anch::rest::Request& request, anch::rest::Status status, const char* message) const noexcept;
311
319 anch::rest::Response dispatch(anch::rest::Request& request);
320
329 anch::rest::Response next(anch::rest::Request& request, std::size_t index);
330 // Methods -
331
332 };
333
334}
335
336#include "rest/impl/api.hpp"
void validate() const
anch::rest::Endpoint< std::decay_t< F > > & route(anch::rest::Method method, std::string_view pattern, F &&function)
Api & operator=(const Api &)=delete
anch::rest::Endpoint< std::decay_t< F > > & del(std::string_view pattern, F &&function)
Api(Api &&other) noexcept
anch::rest::Endpoint< std::decay_t< F > > & put(std::string_view pattern, F &&function)
Api & operator=(Api &&other) noexcept
Api & cors(anch::rest::CorsOptions options)
virtual ~Api()
anch::rest::Endpoint< std::decay_t< F > > & patch(std::string_view pattern, F &&function)
anch::rest::Response handle(anch::rest::Request &request) noexcept
Api(const Api &)=delete
Api & filter(anch::rest::Filter filter)
anch::rest::Endpoint< std::decay_t< F > > & get(std::string_view pattern, F &&function)
anch::rest::Endpoint< std::decay_t< F > > & post(std::string_view pattern, F &&function)
Definition endpoint.hpp:324
Definition request.hpp:50
Definition response.hpp:47
AnCH REST library namespace.
Definition auth.hpp:29
Method
Definition method.hpp:36
std::function< anch::rest::Response(anch::rest::Request &, const anch::rest::Next &)> Filter
Definition filter.hpp:36
Status
Definition status.hpp:36
Definition cors.hpp:42