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
|
#pragma once
#include "http_handler.h"
namespace NKikimr {
namespace NViewer {
struct THandlerWithParams {
THandlerWithParams(TJsonHandlerBase::TPtr handler, std::map<TString, TString> pathParams)
: Handler(std::move(handler))
, PathParams(std::move(pathParams))
{
}
THandlerWithParams() = default;
THandlerWithParams(THandlerWithParams&&) = default;
THandlerWithParams& operator=(const THandlerWithParams&) = default;
THandlerWithParams& operator=(const THandlerWithParams&&) = default;
TJsonHandlerBase::TPtr Handler;
std::map<TString, TString> PathParams;
};
class THttpRequestRouter {
public:
void RegisterHandler(HTTP_METHOD method, const TString& pathPattern, TJsonHandlerBase::TPtr handler);
void RegisterGetHandler(const TString& pathPattern, TJsonHandlerBase::TPtr handler) {
RegisterHandler(HTTP_METHOD_GET, pathPattern, handler);
}
std::optional<THandlerWithParams> ResolveHandler(HTTP_METHOD method, const TStringBuf& path) const;
size_t GetSize() const;
template <typename F>
void ForEach(F f) const {
for (const auto& [p, handler] : Data) {
f(p.first, p.second, handler);
}
}
private:
std::map<std::pair<HTTP_METHOD, TString>, TJsonHandlerBase::TPtr> Data;
};
}
}
|