-
Notifications
You must be signed in to change notification settings - Fork 92
/
main.cpp
77 lines (65 loc) · 1.79 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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() )
{
fmt::basic_memory_buffer< char, 1u > response_body;
fmt::format_to( response_body, "GET request to '{}'\n",
req->header().request_target() );
// Request header fields.
fmt::format_to( response_body, "HTTP-fields ({}):\n",
req->header().fields_count() );
for( const auto & f : req->header() )
{
fmt::format_to( response_body, "{}: {}\n",
f.name(), f.value() );
}
// Query params.
const auto qp = restinio::parse_query( req->header().query() );
if( qp.empty() )
{
fmt::format_to( response_body, "No query parameters." );
}
else
{
fmt::format_to( response_body, "Query params ({}):\n", qp.size() );
for( const auto p : qp )
{
fmt::format_to( response_body, "'{}' => '{}'\n",
p.first, p.second );
}
}
if( qp.has( "debug" ) && qp[ "debug" ] == "true" )
{
std::cout.write( response_body.data(), response_body.size() );
std::cout << std::flush;
}
req->create_response()
.append_header( restinio::http_field::server, "RESTinio query string params server" )
.append_header_date_field()
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( std::move(response_body) )
.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;
}