MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_router.hpp
1#ifndef MSTL_NETWORK_HTTP_ROUTER_HPP__
2#define MSTL_NETWORK_HTTP_ROUTER_HPP__
4#include "http_filter.hpp"
6
7class MSTL_API http_router {
8public:
9 using handler_func = function<void(http_request&, http_response&)>;
10
11private:
12 unordered_map<string, unordered_map<string, handler_func>> routes_;
13 http_filter_chain middleware_chain_;
14
15 handler_func not_found_handler_;
16 handler_func method_not_allowed_handler_;
17
18 handler_func* find_handler(const HTTP_METHOD& method, const string& path);
19 void setup_default_handlers();
20 static vector<string> split_methods(const string& method_str);
21
22public:
23 http_router();
24 ~http_router() = default;
25
26 void get(const string& path, handler_func handler);
27 void post(const string& path, handler_func handler);
28 void put(const string& path, handler_func handler);
29 void del(const string& path, handler_func handler);
30 void head(const string& path, handler_func handler);
31 void options(const string& path, handler_func handler);
32 void trace(const string& path, handler_func handler);
33 void connect(const string& path, handler_func handler);
34 void get_post(const string& path, handler_func handler);
35 void post_delete(const string& path, handler_func handler);
36 void all(const string& path, handler_func handler);
37
38 void route(const HTTP_METHOD& method, const string& path, handler_func handler);
39
40 void use(http_filter* middleware) { middleware_chain_.add_filter(middleware); }
41
42 void set_not_found_handler(handler_func handler) { not_found_handler_ = _MSTL move(handler); }
43 void set_method_not_allowed_handler(handler_func handler) { method_not_allowed_handler_ = _MSTL move(handler); }
44
45 http_response handle_request(http_request& request);
46};
47
49#endif // MSTL_NETWORK_HTTP_ROUTER_HPP__
MSTL通用函数包装器
MSTL_ALWAYS_INLINE enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素