-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
85 lines (72 loc) · 1.86 KB
/
main.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
#include <www-native/test.hpp>
#include <beast_http.hpp>
struct controller
{
handler
<
void,
get,
path<"cats/:id">
>
foo(param<int, "id"> id)
{
std::cout << "foo::id " << id << std::endl;
return {};
}
};
template <typename T, typename L, typename U, typename...Args, typename Body = http::string_body>
void bar(L listener, U(T::* f)(Args...))
{
listener->handler =
[]
(http::request<Body> req,
std::function<void(http::response<Body>)> send)
{
std::cout << "handler for type " << typeid(U(Args...)).name() << std::endl;
parse_path<U> (req);
};
}
int main(int argc, const char** argv)
{
// Check command line arguments.
if (argc == 2 && !strcmp(argv[1], "-h"))
{
std::cerr <<
"Usage: http-server-async <address> <port> <doc_root> <threads>\n" <<
"Example:\n" <<
" http-server-async [0.0.0.0] [8080]\n";
return EXIT_FAILURE;
}
auto address = net::ip::make_address("0.0.0.0");
if (argc >= 2)
address = net::ip::make_address(argv[1]);
unsigned short port = 8080;
if (argc >= 3)
port = static_cast<unsigned short>(std::atoi(argv[2]));
//auto const doc_root = std::make_shared<std::string>(argv[3]);
//auto const threads = std::max<int>(1, std::atoi(argv[4]));
auto const threads = 1;
// The io_context is required for all I/O
net::io_context ioc{threads};
// Create and launch a listening port
auto const listener = std::make_shared<struct listener>(
ioc,
tcp::endpoint{address, port}
/*, doc_root*/);
listener->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ioc]
{
ioc.run();
});
bar<controller>
(
listener
, &controller::foo
);
ioc.run();
}