Malloy
Loading...
Searching...
No Matches
malloy::server::router Class Referencefinal

#include <router.hpp>

Public Types

template<typename Derived>
using req_generator = std::shared_ptr<typename http::connection<Derived>::request_generator>
using request_header = boost::beast::http::request_header<>
using method_type = malloy::http::method
using request_type = malloy::http::request<>
using response_type = malloy::http::response<>

Public Member Functions

 router ()=default
 router (std::shared_ptr< spdlog::logger > logger)
 router (const router &other)=delete
 router (router &&other) noexcept=default
 ~router ()=default
router & operator= (const router &rhs)=delete
router & operator= (router &&rhs) noexcept=default
void set_logger (std::shared_ptr< spdlog::logger > logger)
bool add_subrouter (std::string resource, std::unique_ptr< router > sub_router)
bool add_subrouter (std::string resource, router &&sub_router)
template<concepts::request_filter ExtraInfo, concepts::route_handler< typename ExtraInfo::request_type > Func>
bool add (const method_type method, const std::string_view target, Func &&handler, ExtraInfo &&extra)
template<concepts::route_handler< typename detail::default_route_filter::request_type > Func>
auto add (const method_type method, const std::string_view target, Func &&handler)
bool add_preflight (std::string_view target, http::preflight_config cfg)
template<malloy::concepts::callable_string CacheControl>
bool add_file_serving (std::string resource, std::filesystem::path storage_base_path, const CacheControl &cc)
bool add_file_serving (std::string resource, std::filesystem::path storage_base_path)
bool add_redirect (malloy::http::status status, std::string &&resource_old, std::string &&resource_new)
bool add_websocket (std::string &&resource, typename websocket::connection::handler_t &&handler)
template<concepts::request_validator Policy>
void add_policy (const std::string &resource, Policy &&policy)
template<bool isWebsocket = false, typename Derived, typename Connection>
void handle_request (const std::filesystem::path &doc_root, const req_generator< Derived > &req, Connection &&connection)
constexpr std::string_view server_string () const

Friends

class routing_context

Detailed Description

An HTTP request router.

This class implements a basic router for HTTP requests.

Member Typedef Documentation

◆ method_type

◆ request_type

The request type to use.

◆ response_type

Constructor & Destructor Documentation

◆ router() [1/4]

malloy::server::router::router ( )
default

Default constructor.

◆ router() [2/4]

router::router ( std::shared_ptr< spdlog::logger > logger)
explicit

Constructor.

Parameters
loggerThe logger instance to use.
Here is the call graph for this function:

◆ router() [3/4]

malloy::server::router::router ( const router & other)
delete

Copy constructor.

Here is the call graph for this function:

◆ router() [4/4]

malloy::server::router::router ( router && other)
defaultnoexcept

Move constructor.

Here is the call graph for this function:

◆ ~router()

malloy::server::router::~router ( )
default

Destructor.

Here is the call graph for this function:

Member Function Documentation

◆ add()

template<concepts::request_filter ExtraInfo, concepts::route_handler< typename ExtraInfo::request_type > Func>
bool malloy::server::router::add ( const method_type method,
const std::string_view target,
Func && handler,
ExtraInfo && extra )
inline

Add an HTTP regex endpoint.

Template Parameters
ExtraInfoa type that satisfies request_filter. Used to provide additional customisation of request handling. Must satisfy request_filter Route concepts
Funcinvoked on a request to the specified target with the specified method. Must satisfy route_handler Route concepts
Parameters
methodThe HTTP method.
targetThe resource path (regex).
handlerThe handler to generate the response.
Returns
Whether adding the route was successful.

◆ add_file_serving() [1/2]

bool malloy::server::router::add_file_serving ( std::string resource,
std::filesystem::path storage_base_path )
inline

Add an HTTP file-serving location.

Parameters
resource
storage_base_path
Returns
Whether adding the file serving was successful.
Here is the call graph for this function:

◆ add_file_serving() [2/2]

template<malloy::concepts::callable_string CacheControl>
bool malloy::server::router::add_file_serving ( std::string resource,
std::filesystem::path storage_base_path,
const CacheControl & cc )
inline

Add an HTTP file-servinc location.

Template Parameters
CacheControl
Parameters
resource
storage_base_path
cc
Returns
Whether adding the file serving was successful.

◆ add_policy()

template<concepts::request_validator Policy>
void malloy::server::router::add_policy ( const std::string & resource,
Policy && policy )
inline

Add an access policy for a specific resource.

A policy allow restricting access to any resource registered on this router.

Parameters
resourceThe resource path.
policyThe policy.

◆ add_redirect()

bool router::add_redirect ( malloy::http::status status,
std::string && resource_old,
std::string && resource_new )

Adds an HTTP redirection rule.

Parameters
resource_old
resource_new
statusThe HTTP status code to use. This must be a 3xx status code.
Returns
Whether adding the redirect was successful.

◆ add_subrouter() [1/2]

bool router::add_subrouter ( std::string resource,
router && sub_router )

Add a sub-router for a specific resource.

Parameters
resourceThe resource base path for the sub-router.
sub_routerThe sub-router.
Returns
Whether adding the sub-router was successful.
Here is the call graph for this function:

◆ add_subrouter() [2/2]

bool router::add_subrouter ( std::string resource,
std::unique_ptr< router > sub_router )

Add a sub-router for a specific resource.

Parameters
resourceThe resource base path for the sub-router.
sub_routerThe sub-router.
Returns
Whether adding the sub-router was successful.

◆ add_websocket()

bool router::add_websocket ( std::string && resource,
typename websocket::connection::handler_t && handler )

Add a websocket endpoint.

Parameters
resourceThe resource path.
handlerThe handler for incoming websocket requests.
Returns
Whether adding the endpoint was successful.

◆ handle_request()

template<bool isWebsocket = false, typename Derived, typename Connection>
void malloy::server::router::handle_request ( const std::filesystem::path & doc_root,
const req_generator< Derived > & req,
Connection && connection )
inline

Handle a request.

This function will either:

  • Forward to the appropriate sub-router
  • Handle the HTTP request
  • Handle the WS request

HTTP requests are first policy checked.

Template Parameters
isWebsocket
Connection
Parameters
doc_root
reqThe HTTP request to handle.
connectionThe HTTP or WS connection.
Here is the call graph for this function:

◆ operator=() [1/2]

router & malloy::server::router::operator= ( const router & rhs)
delete

Copy assignment operator.

Parameters
rhsThe right-hand-side object to copy-assign from.
Returns
A reference to the assignee.
Here is the call graph for this function:

◆ operator=() [2/2]

router & malloy::server::router::operator= ( router && rhs)
defaultnoexcept

Move assignment operator.

Parameters
rhsThe right-hand-side object to move-assign from.
Returns
A reference to the assignee.
Here is the call graph for this function:

◆ set_logger()

void router::set_logger ( std::shared_ptr< spdlog::logger > logger)

Set the logger to use.

Parameters
loggerThe logger to use.

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