Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Server.hpp
1#pragma once
2
3#include "util/Taggable.hpp"
4#include "util/config/ConfigDefinition.hpp"
5#include "util/log/Logger.hpp"
6#include "web/ProxyIpResolver.hpp"
7#include "web/interface/Concepts.hpp"
8#include "web/ng/Connection.hpp"
9#include "web/ng/MessageHandler.hpp"
10#include "web/ng/ProcessingPolicy.hpp"
11#include "web/ng/Response.hpp"
12#include "web/ng/impl/ConnectionHandler.hpp"
13
14#include <boost/asio/io_context.hpp>
15#include <boost/asio/ip/tcp.hpp>
16#include <boost/asio/spawn.hpp>
17#include <boost/asio/ssl/context.hpp>
18
19#include <cstddef>
20#include <functional>
21#include <optional>
22#include <string>
23
24namespace web::ng {
25
29class Server : public ServerTag {
30public:
36 using OnConnectCheck = std::function<std::expected<void, Response>(Connection const&)>;
37
43 using OnIpChangeHook = impl::ConnectionHandler::OnIpChangeHook;
44
48 using OnDisconnectHook = impl::ConnectionHandler::OnDisconnectHook;
49
58
59private:
60 util::Logger log_{"WebServer"};
61 util::Logger perfLog_{"Performance"};
62
63 std::reference_wrapper<boost::asio::io_context> ctx_;
64 std::optional<boost::asio::ssl::context> sslContext_;
65
66 util::TagDecoratorFactory tagDecoratorFactory_;
67 size_t maxSubscriptionSendQueueSize_;
68
69 impl::ConnectionHandler connectionHandler_;
70 boost::asio::ip::tcp::endpoint endpoint_;
71
72 OnConnectCheck onConnectCheck_;
73
74 bool running_{false};
75
76public:
91 Server(
92 boost::asio::io_context& ctx,
93 boost::asio::ip::tcp::endpoint endpoint,
94 std::optional<boost::asio::ssl::context> sslContext,
95 ProcessingPolicy processingPolicy,
96 std::optional<size_t> parallelRequestLimit,
97 util::TagDecoratorFactory tagDecoratorFactory,
98 ProxyIpResolver proxyIpResolver,
99 size_t maxSubscriptionSendQueueSize,
100 Hooks hooks
101 );
102
106 Server(Server const&) = delete;
107
112 Server(Server&&) = delete;
113
121 void
122 onGet(std::string const& target, MessageHandler handler);
123
131 void
132 onPost(std::string const& target, MessageHandler handler);
133
140 void
141 onWs(MessageHandler handler);
142
148 std::optional<std::string>
149 run();
150
159 void
160 stop(boost::asio::yield_context yield);
161
162private:
163 void
164 handleConnection(boost::asio::ip::tcp::socket socket, boost::asio::yield_context yield);
165};
166
178std::expected<Server, std::string>
181 Server::OnConnectCheck onConnectCheck,
182 Server::OnIpChangeHook onIpChangeHook,
183 Server::OnDisconnectHook onDisconnectHook,
184 boost::asio::io_context& context
185);
186
187} // namespace web::ng
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
A factory for TagDecorator instantiation.
Definition Taggable.hpp:165
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
A class representing a connection to a client.
Definition Connection.hpp:117
void stop(boost::asio::yield_context yield)
Stop the server. This method will asynchronously sleep unless all the users are disconnected.
Definition Server.cpp:278
std::function< std::expected< void, Response >(Connection const &)> OnConnectCheck
Check to perform for each new client connection. The check takes client ip as input and returns a Res...
Definition Server.hpp:36
void onGet(std::string const &target, MessageHandler handler)
Set handler for GET requests.
Definition Server.cpp:220
std::optional< std::string > run()
Run the server.
Definition Server.cpp:241
impl::ConnectionHandler::OnIpChangeHook OnIpChangeHook
Hook called when a connection's client ip changes (e.g. resolved from a proxy).
Definition Server.hpp:43
Server(boost::asio::io_context &ctx, boost::asio::ip::tcp::endpoint endpoint, std::optional< boost::asio::ssl::context > sslContext, ProcessingPolicy processingPolicy, std::optional< size_t > parallelRequestLimit, util::TagDecoratorFactory tagDecoratorFactory, ProxyIpResolver proxyIpResolver, size_t maxSubscriptionSendQueueSize, Hooks hooks)
Construct a new Server object.
Definition Server.cpp:198
void onPost(std::string const &target, MessageHandler handler)
Set handler for POST requests.
Definition Server.cpp:227
Server(Server const &)=delete
Copy constructor is deleted. The Server couldn't be copied.
void onWs(MessageHandler handler)
Set handler for WebSocket requests.
Definition Server.cpp:234
Server(Server &&)=delete
Move constructor is deleted because connectionHandler_ contains references to some fields of the Serv...
impl::ConnectionHandler::OnDisconnectHook OnDisconnectHook
Hook called when any connection disconnects.
Definition Server.hpp:48
The next generation web server implementation.
Definition Connection.cpp:10
std::expected< Server, std::string > makeServer(util::config::ClioConfigDefinition const &config, Server::OnConnectCheck onConnectCheck, Server::OnIpChangeHook onIpChangeHook, Server::OnDisconnectHook onDisconnectHook, boost::asio::io_context &context)
Create a new Server.
Definition Server.cpp:351
std::function< Response( Request const &, ConnectionMetadata &, SubscriptionContextPtr, boost::asio::yield_context)> MessageHandler
Handler for messages.
Definition MessageHandler.hpp:17
ProcessingPolicy
Requests processing policy.
Definition ProcessingPolicy.hpp:8
A tag class for server to help identify Server in templated code.
Definition Concepts.hpp:33
A struct that holds all the hooks for the server.
Definition Server.hpp:53
OnConnectCheck onConnectCheck
Check performed for each new client connection.
Definition Server.hpp:54
OnDisconnectHook onDisconnectHook
Hook called when a connection disconnects.
Definition Server.hpp:56
OnIpChangeHook onIpChangeHook
Hook called when a connection's client ip changes.
Definition Server.hpp:55