-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
97 lines (83 loc) · 2.46 KB
/
example.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <memory>
#include <string>
#include <system_error>
#include <functional>
#include <experimental/net>
#include <html.hpp>
namespace net = std::experimental::net;
using tcp = net::ip::tcp;
class server
{
using handler_t = std::function<void(tcp::iostream&&)>;
public:
server(int port) : acceptor_(ctx_)
{
tcp::endpoint endpoint(tcp::v4(), port);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
accept();
}
void run(handler_t handler)
{
handler_ = handler;
ctx_.run();
}
private:
void accept()
{
acceptor_.async_accept(
ctx_,
[this](std::error_code ec, tcp::socket socket)
{
tcp::iostream stream(std::move(socket));
/* ignore request */
bool status = true;
std::string line;
do {
std::getline(stream, line, '\n');
line.erase(line.size() - 1);
if (status)
{
std::cout << line << std::endl;
status = false;
}
} while (!line.empty());
/* send basic status / headers */
stream << "HTTP/1.1 200 Ok\r\n"
<< "Content-type: text/html\r\n"
<< "\r\n";
/* invoke handler */
handler_(std::move(stream));
accept();
}
);
}
net::io_context ctx_;
tcp::acceptor acceptor_;
handler_t handler_;
};
int
main(int argc, char **argv)
{
int port = argc > 1 ? std::atoi(argv[1]) : 8080;
server server(port);
std::cout << "starting server at "
"http://localhost:" << port << "/" << std::endl;
server.run([](auto&& stream) {
stream << html::indent;
stream << html::html
<< html::head
<< html::script[html::language("javascript")]
<< "function sayHello() {" << std::endl
<< " document.getElementById('message').innerHTML = 'Hello, world';" << std::endl
<< "}" << std::endl
<< html::head::end
<< html::body[html::attr("onload", "sayHello()")]
<< html::div[html::id("message")]
<< html::end;
});
return 0;
}