Skip to content

Commit 2a64ba6

Browse files
author
Sebastiano Merlino
committed
Added hello_world example
1 parent 15c74a9 commit 2a64ba6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

examples/hello_world.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <httpserver.hpp>
2+
3+
using namespace httpserver;
4+
5+
class hello_world_resource : public http_resource<hello_world_resource> {
6+
public:
7+
void render(const http_request&, http_response**);
8+
};
9+
10+
//using the render method you are able to catch each type of request you receive
11+
void hello_world_resource::render(const http_request& req, http_response** res)
12+
{
13+
//it is possible to send a response initializing an http_string_response
14+
//that reads the content to send in response from a string.
15+
*res = new http_string_response("Hello World!!!", 200);
16+
}
17+
18+
int main()
19+
{
20+
//it is possible to create a webserver passing a great number of parameters.
21+
//In this case we are just passing the port and the number of thread running.
22+
webserver ws = create_webserver(8080).max_threads(5);
23+
24+
hello_world_resource hwr;
25+
//this way we are registering the hello_world_resource to answer for the endpoint
26+
//"/hello". The requested method is called (if the request is a GET we call the render_GET
27+
//method. In case that the specific render method is not implemented, the generic "render"
28+
//method is called.
29+
ws.register_resource("/hello", &hwr, true);
30+
31+
//This way we are putting the created webserver in listen. We pass true in order to have
32+
//a blocking call; if we want the call to be non-blocking we can just pass false to the
33+
//method.
34+
ws.start(true);
35+
return 0;
36+
}

0 commit comments

Comments
 (0)