1#ifndef MSTL_NETWORK_HTTP_ROUTER_HPP__
2#define MSTL_NETWORK_HTTP_ROUTER_HPP__
4#include "http_filter.hpp"
7class MSTL_API http_router {
9 using handler_func = function<void(http_request&, http_response&)>;
12 unordered_map<string, unordered_map<string, handler_func>> routes_;
13 http_filter_chain middleware_chain_;
15 handler_func not_found_handler_;
16 handler_func method_not_allowed_handler_;
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);
24 ~http_router() =
default;
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);
38 void route(
const HTTP_METHOD& method,
const string& path, handler_func handler);
40 void use(http_filter* middleware) { middleware_chain_.add_filter(middleware); }
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); }
45 http_response handle_request(http_request& request);
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)
移动范围元素