RAVENNAKIT
Loading...
Searching...
No Matches
rav::HttpServer Class Reference

A high-level wrapper around boost::beast for creating an HTTP server. More...

#include <http_server.hpp>

Public Types

using Request = boost::beast::http::request<boost::beast::http::string_body>
using Response = boost::beast::http::response<boost::beast::http::string_body>
using Handler = std::function<void(const Request&, Response&, PathMatcher::Parameters&)>

Public Member Functions

 HttpServer (boost::asio::io_context &io_context)
 Constructs a new HttpServer using the given io_context.
 ~HttpServer ()
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.
boost::asio::ip::tcp::endpoint get_local_endpoint () const
std::string get_address_string () const
size_t get_client_count () const
void get (const std::string_view pattern, Handler handler)
 Adds a handler for GET requests to the given pattern.
void post (const std::string_view pattern, Handler handler)
 Adds a handler for POST requests to the given pattern.
void options (const std::string_view pattern, Handler handler)
 Adds a handler for OPTIONS requests to the given pattern.
void patch (const std::string_view pattern, Handler handler)
 Adds a handler for PATCH requests to the given pattern.

Static Public Attributes

static constexpr auto k_timeout_seconds = 5
 The time to wait for a request before closing the connection.

Detailed Description

A high-level wrapper around boost::beast for creating an HTTP server.

Definition at line 45 of file http_server.hpp.

Member Typedef Documentation

◆ Handler

using rav::HttpServer::Handler = std::function<void(const Request&, Response&, PathMatcher::Parameters&)>

Definition at line 49 of file http_server.hpp.

◆ Request

using rav::HttpServer::Request = boost::beast::http::request<boost::beast::http::string_body>

Definition at line 47 of file http_server.hpp.

◆ Response

using rav::HttpServer::Response = boost::beast::http::response<boost::beast::http::string_body>

Definition at line 48 of file http_server.hpp.

Constructor & Destructor Documentation

◆ HttpServer()

rav::HttpServer::HttpServer ( boost::asio::io_context & io_context)
explicit

Constructs a new HttpServer using the given io_context.

The io_context must be single threaded.

Parameters
io_contextThe io_context to use for the server.

◆ ~HttpServer()

rav::HttpServer::~HttpServer ( )

Member Function Documentation

◆ get()

void rav::HttpServer::get ( const std::string_view pattern,
Handler handler )
inline

Adds a handler for GET requests to the given pattern.

Parameters
patternThe pattern to match against the request path.
handlerThe handler to call when a request matches the pattern.

Definition at line 96 of file http_server.hpp.

◆ get_address_string()

std::string rav::HttpServer::get_address_string ( ) const
nodiscard
Returns
The address string of the server in the format "address:port".

◆ get_client_count()

size_t rav::HttpServer::get_client_count ( ) const
nodiscard
Returns
The number of active client sessions.

◆ get_local_endpoint()

boost::asio::ip::tcp::endpoint rav::HttpServer::get_local_endpoint ( ) const
nodiscard
Returns
The local (listening) endpoint of the server.

◆ options()

void rav::HttpServer::options ( const std::string_view pattern,
Handler handler )
inline

Adds a handler for OPTIONS requests to the given pattern.

Parameters
patternThe pattern to match against the request path.
handlerThe handler to call when a request matches the pattern.

Definition at line 114 of file http_server.hpp.

◆ patch()

void rav::HttpServer::patch ( const std::string_view pattern,
Handler handler )
inline

Adds a handler for PATCH requests to the given pattern.

Parameters
patternThe pattern to match against the request path.
handlerThe handler to call when a request matches the pattern.

Definition at line 123 of file http_server.hpp.

◆ post()

void rav::HttpServer::post ( const std::string_view pattern,
Handler handler )
inline

Adds a handler for POST requests to the given pattern.

Parameters
patternThe pattern to match against the request path.
handlerThe handler to call when a request matches the pattern.

Definition at line 105 of file http_server.hpp.

◆ start()

boost::system::result< void > rav::HttpServer::start ( std::string_view bind_address,
uint16_t port )

Starts the server on the given host and port.

Parameters
bind_addressThe address to bind to.
portThe port to bind to.
Returns
An error code if the server fails to start, or an empty result if it succeeds.

◆ stop()

void rav::HttpServer::stop ( )

Stops the server and closes all client sessions.

Member Data Documentation

◆ k_timeout_seconds

auto rav::HttpServer::k_timeout_seconds = 5
staticconstexpr

The time to wait for a request before closing the connection.

Definition at line 52 of file http_server.hpp.


The documentation for this class was generated from the following file: