-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
92 lines (82 loc) · 3.2 KB
/
test.cpp
File metadata and controls
92 lines (82 loc) · 3.2 KB
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
#include "http/server.hpp"
#include "http/room.hpp"
int main() {
http::server server{ 8080, 8 }; //8 threads
server.get("/hello", [ ](const http::request&){
http::response res;
res.status = http::status_type::ok;
res.content.emplace<std::string>("<h1>Hello world!</h1>");
return res;
});
server.get("/.*\\.html", [ ](const http::request& req) -> http::task<http::response> {
http::response res;
res.status = http::status_type::ok;
res.headers.insert({ "Content-Type", "text/html" });
res.content.emplace<std::filesystem::path>(std::string{ R"(../public)" } + std::string{req.url});
co_return res;
});
server.get("/.*\\.jpeg", [ ](const http::request& req){
http::response res;
res.status = http::status_type::ok;
res.headers.insert({ "Content-Type", "image/jpeg" });
res.content.emplace<std::filesystem::path>(std::string{ R"(../public)" } + std::string{req.url});
return res;
});
server.get("/.*\\.mp4", [ ](const http::request& req) -> http::task<http::response> {
http::response res;
res.status = http::status_type::ok;
res.headers.insert({ "Content-Type", "video/mp4" });
res.content.emplace<std::filesystem::path>(std::string{ R"(../public)" } + std::string{req.url});
co_return res;
});
//Simulate CPU-intensive request. For exemple, /fib28.
server.get("/fib.*", [ ](const http::request& req){
http::response res;
struct cal {
unsigned long long fib(int x) {
if (x <= 0 or x > 94 or x == 1 or x == 2)
return 1;
else return fib(x - 1) + fib(x - 2);
}
};
cal c;
res.status = http::status_type::ok;
res.headers.insert({ "Content-Type", "text/plain" });
try {
int x = std::stoi(std::string{ req.url.begin() + 4, req.url.end() });
res.content.emplace<std::string>(std::to_string(c.fib(x)));
}
catch (...) {
res.content.emplace<std::string>("Invalid argument.");
}
return res;
});
server.websocket("/echo", [ ](http::ws_stream stream)->http::task<void> {
while (true) {
std::string msg;
boost::asio::dynamic_string_buffer buffer{ msg, 1 << 16 };
auto ret = co_await stream.async_read(buffer);
if (std::get<0>(ret))
co_return;
stream.send(std::make_shared<std::string>(std::move(msg)));
}
});
//subscribe-publish
server.websocket("/chat-room", [ ](http::ws_stream stream)->http::task<void> {
stream.join("chat"); //join in a room named "chat"
while (true) {
auto msg = std::make_shared<std::string>();
msg->reserve(128);
boost::asio::dynamic_string_buffer buffer{ *msg, 1 << 16 };
auto ret = co_await stream.async_read(buffer);
if(std::get<0>(ret))
co_return; //automatic call stream.leave() to leave room
/*
* deal with message
*/
stream.room().publish(msg);
}
});
server.listen();
return 0;
}