Decentralised Art Server
High-performance C++ backend that exposes HTML interface and a secure REST API for managing Performative Transactions entities
 
Loading...
Searching...
No Matches
route.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <functional>
4using namespace std::placeholders;
5
6#include <utility>
7#include <regex>
8#include <vector>
9#include <cassert>
10
11#include "native.h"
12#include <asio.hpp>
13#include <absl/hash/hash.h>
14#include <absl/container/flat_hash_map.h>
15
16#include "http.hpp"
17
18#include "route_arg.hpp"
19#include "route_key.hpp"
20
21namespace dcn
22{
23 using QueryArgsList = absl::flat_hash_map<std::string, RouteArg>;
24
31 {
32 private:
33 class Base
34 {
35 public:
36 virtual ~Base() = default;
37 };
38
39 template<typename... Args>
40 struct Wrapper : public Base
41 {
42 Wrapper(std::function<asio::awaitable<http::Response>(Args...)> func)
43 : function(std::move(func))
44 {}
45
46 std::function<asio::awaitable<http::Response>(Args...)> function;
47 };
48
49 public:
50
51 template<typename... Args>
52 RouteHandlerFunc(std::function<asio::awaitable<http::Response>(Args...)> func)
53 : _base(std::make_unique<Wrapper<Args...>>(std::move(func)))
54 {}
55
57
58 template<typename... Args>
59 asio::awaitable<http::Response> operator()(Args && ... args) const
60 {
61 Wrapper<Args...>* wrapper_ptr = dynamic_cast<Wrapper<Args...>*>(_base.get());
62
63 if(wrapper_ptr)
64 {
65 co_return co_await wrapper_ptr->function(std::forward<Args>(args)...);
66 }
67 else
68 {
69 throw std::runtime_error("Invalid arguments to function object call!");
70 }
71 }
72
73 private:
74 std::unique_ptr<Base> _base;
75 };
76
82 class Router
83 {
84 public:
85 Router() = default;
86 ~Router() = default;
87
89
90 std::tuple<const RouteHandlerFunc *, std::vector<RouteArg>, QueryArgsList> findRoute(const http::Request & request) const;
91 protected:
92 std::tuple<bool, std::vector<RouteArg>, QueryArgsList> doesRouteMatch(
93 const RouteKey & route,
95 const std::string & module_path,
96 const std::vector<std::string> & request_path_info_segments,
97 const absl::flat_hash_map<std::string, std::string> request_query_segments) const;
98
99 private:
100 absl::flat_hash_map<RouteKey, RouteHandlerFunc> _routes;
101 };
102}
A class representing a route handler function.
Definition route.hpp:31
RouteHandlerFunc(std::function< asio::awaitable< http::Response >(Args...)> func)
Definition route.hpp:52
asio::awaitable< http::Response > operator()(Args &&... args) const
Definition route.hpp:59
RouteHandlerFunc(RouteHandlerFunc &&other)=default
A class representing a route key, which is a combination of a HTTP method and a URL.
Definition route_key.hpp:20
A class representing a router for handling HTTP requests.
Definition route.hpp:83
void addRoute(RouteKey route, RouteHandlerFunc handler)
Definition route.cpp:153
std::tuple< const RouteHandlerFunc *, std::vector< RouteArg >, QueryArgsList > findRoute(const http::Request &request) const
Definition route.cpp:122
Router()=default
std::tuple< bool, std::vector< RouteArg >, QueryArgsList > doesRouteMatch(const RouteKey &route, const http::Method &request_method, const std::string &module_path, const std::vector< std::string > &request_path_info_segments, const absl::flat_hash_map< std::string, std::string > request_query_segments) const
Definition route.cpp:5
~Router()=default
Definition http.hpp:110
Method
Enum to represent the request method.
Definition http_method.hpp:31
Definition decentralised_art.hpp:30
absl::flat_hash_map< std::string, RouteArg > QueryArgsList
Definition route.hpp:23