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/interface/Concepts.hpp"
27#include "web/ng/Connection.hpp"
28#include "web/ng/MessageHandler.hpp"
29#include "web/ng/ProcessingPolicy.hpp"
30#include "web/ng/Response.hpp"
31#include "web/ng/impl/ConnectionHandler.hpp"
32
33#include <boost/asio/io_context.hpp>
34#include <boost/asio/ip/tcp.hpp>
35#include <boost/asio/spawn.hpp>
36#include <boost/asio/ssl/context.hpp>
37
38#include <cstddef>
39#include <functional>
40#include <optional>
41#include <string>
42
43namespace web::ng {
44
48class Server : public ServerTag {
49public:
54 using OnConnectCheck = std::function<std::expected<void, Response>(Connection const&)>;
55
56 using OnIpChangeHook = impl::ConnectionHandler::OnIpChangeHook;
57
61 using OnDisconnectHook = impl::ConnectionHandler::OnDisconnectHook;
62
66 struct Hooks {
67 OnConnectCheck onConnectCheck;
68 OnIpChangeHook onIpChangeHook;
69 OnDisconnectHook onDisconnectHook;
70 };
71
72private:
73 util::Logger log_{"WebServer"};
74 util::Logger perfLog_{"Performance"};
75
76 std::reference_wrapper<boost::asio::io_context> ctx_;
77 std::optional<boost::asio::ssl::context> sslContext_;
78
79 util::TagDecoratorFactory tagDecoratorFactory_;
80
81 impl::ConnectionHandler connectionHandler_;
82 boost::asio::ip::tcp::endpoint endpoint_;
83
84 OnConnectCheck onConnectCheck_;
85
86 bool running_{false};
87
88public:
103 Server(
104 boost::asio::io_context& ctx,
105 boost::asio::ip::tcp::endpoint endpoint,
106 std::optional<boost::asio::ssl::context> sslContext,
107 ProcessingPolicy processingPolicy,
108 std::optional<size_t> parallelRequestLimit,
109 util::TagDecoratorFactory tagDecoratorFactory,
110 ProxyIpResolver proxyIpResolver,
111 std::optional<size_t> maxSubscriptionSendQueueSize,
112 Hooks hooks
113 );
114
118 Server(Server const&) = delete;
119
123 Server(Server&&) = delete;
124
132 void
133 onGet(std::string const& target, MessageHandler handler);
134
142 void
143 onPost(std::string const& target, MessageHandler handler);
144
151 void
152 onWs(MessageHandler handler);
153
159 std::optional<std::string>
160 run();
161
168 void
169 stop(boost::asio::yield_context yield);
170
171private:
172 void
173 handleConnection(boost::asio::ip::tcp::socket socket, boost::asio::yield_context yield);
174};
175
186std::expected<Server, std::string>
187makeServer(
189 Server::OnConnectCheck onConnectCheck,
190 Server::OnIpChangeHook onIpChangeHook,
191 Server::OnDisconnectHook onDisconnectHook,
192 boost::asio::io_context& context
193);
194
195} // namespace web::ng
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:95
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
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:54
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:61
A tag class for server to help identify Server in templated code.
Definition Concepts.hpp:46
A struct that holds all the hooks for the server.
Definition Server.hpp:66