belle
/
example
/
server
/
hello
/
src
/
main.cc
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
// belle hello example
#include "belle.hh"
namespace Belle = OB::Belle;
#include <string>
#include <iostream>
int main(int argc, char *argv[])
{
// init the server
Belle::Server app;
// set the listening address
std::string address {"127.0.0.1"};
app.address(address);
// set the listening port
int port {8080};
app.port(port);
// enable serving static files from a public directory
// if the path is relative, make sure to run the program
// in the right working directory
app.public_dir("../public");
// handle route GET '/'
app.on_http("/", Belle::Method::get, [](Belle::Server::Http_Ctx& ctx)
{
// set http response headers
ctx.res.set(Belle::Header::content_type, "text/plain");
// set the http status code
ctx.res.result(Belle::Status::ok);
// set the http body
ctx.res.body() = "Hello, Belle!";
});
// handle route POST '/'
app.on_http("/", Belle::Method::post, [](Belle::Server::Http_Ctx& ctx)
{
// set http response headers
ctx.res.set(Belle::Header::content_type, "text/plain");
// set the http status code
ctx.res.result(Belle::Status::ok);
// echo back the request body
ctx.res.body() = ctx.req.body();
});
// print out the address and port
// along with the routes
std::cout
<< "Server: " << address << ":" << port << "\n\n"
<< "Try out the following urls:\n"
<< " http://" << address << ":" << port << "/\n"
<< " http://" << address << ":" << port << "/index.html\n\n"
<< "Try running the following command:\n"
<< " curl -X POST --data 'post body message here' http://"
<< address << ":" << port << "/\n\n";
// start the server
app.listen();
// the server blocks until a signal is received
return 0;
}