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
server.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4using namespace std::chrono_literals;
5
6#include "native.h"
7#include <asio.hpp>
8#include <asio/experimental/awaitable_operators.hpp>
9using namespace asio::experimental::awaitable_operators;
10
11#include <spdlog/spdlog.h>
12
13#include "async.hpp"
14#include "http.hpp"
15#include "route.hpp"
16
17namespace dcn::server
18{
22 using HandlerDefinition = std::function<asio::awaitable<dcn::http::Response>(const dcn::http::Request &, std::vector<RouteArg>, QueryArgsList)>;
23
24 // TODO: Add a concept to check if the handler is callable with the expected arguments
25 // template<class F, class ... Args>
26 // concept HandlerCallable = requires(F handler, Args... args)
27 // {
28 // { t(args...) } -> std::same_as<asio::awaitable<dcn::http::Response>>;
29 // };
30
37 class Server
38 {
39 public:
40 Server(asio::io_context & io_context, asio::ip::tcp::endpoint endpoint);
41 ~Server() = default;
42
55 asio::awaitable<void> listen();
56
62 template<class F>
63 void addRoute(RouteKey route, F && handler)
64 {
65 _router.addRoute(std::move(route), RouteHandlerFunc(
66 HandlerDefinition(std::move(handler)))
67 );
68 }
69
76 template<class F, class ... Args>
77 void addRoute(RouteKey route, F && handler, Args&&... binded_args)
78 {
79 _router.addRoute(std::move(route), RouteHandlerFunc(
81 std::bind(handler, _1, _2, _3, std::forward<Args>(binded_args)...)
82 )
83 ));
84 }
85
97 template<class F>
98 void addStreamingRoute(RouteKey route, F && handler)
99 {
100 _router.addRoute(std::move(route), RouteHandlerFunc(
101 StreamingHandlerDefinition(std::move(handler))
102 ));
103 }
104
108 template<class F, class ... Args>
109 void addStreamingRoute(RouteKey route, F && handler, Args&&... binded_args)
110 {
111 _router.addRoute(std::move(route), RouteHandlerFunc(
113 std::bind(handler, _1, _2, _3, _4, _5, std::forward<Args>(binded_args)...)
114 )
115 ));
116 }
117
123 void setIdleInterval(std::chrono::milliseconds idle_interval);
124
133 asio::awaitable<void> close();
134
135 protected:
136
143 asio::awaitable<void> handleConnection(asio::ip::tcp::socket sock);
144
156 asio::awaitable<void> readData(asio::ip::tcp::socket & sock, std::chrono::steady_clock::time_point & deadline);
157
169 asio::awaitable<void> writeData(asio::ip::tcp::socket & sock, std::string message);
170
171 private:
172 asio::io_context & _io_context;
173 asio::strand<asio::io_context::executor_type> _strand;
174 bool _close;
175
176 asio::ip::tcp::acceptor _acceptor;
177 Router _router;
178
179 std::chrono::milliseconds _idle_interval;
180 };
181}
Definition http.hpp:110
A class representing a route handler function.
Definition route.hpp:48
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:131
void addRoute(RouteKey route, RouteHandlerFunc handler)
Definition route.cpp:153
A class representing a server for handling HTTP requests.
Definition server.hpp:38
void addStreamingRoute(RouteKey route, F &&handler)
Adds a streaming route to the server.
Definition server.hpp:98
asio::awaitable< void > listen()
Definition server.cpp:26
asio::awaitable< void > handleConnection(asio::ip::tcp::socket sock)
Definition server.cpp:56
asio::awaitable< void > writeData(asio::ip::tcp::socket &sock, std::string message)
Asynchronously writes data to a TCP socket.
Definition server.cpp:213
asio::awaitable< void > close()
Closes the server gracefully.
Definition server.cpp:18
void addRoute(RouteKey route, F &&handler, Args &&... binded_args)
Adds a route to the server with a specified HTTP method and path.
Definition server.hpp:77
asio::awaitable< void > readData(asio::ip::tcp::socket &sock, std::chrono::steady_clock::time_point &deadline)
Reads data from the given socket and processes incoming HTTP requests.
Definition server.cpp:79
void addStreamingRoute(RouteKey route, F &&handler, Args &&... binded_args)
Adds a streaming route with extra bound arguments.
Definition server.hpp:109
void addRoute(RouteKey route, F &&handler)
Adds a route to the server with a specified HTTP method and path.
Definition server.hpp:63
void setIdleInterval(std::chrono::milliseconds idle_interval)
Set the idle interval after which the server will close the connection.
Definition server.cpp:51
Definition route.hpp:24
std::function< asio::awaitable< void >(asio::ip::tcp::socket &, const dcn::http::Request &, std::vector< RouteArg >, QueryArgsList, std::chrono::steady_clock::time_point &)> StreamingHandlerDefinition
Streaming handler signature.
Definition route.hpp:40
absl::flat_hash_map< std::string, RouteArg > QueryArgsList
Definition route.hpp:25
std::function< asio::awaitable< dcn::http::Response >(const dcn::http::Request &, std::vector< RouteArg >, QueryArgsList)> HandlerDefinition
Type representing a route handler function.
Definition server.hpp:22