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
38 using OnIpChangeHook = impl::ConnectionHandler::OnIpChangeHook;
39
43 using OnDisconnectHook = impl::ConnectionHandler::OnDisconnectHook;
44
48 struct Hooks {
49 OnConnectCheck onConnectCheck;
50 OnIpChangeHook onIpChangeHook;
51 OnDisconnectHook onDisconnectHook;
52 };
53
54private:
55 util::Logger log_{"WebServer"};
56 util::Logger perfLog_{"Performance"};
57
58 std::reference_wrapper<boost::asio::io_context> ctx_;
59 std::optional<boost::asio::ssl::context> sslContext_;
60
61 util::TagDecoratorFactory tagDecoratorFactory_;
62
63 impl::ConnectionHandler connectionHandler_;
64 boost::asio::ip::tcp::endpoint endpoint_;
65
66 OnConnectCheck onConnectCheck_;
67
68 bool running_{false};
69
70public:
85 Server(
86 boost::asio::io_context& ctx,
87 boost::asio::ip::tcp::endpoint endpoint,
88 std::optional<boost::asio::ssl::context> sslContext,
89 ProcessingPolicy processingPolicy,
90 std::optional<size_t> parallelRequestLimit,
91 util::TagDecoratorFactory tagDecoratorFactory,
92 ProxyIpResolver proxyIpResolver,
93 std::optional<size_t> maxSubscriptionSendQueueSize,
94 Hooks hooks
95 );
96
100 Server(Server const&) = delete;
101
106 Server(Server&&) = delete;
107
115 void
116 onGet(std::string const& target, MessageHandler handler);
117
125 void
126 onPost(std::string const& target, MessageHandler handler);
127
134 void
135 onWs(MessageHandler handler);
136
142 std::optional<std::string>
143 run();
144
153 void
154 stop(boost::asio::yield_context yield);
155
156private:
157 void
158 handleConnection(boost::asio::ip::tcp::socket socket, boost::asio::yield_context yield);
159};
160
171std::expected<Server, std::string>
172makeServer(
174 Server::OnConnectCheck onConnectCheck,
175 Server::OnIpChangeHook onIpChangeHook,
176 Server::OnDisconnectHook onDisconnectHook,
177 boost::asio::io_context& context
178);
179
180} // namespace web::ng
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:77
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:93
void stop(boost::asio::yield_context yield)
Stop the server. This method will asynchronously sleep unless all the users are disconnected.
Definition Server.cpp:270
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:212
std::optional< std::string > run()
Run the server.
Definition Server.cpp:233
void onPost(std::string const &target, MessageHandler handler)
Set handler for POST requests.
Definition Server.cpp:219
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, std::optional< size_t > maxSubscriptionSendQueueSize, Hooks hooks)
Construct a new Server object.
Definition Server.cpp:191
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:226
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:43
A tag class for server to help identify Server in templated code.
Definition Concepts.hpp:31
A struct that holds all the hooks for the server.
Definition Server.hpp:48