MCPcopy Create free account
hub / github.com/Stiffstream/restinio / server_handler

Function server_handler

dev/sample/express_router_tutorial/main.cpp:22–127  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20}
21
22auto server_handler()
23{
24 auto router = std::make_unique< router_t >();
25
26 // GET request to homepage.
27 router->http_get( "/", []( auto req, auto ){
28 return
29 init_resp( req->create_response() )
30 .set_body( "GET request to the homepage.")
31 .done();
32 } );
33
34 // POST request to homepage.
35 router->http_post( "/", []( auto req, auto ){
36 return
37 init_resp( req->create_response() )
38 .set_body( "POST request to the homepage.\nbody: " + req->body() )
39 .done();
40 } );
41
42 // GET request with single parameter.
43 router->http_get( "/single/:param", []( auto req, auto params ){
44 return
45 init_resp( req->create_response() )
46 .set_body(
47 fmt::format(
48 RESTINIO_FMT_FORMAT_STRING(
49 "GET request with single parameter: '{}'" ),
50 restinio::fmtlib_tools::streamed( params[ "param" ] ) ) )
51 .done();
52 } );
53
54 // POST request with several parameters.
55 router->http_post( R"(/many/:year(\d{4}).:month(\d{2}).:day(\d{2}))",
56 []( auto req, auto params ){
57 return
58 init_resp( req->create_response() )
59 .set_body(
60 fmt::format(
61 RESTINIO_FMT_FORMAT_STRING(
62 "POST request with many parameters:\n"
63 "year: {}\nmonth: {}\nday: {}\nbody: {}" ),
64 restinio::fmtlib_tools::streamed( params[ "year" ] ),
65 restinio::fmtlib_tools::streamed( params[ "month" ] ),
66 restinio::fmtlib_tools::streamed( params[ "day" ] ),
67 req->body() ) )
68 .done();
69 } );
70
71 // GET request with indexed parameters.
72 router->http_get( R"(/indexed/([a-z]+)-(\d+)/(one|two|three))",
73 []( auto req, auto params ){
74 return
75 init_resp( req->create_response() )
76 .set_body(
77 fmt::format(
78 RESTINIO_FMT_FORMAT_STRING(
79 "GET request with indexed parameters:\n"

Callers 1

mainFunction · 0.70

Calls 12

streamedFunction · 0.85
parse_queryFunction · 0.85
status_not_foundFunction · 0.85
create_responseMethod · 0.80
queryMethod · 0.80
init_respFunction · 0.70
http_getMethod · 0.45
doneMethod · 0.45
http_postMethod · 0.45
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected