-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrouter.hpp
56 lines (41 loc) · 1.53 KB
/
router.hpp
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
#pragma once
#include <functional>
#include "http.hpp"
#include "server.hpp"
class Router {
public:
using RouteParams = std::unordered_map<std::string_view, std::string_view>;
void route(std::string_view pattern,
std::function<void(const Request&, const RouteParams&, std::unique_ptr<Responder>)>
handler);
void route(std::string_view pattern,
std::function<Response(const Request&, const RouteParams&)> handler);
void route(Method method, std::string_view pattern,
std::function<void(const Request&, const RouteParams&, std::unique_ptr<Responder>)>
handler);
void route(Method method, std::string_view pattern,
std::function<Response(const Request&, const RouteParams&)> handler);
void operator()(const Request& request, std::unique_ptr<Responder>) const;
private:
struct Route {
struct Pattern {
struct Part {
enum class Type {
Literal,
Placeholder,
PlaceholderPath,
};
Type type;
std::string_view str;
};
std::string pattern;
std::vector<Part> parts;
static Pattern parse(std::string_view str);
std::optional<RouteParams> match(std::string_view urlPath) const;
};
Pattern pattern;
Method method;
std::function<void(const Request&, const RouteParams&, std::unique_ptr<Responder>)> handler;
};
std::vector<Route> routes_;
};