forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsf_http_router.h
More file actions
125 lines (109 loc) · 5.08 KB
/
Copy pathsf_http_router.h
File metadata and controls
125 lines (109 loc) · 5.08 KB
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
/**
* @version 1.0.0
* @author skyfire
* @mail skyfireitdiy@hotmail.com
* @see http://github.com/skyfireitdiy/sflib
* @file sf_http_router.h
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
#pragma once
#include <map>
#include <vector>
#include <regex>
#include <mutex>
#include "sf_router.hpp"
#include "sf_http_response.hpp"
#include "sf_http_request.hpp"
namespace skyfire
{
/**
* @brief http路由
*/
class sf_http_router : public sf_router
{
private:
std::function<bool(const sf_http_request &,sf_http_response&,const std::string &)> route_callback__;
const int priority__;
const std::vector<std::string> methods__;
std::recursive_mutex methods_mu__;
template <typename FuncType, int N, typename ... Args>
typename std::enable_if<sizeof...(Args) == N, void>::type
callback_call_helper__(const sf_http_request &req,sf_http_response& res,FuncType func,const std::smatch &sm, Args ... args);
template <typename FuncType, int N, typename ... Args>
typename std::enable_if<sizeof...(Args) != N, void>::type
callback_call_helper__(const sf_http_request &req,sf_http_response& res,FuncType func,const std::smatch &sm, Args ... args);;
public:
/**
* 构造函数
* @tparam StringType 匹配到的字符串
* @param pattern 用于匹配url的正则表达式
* @param callback 回调函数,接收N个string类型参数,分别对应pattern匹配到的字符串
* @param methods 请求类型集合,"GET","POST","PUT","DELETE"等组合,"*"表示所有请求类型
* @param priority 路由优先级
*/
template <typename ... StringType>
sf_http_router(const std::string& pattern,void(*callback)(const sf_http_request &,sf_http_response&,StringType...),
const std::vector<std::string> &methods = {{"*"}} ,int priority = 0);
/**
* 构造函数
* @tparam StringType 匹配到的字符串
* @param pattern 用于匹配url的正则表达式
* @param callback 回调函数,接收N个string类型参数,分别对应pattern匹配到的字符串
* @param methods 请求类型集合,"GET","POST","PUT","DELETE"等组合,"*"表示所有请求类型
* @param priority 路由优先级
*/
template <typename ... StringType>
sf_http_router(const std::string& pattern,std::function<void(const sf_http_request &,sf_http_response&,StringType...)> callback,
const std::vector<std::string> &methods = {{"*"}} ,int priority = 0);
/**
* 运行路由(由框架调用)
* @param req 请求
* @param res 响应
* @param url url
* @param method 请求方式
* @return 是否已处理
*/
bool run_route(const sf_http_request &req, sf_http_response &res,const std::string &url, const std::string &method);
/**
* 重载小于运算符(主要用于排序)
* @param router 其他router
* @return 是否小于
*/
bool operator<(const sf_http_router& router) const;
/**
* 获取优先级
* @return 优先级
*/
int get_priority() const override;
};
/**
* 生成http路由
* @tparam StringType 匹配到的字符串
* @param pattern 用于匹配url的正则表达式
* @param callback 回调函数,接收N个string类型参数,分别对应pattern匹配到的字符串
* @param methods 请求类型集合,"GET","POST","PUT","DELETE"等组合,"*"表示所有请求类型
* @param priority 路由优先级
*/
template<typename ...StringType>
std::shared_ptr<sf_http_router> make_http_router(const std::string &pattern,
void(*callback)(const sf_http_request &, sf_http_response &,
StringType...),
const std::vector<std::string> &methods = {{"*"}},
int priority = 0);
/**
* 生成http路由
* @tparam StringType 匹配到的字符串
* @param pattern 用于匹配url的正则表达式
* @param callback 回调函数,接收N个string类型参数,分别对应pattern匹配到的字符串
* @param methods 请求类型集合,"GET","POST","PUT","DELETE"等组合,"*"表示所有请求类型
* @param priority 路由优先级
*/
template<typename ...StringType>
std::shared_ptr<sf_http_router> make_http_router(const std::string &pattern,
std::function<void(const sf_http_request &, sf_http_response &,
StringType...)> callback,
const std::vector<std::string> &methods = {{"*"}},
int priority = 0);
}