-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_router.h
More file actions
153 lines (129 loc) · 4.07 KB
/
api_router.h
File metadata and controls
153 lines (129 loc) · 4.07 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include <functional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
#include <httplib.h>
/**
* @brief Minimal routing layer on top of `httplib::Server`.
*
* Supports:
* - Registering handlers per HTTP method + path
* - Grouping routes under a prefix
* - Middleware executed before and after the route handler
* - Custom 404 handler and a global exception handler
*
* `bind()` wires all registered routes into an `httplib::Server`.
*/
class ApiRouter {
public:
/**
* @brief Endpoint handler.
*
* Receives the incoming request and a mutable response to fill.
*/
using Handler = std::function<void(const httplib::Request&, httplib::Response&)>;
/**
* @brief Continuation used by middleware to proceed to the next step.
*/
using Next = std::function<void()>;
/**
* @brief Middleware invoked with (req, res, next).
*
* Middleware is responsible for calling `next()` to continue the chain.
* If `next()` is not called, the request handling stops at that point.
*/
using Middleware = std::function<void(const httplib::Request&, httplib::Response&, Next)>;
/**
* @brief Register a GET route.
* @throws std::logic_error if the route is duplicated.
*/
void get(const std::string& path, Handler h);
/**
* @brief Register a POST route.
* @throws std::logic_error if the route is duplicated.
*/
void post(const std::string& path, Handler h);
/**
* @brief Register a PUT route.
* @throws std::logic_error if the route is duplicated.
*/
void put(const std::string& path, Handler h);
/**
* @brief Register a DELETE route.
* @throws std::logic_error if the route is duplicated.
*/
void del(const std::string& path, Handler h);
/**
* @brief Add middleware executed before the route handler.
*/
void use(Middleware m);
/**
* @brief Add middleware executed after the route handler.
*
* Note: "after" middleware runs only after the endpoint handler has completed.
*/
void use_after(Middleware m);
/**
* @brief Define a group of routes under a prefix.
*
* The prefix is temporarily applied for the duration of `define_routes`,
* then restored to the previous value.
*/
void group(std::string prefix, const std::function<void(ApiRouter&)>& define_routes);
/**
* @brief Set custom handler for HTTP 404.
*
* If not set, a plain text "Not Found" is returned.
*/
void set_not_found(Handler h);
/**
* @brief Set global exception handler for exceptions thrown by middleware/handlers.
*
* If not set, a plain text "Internal Server Error" (500) is returned.
*/
void set_exception_handler(
std::function<void(const httplib::Request&, httplib::Response&, const std::exception&)> h);
/**
* @brief Bind all registered routes and error handling into an `httplib::Server`.
*/
void bind(httplib::Server& server) const;
private:
/// @brief Internal enumeration mapping to HTTP methods.
enum class Method {
Get,
Post,
Put,
Del,
};
/**
* @brief Join prefix and path handling slashes.
*
* Ensures there is exactly one '/' between non-empty segments.
*/
static std::string join_path(std::string_view prefix, std::string_view path);
/// @brief Convert method enum to HTTP method string (e.g., "GET").
static std::string to_string(Method m);
/**
* @brief Add a route under the current prefix.
* @throws std::logic_error if the route is duplicated.
*/
void add_route(Method method, const std::string& path, Handler h);
/**
* @brief Wrap an endpoint with before/after middleware and exception translation.
*/
Handler compose(Handler endpoint) const;
std::unordered_map<std::string, Handler> get_;
std::unordered_map<std::string, Handler> post_;
std::unordered_map<std::string, Handler> put_;
std::unordered_map<std::string, Handler> del_;
std::vector<Middleware> before_;
std::vector<Middleware> after_;
/// @brief Current route group prefix (empty means no prefix).
std::string prefix_;
Handler not_found_;
std::function<void(const httplib::Request&, httplib::Response&, const std::exception&)> exception_handler_;
};