RESTinio

sample/hello_world_basic/main.cpp
#include <iostream>
#include <restinio/all.hpp>
// Create request handler.
restinio::request_handling_status_t handler( const restinio::request_handle_t& req )
{
if( restinio::http_method_get() == req->header().method() &&
req->header().request_target() == "/" )
{
req->create_response()
.append_header( restinio::http_field::server, "RESTinio hello world server" )
.append_header_date_field()
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( fmt::format( "{}: Hello world!", req->remote_endpoint() ) )
.done();
return restinio::request_accepted();
}
return restinio::request_rejected();
}
int main()
{
try
{
restinio::run(
restinio::on_thread_pool( std::thread::hardware_concurrency() )
.port( 8080 )
.address( "localhost" )
.request_handler( handler ) );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
all.hpp
restinio::on_thread_pool
run_on_thread_pool_settings_t< Traits > on_thread_pool(std::size_t pool_size)
A special marker for the case when http_server must be run on an thread pool.
Definition: http_server_run.hpp:183
restinio::request_rejected
RESTINIO_NODISCARD constexpr request_handling_status_t request_rejected() noexcept
Definition: common_types.hpp:53
restinio::run
void run(asio_ns::io_context &ioctx, run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
Definition: http_server_run.hpp:216
restinio::request_handling_status_t
request_handling_status_t
Request handling status.
Definition: common_types.hpp:26
restinio::request_accepted
RESTINIO_NODISCARD constexpr request_handling_status_t request_accepted() noexcept
Definition: common_types.hpp:46
restinio::request_handle_t
std::shared_ptr< request_t > request_handle_t
An alias for handle for incoming request without additional extra-data.
Definition: request_handler.hpp:463