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 "utils.hpp"
14#include "session_manager.hpp"
15#include "session.hpp"
16#include "http.hpp"
17#include "route.hpp"
18
19namespace dcn
20{
24 using HandlerDefinition = std::function<asio::awaitable<dcn::http::Response>(const dcn::http::Request &, std::vector<RouteArg>, QueryArgsList)>;
25
26 // TODO: Add a concept to check if the handler is callable with the expected arguments
27 // template<class F, class ... Args>
28 // concept HandlerCallable = requires(F handler, Args... args)
29 // {
30 // { t(args...) } -> std::same_as<asio::awaitable<dcn::http::Response>>;
31 // };
32
39 class Server
40 {
41 public:
42 Server(asio::io_context & io_context, asio::ip::tcp::endpoint endpoint);
43 ~Server() = default;
44
57 asio::awaitable<void> listen();
58
64 template<class F>
66 {
67 _router.addRoute(std::move(route), RouteHandlerFunc(
68 HandlerDefinition(std::move(handler)))
69 );
70 }
71
78 template<class F, class ... Args>
80 {
81 _router.addRoute(std::move(route), RouteHandlerFunc(
83 std::bind(handler, _1, _2, _3, std::forward<Args>(binded_args)...)
84 )
85 ));
86 }
87
93 void setIdleInterval(std::chrono::milliseconds idle_interval);
94
103 asio::awaitable<void> close();
104
105 protected:
106
113 asio::awaitable<void> handleConnection(asio::ip::tcp::socket sock);
114
126 asio::awaitable<void> readData(asio::ip::tcp::socket & sock, std::chrono::steady_clock::time_point & deadline);
127
139 asio::awaitable<void> writeData(asio::ip::tcp::socket & sock, std::string message);
140
141 private:
142 asio::io_context & _io_context;
143 asio::strand<asio::io_context::executor_type> _strand;
144 bool _close;
145
146 asio::ip::tcp::acceptor _acceptor;
147 Router _router;
148
149 SessionManager _session_mgr;
150
151 std::chrono::milliseconds _idle_interval;
152 };
153}
A class representing a route handler function.
Definition route.hpp:31
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
A class representing a server for handling HTTP requests.
Definition server.hpp:40
void addRoute(RouteKey route, F &&handler)
Adds a route to the server with a specified HTTP method and path.
Definition server.hpp:65
asio::awaitable< void > close()
Closes the server gracefully.
Definition server.cpp:15
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:79
asio::awaitable< void > writeData(asio::ip::tcp::socket &sock, std::string message)
Asynchronously writes data to a TCP socket.
Definition server.cpp:170
~Server()=default
asio::awaitable< void > handleConnection(asio::ip::tcp::socket sock)
Definition server.cpp:47
void setIdleInterval(std::chrono::milliseconds idle_interval)
Set the idle interval after which the server will close the connection.
Definition server.cpp:42
asio::awaitable< void > listen()
Definition server.cpp:23
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:58
Definition session_manager.hpp:19
Definition http.hpp:110
Definition decentralised_art.hpp:30
absl::flat_hash_map< std::string, RouteArg > QueryArgsList
Definition route.hpp:23
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:24