-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.cpp
43 lines (38 loc) · 1.05 KB
/
route.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
#include "includes/route.h"
route::route()
{
}
route::~route()
{
for (map< string, std::function<string()>*>::iterator itr = routes.begin(); itr != routes.end(); itr++)
{
delete itr->second;
}
routes.clear();
}
string route::trace(string path, map<string,string> &headers)
{
if (routes.count(path)>0)
return (*routes[path])();
else
{
// images, videos, files should be different
regex file_pattern(".*((.js)|(.css))$");
smatch result;
if (regex_match(path, result, file_pattern))
{
render newrender;
map<string,string> none;
headers["Content-Type"]="text/plain; charset=utf-8";
string tmp = newrender.render_from_template(string("../"+path), none);
if (!tmp.empty()) return tmp;
}
return error(string("404"), path);
}
}
void route::bind_path(string path, string(*dealer)())
{
std::function<string()> *t = new std::function<string()>;
*t = std::function<string()>(dealer);
routes[path] = t;
}