ORE Studio 0.0.4
Loading...
Searching...
No Matches
http_server.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_HTTP_NET_HTTP_SERVER_HPP
21#define ORES_HTTP_NET_HTTP_SERVER_HPP
22
23#include <memory>
24#include <atomic>
25#include <boost/asio/io_context.hpp>
26#include <boost/asio/ip/tcp.hpp>
27#include <boost/asio/awaitable.hpp>
28#include "ores.http.api/net/router.hpp"
29#include "ores.http.api/net/http_session.hpp"
30#include "ores.http.api/net/http_server_options.hpp"
31#include "ores.security/jwt/jwt_authenticator.hpp"
32#include "ores.http.api/openapi/endpoint_registry.hpp"
33#include "ores.logging/make_logger.hpp"
34
35namespace ores::http::net {
36
40class http_server final {
41public:
42 explicit http_server(boost::asio::io_context& io_ctx,
43 const http_server_options& options);
44
48 std::shared_ptr<router> get_router() { return router_; }
49
53 std::shared_ptr<openapi::endpoint_registry> get_registry() { return registry_; }
54
58 std::shared_ptr<ores::security::jwt::jwt_authenticator> get_authenticator() { return authenticator_; }
59
66 void set_session_bytes_callback(session_bytes_callback callback) {
67 bytes_callback_ = std::move(callback);
68 }
69
73 boost::asio::awaitable<void> run();
74
78 void stop();
79
83 bool is_running() const { return running_.load(); }
84
85private:
86 inline static std::string_view logger_name = "ores.http.net.http_server";
87
88 static auto& lg() {
89 using namespace ores::logging;
90 static auto instance = make_logger(logger_name);
91 return instance;
92 }
93
94 void setup_builtin_routes();
95
96 boost::asio::awaitable<void> accept_connections();
97
98 boost::asio::io_context& io_ctx_;
99 http_server_options options_;
100 std::shared_ptr<router> router_;
101 std::shared_ptr<ores::security::jwt::jwt_authenticator> authenticator_;
102 std::shared_ptr<openapi::endpoint_registry> registry_;
103 std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
104 std::atomic<bool> running_{false};
105 std::atomic<std::uint32_t> active_connections_{0};
106 session_bytes_callback bytes_callback_;
107};
108
109}
110
111#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
HTTP server built on Boost.Beast.
Definition http_server.hpp:40
bool is_running() const
Returns whether the server is running.
Definition http_server.hpp:83
void set_session_bytes_callback(session_bytes_callback callback)
Sets the session bytes callback for tracking request/response sizes.
Definition http_server.hpp:66
std::shared_ptr< router > get_router()
Returns the router for registering endpoints.
Definition http_server.hpp:48
std::shared_ptr< ores::security::jwt::jwt_authenticator > get_authenticator()
Returns the JWT authenticator for token generation/validation.
Definition http_server.hpp:58
void stop()
Signals the server to stop accepting new connections.
Definition http_server.cpp:169
std::shared_ptr< openapi::endpoint_registry > get_registry()
Returns the endpoint registry for OpenAPI generation.
Definition http_server.hpp:53
boost::asio::awaitable< void > run()
Starts the server and accepts connections.
Definition http_server.cpp:101
Configuration options for the HTTP server.
Definition http_server_options.hpp:33