-
Notifications
You must be signed in to change notification settings - Fork 2
/
HttpController.h
146 lines (103 loc) · 3.52 KB
/
HttpController.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Created by romanov on 7/5/17.
//
#ifndef EVESTANDALONE_SERVERCONTROLLER_H
#define EVESTANDALONE_SERVERCONTROLLER_H
#include <httplib.h>
#include <cstdio>
#include <utility>
#include <utility>
namespace hdvis{
class HttpController {
public:
typedef std::function<std::string (const httplib::Request&)> Handler;
~HttpController()
{
_server.stop();
}
void AddApiGetRoute(const char* pattern, HttpController::Handler handler)
{
_server.get(pattern, [handler](const httplib::Request &req, httplib::Response &res) {
string jsonResult = handler(req);
res.set_content(jsonResult, "application/json");
});
}
std::string DumpHeaders(const httplib::MultiMap &headers)
{
std::string s;
char buf[BUFSIZ];
for (auto it = headers.begin(); it != headers.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
s += buf;
}
return s;
}
std::string Log(const httplib::Request &req, const httplib::Response &res)
{
std::string s;
char buf[BUFSIZ];
s += "================================\n";
snprintf(buf, sizeof(buf), "%s %s", req.method.c_str(), req.path.c_str());
s += buf;
std::string query;
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%c%s=%s",
(it == req.params.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str());
query += buf;
}
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
s += buf;
// s += DumpHeaders(req.headers);
s += "--------------------------------\n";
snprintf(buf, sizeof(buf), "%d\n", res.status);
s += buf;
// s += DumpHeaders(res.headers);
if (!res.body.empty()) {
s += res.body;
}
s += "\n";
return s;
}
void StartListening()
{
_runThread = std::thread([&](){this->StartServer();});
}
protected:
void StartServer() {
using namespace httplib;
_server.set_base_dir(baseDir.c_str());
_server.get("/", [=](const Request &req, Response &res) {
res.set_redirect("/event.html");
});
_server.get("/hi", [](const Request &req, Response &res) {
std::cout<<"I saw /hi"<<endl;
res.set_content("Hello World!", "text/plain");
});
_server.get("/dump", [&](const Request &req, Response &res) {
res.set_content(DumpHeaders(req.headers), "text/plain");
});
_server.get("/stop", [&](const Request &req, Response &res) {
_server.stop();
});
_server.set_error_handler([](const Request &req, Response &res) {
const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), fmt, res.status);
res.set_content(buf, "text/html");
});
/*_server.set_logger([&](const Request &req, const Response &res) {
printf("%s", Log(req, res).c_str());
});*/
cout << "The server started at port " << port << "..." << endl;
_server.listen("localhost", 5000);
}
private:
std::thread _runThread;
httplib::Server _server;
int port = 5000;
std::string baseDir = "./www";
};
}
#endif //EVESTANDALONE_SERVERCONTROLLER_H