RAVENNAKIT
Loading...
Searching...
No Matches
http_server_example.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3 * Project: RAVENNAKIT (RAVENNA / AES67 / ST2110-30 SDK)
4 * Copyright (c) 2024-2025 Sound on Digital
5 *
6 * This file is part of RAVENNAKIT.
7 *
8 * RAVENNAKIT is dual-licensed:
9 * 1) Under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version (the "AGPL License"); and
12 * 2) Under a commercial license from Sound on Digital, for customers who
13 * cannot (or do not wish to) comply with the AGPL License terms.
14 *
15 * If you obtained this file under the AGPL License, you may redistribute it
16 * and/or modify it under the terms of the AGPL License. See the LICENSE
17 * file in the project root for details.
18 *
19 * For commercial licensing, support, and other inquiries, please visit:
20 *
21 * https://ravennakit.com
22 *
23 */
24
28
29int main() {
32
33 boost::asio::io_context io_context;
34
35 // Create a server instance
36 rav::HttpServer server(io_context);
37
39 response.result(boost::beast::http::status::ok);
40 response.set(boost::beast::http::field::content_type, "text/plain");
41 response.body() = "Hello, World!";
42 response.prepare_payload();
43 });
44
45 server.get(
46 "/shutdown",
47 [&io_context, &server](const rav::HttpServer::Request&, rav::HttpServer::Response& response, rav::PathMatcher::Parameters&) {
48 response.result(boost::beast::http::status::ok);
49 response.set(boost::beast::http::field::content_type, "text/plain");
50 response.body() = "Shutting down server...";
51 response.prepare_payload();
52
53 boost::asio::post(io_context, [&server] {
54 server.stop();
55 });
56 }
57 );
58
59 // Start the server
60 const auto result = server.start("127.0.0.1", 8080);
61 if (result.has_error()) {
62 RAV_LOG_ERROR("Error starting server: {}", result.error().message());
63 return 1;
64 }
65
66 RAV_LOG_INFO("Server started at http://{}", server.get_address_string());
67 RAV_LOG_INFO("Visit http://{}/shutdown to stop the server", server.get_address_string());
68
69 // Run the io_context to start accepting connections
70 io_context.run();
71
72 return 0;
73}
A high-level wrapper around boost::beast for creating an HTTP server.
boost::beast::http::response< boost::beast::http::string_body > Response
boost::system::result< void > start(std::string_view bind_address, uint16_t port)
Starts the server on the given host and port.
void stop()
Stops the server and closes all client sessions.
std::string get_address_string() const
boost::beast::http::request< boost::beast::http::string_body > Request
void get(const std::string_view pattern, Handler handler)
Adds a handler for GET requests to the given pattern.
Contains parameters extracted from a path with convenience functions to convert to integers.
int main()
#define RAV_LOG_ERROR(...)
Definition log.hpp:106
#define RAV_LOG_INFO(...)
Definition log.hpp:120
void set_log_level_from_env(const char *env_var="RAV_LOG_LEVEL")
Tries to find given environment variable and set the log level accordingly.
Definition log.hpp:216
void do_system_checks()
Definition system.hpp:33