Line data Source code
1 : #ifndef THINGER_HTTP_ROUTE_BUILDER_HPP
2 : #define THINGER_HTTP_ROUTE_BUILDER_HPP
3 :
4 : #include <deque>
5 : #include <string>
6 : #include "route.hpp"
7 : #include "route_tree.hpp"
8 : #include "../../common/http_request.hpp"
9 :
10 : namespace thinger::http {
11 :
12 : class route_builder {
13 : public:
14 34202 : route_builder(method http_method, std::deque<route>& routes, route_tree& tree,
15 : const nlohmann::json* schema_components = nullptr)
16 34202 : : method_(http_method), routes_(routes), tree_(tree), schema_components_(schema_components) {}
17 :
18 : // Create a new route with the given pattern (throws std::regex_error or
19 : // std::invalid_argument if the pattern is not valid)
20 34202 : route& operator[](const std::string& pattern) {
21 34202 : auto& added = routes_.emplace_back(pattern);
22 34196 : added.set_schema_components(schema_components_);
23 : try {
24 34196 : tree_.insert(added, get_method(method_));
25 0 : } catch (...) {
26 0 : routes_.pop_back();
27 0 : throw;
28 0 : }
29 34196 : return added;
30 : }
31 :
32 : private:
33 : method method_;
34 : std::deque<route>& routes_;
35 : route_tree& tree_;
36 : const nlohmann::json* schema_components_;
37 : };
38 :
39 : } // namespace thinger::http
40 :
41 : #endif // THINGER_HTTP_ROUTE_BUILDER_HPP
|