-
Notifications
You must be signed in to change notification settings - Fork 940
/
main.cc
52 lines (47 loc) · 1.59 KB
/
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
#include <evpp/evpphttp/service.h>
#include <iostream>
static int g_port = 29099;
void DefaultHandler(evpp::EventLoop* loop,
evpp::evpphttp::HttpRequest& ctx,
const evpp::evpphttp::HTTPSendResponseCallback& cb) {
std::stringstream oss;
oss << "func=" << __FUNCTION__ << " OK"
<< " ip=" << ctx.remote_ip << "\n"
<< " uri=" << ctx.url_path() << "\n"
<< " body=" << ctx.body.ToString() << "\n";
std::map<std::string, std::string> feild_value = {
{"Content-Type", "application/octet-stream"},
{"Server", "evpp"}
};
cb(200, feild_value, oss.str());
}
int main(int argc, char* argv[]) {
int thread_num = 2;
if (argc > 1) {
if (std::string("-h") == argv[1] ||
std::string("--h") == argv[1] ||
std::string("-help") == argv[1] ||
std::string("--help") == argv[1]) {
std::cout << "usage : " << argv[0] << " <listen_port> <thread_num>\n";
std::cout << " e.g. : " << argv[0] << " 8080 24\n";
return 0;
}
}
if (argc == 2) {
g_port = atoi(argv[1]);
} else if (argc == 3) {
g_port = atoi(argv[1]);
thread_num = atoi(argv[2]);
}
evpp::evpphttp::Service server(std::string("0.0.0.0:") + std::to_string(g_port), "test", thread_num);
server.RegisterHandler("/echo", &DefaultHandler);
if (!server.Start()) {
std::cout << "start serv failed\n";
return -1;
}
std::cout << "start serv on 0.0.0.0:" << g_port << " suc\n";
while (!server.IsStopped()) {
usleep(1);
}
return 0;
}