-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
65 lines (52 loc) · 1.53 KB
/
server.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
#include <cpprest/json.h>
#include <cpprest/http_listener.h>
#include <cpprest/uri.h>
#include <cpprest/asyncrt_utils.h>
#include <cpprest/json.h>
#include <cpprest/filestream.h>
#include <cpprest/containerstream.h>
#include <cpprest/producerconsumerstream.h>
#include <pplx/threadpool.h>
#include <iostream>
using namespace std;
void handleMessage(string m){
cout << "got now "<< m;
}
class handler {
public:
handler(web::http::uri uri) : m_listener{std::move(uri)} {
m_listener.support(
web::http::methods::GET, [](web::http::http_request message) {
cout<<message.to_string();
handleMessage(message.extract_string());
message.reply(web::http::status_codes::OK, "ls -ltr\n")
.then([](pplx::task<void> t) {
try {
t.get();
} catch (...) {
//
}
});
});
}
pplx::task<void> open() { return m_listener.open(); }
pplx::task<void> close() { return m_listener.close(); }
private:
web::http::experimental::listener::http_listener m_listener;
};
int main(int argc, char *argv[]) {
utility::string_t port = "8080";
if (argc == 2) {
port = argv[1];
}
utility::string_t address{"http://localhost:"};
address.append(port);
handler h(address);
h.open().wait();
std::cout << "Listening for requests at: " << address << std::endl;
std::cout << "Press ENTER to exit." << std::endl;
std::string line;
std::getline(std::cin, line);
h.close().wait();
return 0;
}