#include <iostream>
#include <fmt/format.h>
#include <fmt/ostream.h>
template < typename RESP >
RESP
init_resp( RESP resp )
{
resp.append_header( restinio::http_field::server, "RESTinio sample server /v.0.6.10" );
resp.append_header_date_field();
return resp;
}
using router_t = rr::express_router_t<>;
auto server_handler(std::string prefix)
{
auto router = std::make_unique< router_t >();
router->http_get(
"/",
[prefix]( auto req, auto ){
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( prefix + ": Hello world!")
.done();
} );
router->http_get(
"/json",
[prefix]( auto req, auto ){
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "application/json" )
.set_body( fmt::format(
R"-({{"message" : "{}: Hello world!"}})-",
prefix ) )
.done();
} );
router->http_get(
"/html",
[prefix]( auto req, auto ){
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "text/html; charset=utf-8" )
.set_body(
fmt::format(
R"-(<html>
<head><title>Hello from RESTinio!</title></head>
<body>
<center><h1>{}: Hello world</h1></center>
</body>
</html>)-",
prefix ) )
.done();
} );
return router;
}
int main( int argc, const char * argv[] )
{
using namespace std::chrono;
std::string certs_dir = ".";
if( 2 == argc )
{
certs_dir = argv[ 1 ];
}
try
{
using traits_t =
router_t >;
namespace asio_ns = restinio::asio_ns;
auto tls_context = std::make_shared< asio_ns::ssl::context >(
asio_ns::ssl::context::sslv23 );
tls_context->set_options(
asio_ns::ssl::context::default_workarounds
| asio_ns::ssl::context::no_sslv2
| asio_ns::ssl::context::single_dh_use );
tls_context->use_certificate_chain_file( certs_dir + "/server.pem" );
tls_context->use_private_key_file(
certs_dir + "/key.pem",
asio_ns::ssl::context::pem );
tls_context->use_tmp_dh_file( certs_dir + "/dh2048.pem" );
auto first_server = restinio::run_async< traits_t >(
.port( 4443 )
.request_handler( server_handler( "First" ) )
.tls_context( tls_context ),
1u );
auto second_server = restinio::run_async< traits_t >(
.port( 5553 )
.request_handler( server_handler( "Second" ) )
.tls_context( tls_context ),
1u );
std::this_thread::sleep_for( std::chrono::minutes{1} );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
Timer factory implementation using asio timers.
Derived & address(std::string addr) &
A fluent style interface for setting http server params.
ostream_logger_t< null_mutex_t > single_threaded_ostream_logger_t
io_context_holder_t own_io_context()
Function which tells that http_server should create and use its own instance of io_context.
RESTINIO_NODISCARD constexpr request_handling_status_t request_accepted() noexcept