-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.cpp
71 lines (61 loc) · 2.02 KB
/
rest.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
#include "rest.hpp"
#include "meta/root.hpp"
#include "rest_handlers.hpp"
#include <utils/string/string.hpp>
//#include <iostream>
//#include <boost/type_index.hpp>
namespace rest{
auto handler = root(std::string,
"api",
prefix("datetime",
method("log", &time::extract_log),
method("setup", &performance::setup_datetime)),
prefix("compute",
method("accumulate", &performance::extract ),
method("sha1", &hash::compute_sha1)),
prefix("persistent",
method("setup", &persistent::setup ),
method("extract", &persistent::extract)));
/*\brief request callback
*
* contains all information for request
*/
struct request_instance {
request_instance(request& req, response& resp)
: req_(req), resp_(resp), handle_(false) {
}
std::string const& request_source() { return req_.body_;}
void set_response(int code, std::string&& src) {
resp_.code_ = code;
resp_.body_ = std::move(src);
handle_ =true;
}
void set_text_response(int code, std::string&& src) {
resp_.code_ = code;
resp_.body_ = std::move(src);
resp_.mime_ = rest::mime_type::TEXT;
handle_ =true;
}
bool handled() {
return handle_;
}
request& req_;
response& resp_;
bool handle_;
};
//auto meth = method("log", &time::extract_log);
response execute(request&& req){
response resp;
resp.code_ = 404;
resp.mime_ = req.possible_response_mime_;
using iterator_t = typename std::vector<std::string>::iterator;
auto vec = utils::split(req.uri_, '/');
range<iterator_t> rng{vec.begin(), vec.end()};
// std::cerr <<
// boost::typeindex::type_id_with_cvr<decltype(handler)>().pretty_name() <<
// "\n";
request_instance inst(req, resp);
handler.walk(inst,rng);
return resp;
}
}