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/log/Logger.hpp"
24#include "util/newconfig/ConfigDefinition.hpp"
25#include "web/ng/Connection.hpp"
26#include "web/ng/MessageHandler.hpp"
27#include "web/ng/ProcessingPolicy.hpp"
28#include "web/ng/Response.hpp"
29#include "web/ng/impl/ConnectionHandler.hpp"
30
31#include <boost/asio/io_context.hpp>
32#include <boost/asio/ip/tcp.hpp>
33#include <boost/asio/spawn.hpp>
34#include <boost/asio/ssl/context.hpp>
35
36#include <concepts>
37#include <cstddef>
38#include <functional>
39#include <optional>
40#include <string>
41
42namespace web::ng {
43
47struct ServerTag {
48 virtual ~ServerTag() = default;
49};
50
51template <typename T>
52concept SomeServer = std::derived_from<T, ServerTag>;
53
57class Server : public ServerTag {
58public:
63 using OnConnectCheck = std::function<std::expected<void, Response>(Connection const&)>;
64
68 using OnDisconnectHook = impl::ConnectionHandler::OnDisconnectHook;
69
70private:
71 util::Logger log_{"WebServer"};
72 util::Logger perfLog_{"Performance"};
73
74 std::reference_wrapper<boost::asio::io_context> ctx_;
75 std::optional<boost::asio::ssl::context> sslContext_;
76
77 util::TagDecoratorFactory tagDecoratorFactory_;
78
79 impl::ConnectionHandler connectionHandler_;
80 boost::asio::ip::tcp::endpoint endpoint_;
81
82 OnConnectCheck onConnectCheck_;
83
84 bool running_{false};
85
86public:
101 Server(
102 boost::asio::io_context& ctx,
103 boost::asio::ip::tcp::endpoint endpoint,
104 std::optional<boost::asio::ssl::context> sslContext,
105 ProcessingPolicy processingPolicy,
106 std::optional<size_t> parallelRequestLimit,
107 util::TagDecoratorFactory tagDecoratorFactory,
108 std::optional<size_t> maxSubscriptionSendQueueSize,
109 OnConnectCheck onConnectCheck,
110 OnDisconnectHook onDisconnectHook
111 );
112
116 Server(Server const&) = delete;
117
121 Server(Server&&) = default;
122
130 void
131 onGet(std::string const& target, MessageHandler handler);
132
140 void
141 onPost(std::string const& target, MessageHandler handler);
142
149 void
150 onWs(MessageHandler handler);
151
157 std::optional<std::string>
158 run();
159
166 void
167 stop(boost::asio::yield_context yield);
168
169private:
170 void
171 handleConnection(boost::asio::ip::tcp::socket socket, boost::asio::yield_context yield);
172};
173
184std::expected<Server, std::string>
185makeServer(
187 Server::OnConnectCheck onConnectCheck,
188 Server::OnDisconnectHook onDisconnectHook,
189 boost::asio::io_context& context
190);
191
192} // 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:169
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
A class representing a connection to a client.
Definition Connection.hpp:100
Web server class.
Definition Server.hpp:57
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:63
void onGet(std::string const &target, MessageHandler handler)
Set handler for GET requests.
Definition Server.cpp:214
std::optional< std::string > run()
Run the server.
Definition Server.cpp:235
Server(Server &&)=default
Move constructor is defaulted.
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, std::optional< size_t > maxSubscriptionSendQueueSize, OnConnectCheck onConnectCheck, OnDisconnectHook onDisconnectHook)
Construct a new Server object.
Definition Server.cpp:193
void onPost(std::string const &target, MessageHandler handler)
Set handler for POST requests.
Definition Server.cpp:221
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:228
impl::ConnectionHandler::OnDisconnectHook OnDisconnectHook
Hook called when any connection disconnects.
Definition Server.hpp:68
Definition Server.hpp:52
A tag class for server to help identify Server in templated code.
Definition Server.hpp:47