Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Server.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Taggable.hpp"
23#include "util/config/ConfigDefinition.hpp"
24#include "util/log/Logger.hpp"
25#include "web/ProxyIpResolver.hpp"
26#include "web/ng/Connection.hpp"
27#include "web/ng/MessageHandler.hpp"
28#include "web/ng/ProcessingPolicy.hpp"
29#include "web/ng/Response.hpp"
30#include "web/ng/impl/ConnectionHandler.hpp"
31
32#include <boost/asio/io_context.hpp>
33#include <boost/asio/ip/tcp.hpp>
34#include <boost/asio/spawn.hpp>
35#include <boost/asio/ssl/context.hpp>
36
37#include <concepts>
38#include <cstddef>
39#include <functional>
40#include <optional>
41#include <string>
42
43namespace web::ng {
44
48struct ServerTag {
49 virtual ~ServerTag() = default;
50};
51
52template <typename T>
53concept SomeServer = std::derived_from<T, ServerTag>;
54
58class Server : public ServerTag {
59public:
64 using OnConnectCheck = std::function<std::expected<void, Response>(Connection const&)>;
65
66 using OnIpChangeHook = impl::ConnectionHandler::OnIpChangeHook;
67
71 using OnDisconnectHook = impl::ConnectionHandler::OnDisconnectHook;
72
76 struct Hooks {
77 OnConnectCheck onConnectCheck;
78 OnIpChangeHook onIpChangeHook;
79 OnDisconnectHook onDisconnectHook;
80 };
81
82private:
83 util::Logger log_{"WebServer"};
84 util::Logger perfLog_{"Performance"};
85
86 std::reference_wrapper<boost::asio::io_context> ctx_;
87 std::optional<boost::asio::ssl::context> sslContext_;
88
89 util::TagDecoratorFactory tagDecoratorFactory_;
90
91 impl::ConnectionHandler connectionHandler_;
92 boost::asio::ip::tcp::endpoint endpoint_;
93
94 OnConnectCheck onConnectCheck_;
95
96 bool running_{false};
97
98public:
113 Server(
114 boost::asio::io_context& ctx,
115 boost::asio::ip::tcp::endpoint endpoint,
116 std::optional<boost::asio::ssl::context> sslContext,
117 ProcessingPolicy processingPolicy,
118 std::optional<size_t> parallelRequestLimit,
119 util::TagDecoratorFactory tagDecoratorFactory,
120 ProxyIpResolver proxyIpResolver,
121 std::optional<size_t> maxSubscriptionSendQueueSize,
122 Hooks hooks
123 );
124
128 Server(Server const&) = delete;
129
133 Server(Server&&) = delete;
134
142 void
143 onGet(std::string const& target, MessageHandler handler);
144
152 void
153 onPost(std::string const& target, MessageHandler handler);
154
161 void
162 onWs(MessageHandler handler);
163
169 std::optional<std::string>
170 run();
171
178 void
179 stop(boost::asio::yield_context yield);
180
181private:
182 void
183 handleConnection(boost::asio::ip::tcp::socket socket, boost::asio::yield_context yield);
184};
185
196std::expected<Server, std::string>
197makeServer(
199 Server::OnConnectCheck onConnectCheck,
200 Server::OnIpChangeHook onIpChangeHook,
201 Server::OnDisconnectHook onDisconnectHook,
202 boost::asio::io_context& context
203);
204
205} // namespace web::ng
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:87
A factory for TagDecorator instantiation.
Definition Taggable.hpp:182
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
A class representing a connection to a client.
Definition Connection.hpp:112
Web server class.
Definition Server.hpp:58
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:64
void onGet(std::string const &target, MessageHandler handler)
Set handler for GET requests.
Definition Server.cpp:223
std::optional< std::string > run()
Run the server.
Definition Server.cpp:244
void onPost(std::string const &target, MessageHandler handler)
Set handler for POST requests.
Definition Server.cpp:230
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:202
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:237
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:71
Definition Server.hpp:53
A tag class for server to help identify Server in templated code.
Definition Server.hpp:48
A struct that holds all the hooks for the server.
Definition Server.hpp:76