matador::http::server Class Reference

A simple reactor based HTTP server. More...

#include <http_server.hpp>

Public Member Functions

 server (unsigned short port)
 
 server (unsigned short port, const std::string &dir)
 
void run ()
 
void shutdown ()
 
bool is_running () const
 
template<class RequestHandler >
void on_get (const std::string &route, RequestHandler request_handler)
 
template<class RequestHandler >
void on_post (const std::string &route, RequestHandler request_handler)
 
template<class RequestHandler >
void on_put (const std::string &route, RequestHandler request_handler)
 
template<class RequestHandler >
void on_remove (const std::string &route, RequestHandler request_handler)
 
void add_middleware (const std::shared_ptr< middleware > &mware)
 
void add_routing_middleware ()
 

Detailed Description

A simple reactor based HTTP server.

This class implements a single threaded HTTP server based on the reactor implementation.

It provides four HTTP methods for which a route (path) and callback pair can be registered.

The callback takes a http::request object as input parameter and returns a http::response object.

To use the automatic routing of incoming requests by path a routing middleware can be added via calling add_routing_middle_ware().

Constructor & Destructor Documentation

◆ server() [1/2]

matador::http::server::server ( unsigned short  port)
explicit

Creates a HTTP server at the given port.

Parameters
portPort on which the server listens for connections.

◆ server() [2/2]

matador::http::server::server ( unsigned short  port,
const std::string &  dir 
)

Creates a HTTP server at the given port. The given directory path is used as root directory when serving files.

Parameters
portPort on which the server listens for connections.
dirDirectory to serve files from

Member Function Documentation

◆ add_middleware()

void matador::http::server::add_middleware ( const std::shared_ptr< middleware > &  mware)

Adds the given middleware to the middleware pipeline.

Note: The last added middleware will be executed at first.

Parameters
mwareMiddleware to add

◆ add_routing_middleware()

void matador::http::server::add_routing_middleware ( )

Adds the routing middleware to the middleware pipeline. It handles that the correct callback is executed depending on the route and the method of the incoming request.

◆ is_running()

bool matador::http::server::is_running ( ) const

Returns true if the server is running

Returns
True if the server is running

◆ on_get()

template<class RequestHandler >
void matador::http::server::on_get ( const std::string &  route,
RequestHandler  request_handler 
)
inline

Registers a HTTP GET method callback for the given route.

Template Parameters
RequestHandlerType of the callback
Parameters
routeThe route for the callback
request_handlerThe GET callback to be executed

◆ on_post()

template<class RequestHandler >
void matador::http::server::on_post ( const std::string &  route,
RequestHandler  request_handler 
)
inline

Registers a HTTP POST method callback for the given route.

Template Parameters
RequestHandlerType of the callback
Parameters
routeThe route for the callback
request_handlerThe POST callback to be executed

◆ on_put()

template<class RequestHandler >
void matador::http::server::on_put ( const std::string &  route,
RequestHandler  request_handler 
)
inline

Registers a HTTP PUT method callback for the given route.

Template Parameters
RequestHandlerType of the callback
Parameters
routeThe route for the callback
request_handlerThe PUT callback to be executed

◆ on_remove()

template<class RequestHandler >
void matador::http::server::on_remove ( const std::string &  route,
RequestHandler  request_handler 
)
inline

Registers a HTTP DELETE method callback for the given route.

Template Parameters
RequestHandlerType of the callback
Parameters
routeThe route for the callback
request_handlerThe DELETE callback to be executed

◆ run()

void matador::http::server::run ( )

Starts the server

◆ shutdown()

void matador::http::server::shutdown ( )

Stops the server


The documentation for this class was generated from the following file:
  • matador/http/http_server.hpp