File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments