-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.h
127 lines (103 loc) · 2.42 KB
/
router.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
#pragma once
#include <deque>
#include <functional>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>
#include "r3.h"
namespace graft {
class Router
{
public:
// enum class Result { Ok, Failure };
using vars_t = std::vector<std::pair<std::string, std::string>>;
// using Handler = std::function<bool (vars_t&, std::array<char,100>& , std::array<char,100>& ) >;
using Handler = std::function<bool (vars_t&, const std::string& , std::string& ) >;
// typedef std::function<Result (/*const Request&, Http::ResponseWriter*/)> Handler;
/*
Router(std::string root)
: m_root(std::move(root))
{
if (!m_node)
m_node = r3_tree_create(10);
}
*/
Router()
{
if (!m_node)
m_node = r3_tree_create(10);
}
~Router()
{
if (m_node)
{
r3_tree_free(m_node);
m_node = NULL;
}
}
void addRoute(std::string endpoint, int methods, Handler* handler)
{
// m_routes.push_back({m_root + std::move(endpoint), methods, std::move(handler)});
m_routes.push_back({endpoint, methods, handler});
}
bool arm()
{
std::for_each(m_routes.begin(), m_routes.end(),
[this](Route& r)
{
r3_tree_insert_route(
m_node,
r.methods,
r.endpoint.c_str(),
reinterpret_cast<void*>(r.handler)
);
});
char *errstr = NULL;
int err = r3_tree_compile(m_node, &errstr);
if (err != 0)
std::cout << "error: " << std::string(errstr) << std::endl;
else
r3_tree_dump(m_node, 0);
return (err == 0);
}
struct JobParams
{
std::string input;
vars_t vars;
Handler handler;
};
bool match(const std::string& target, int method, JobParams& params) const
{
match_entry *entry;
R3Route *m;
bool ret = false;
entry = match_entry_create(target.c_str());
entry->request_method = method;
m = r3_tree_match_route(m_node, entry);
if (m)
{
for (size_t i = 0; i < entry->vars.tokens.size; i++)
params.vars.emplace_back(std::make_pair(
std::move(std::string(entry->vars.slugs.entries[i].base, entry->vars.slugs.entries[i].len)),
std::move(std::string(entry->vars.tokens.entries[i].base, entry->vars.tokens.entries[i].len))
));
params.handler = *reinterpret_cast<Handler*>(m->data);
ret = true;
}
match_entry_free(entry);
return ret;
}
private:
std::string m_root;
struct Route
{
std::string endpoint;
int methods;
Handler* handler;
};
std::deque<Route> m_routes;
static R3Node *m_node;
};
}//namespace graft