drogon
C++14/17-based HTTP application framework
Toggle main menu visibility
Loading...
Searching...
No Matches
HttpController.h
1
14
15
#pragma once
16
17
#include <
drogon/DrObject.h
>
18
#include <drogon/utils/HttpConstraint.h>
19
#include <
drogon/HttpAppFramework.h
>
20
#include <iostream>
21
#include <string>
22
#include <trantor/utils/Logger.h>
23
#include <vector>
24
27
28
#define METHOD_LIST_BEGIN \
29
static void initPathRouting() \
30
{
31
#define METHOD_ADD(method, pattern, ...) \
32
registerMethod(&method, pattern, {__VA_ARGS__}, true, #method)
33
#define ADD_METHOD_TO(method, path_pattern, ...) \
34
registerMethod(&method, path_pattern, {__VA_ARGS__}, false, #method)
35
#define ADD_METHOD_VIA_REGEX(method, regex, ...) \
36
registerMethodViaRegex(&method, regex, {__VA_ARGS__}, #method)
37
#define METHOD_LIST_END \
38
return; \
39
}
40
41
namespace
drogon
42
{
47
class
HttpControllerBase
48
{
49
};
50
58
template
<
typename
T,
bool
AutoCreation = true>
59
class
HttpController
:
public
DrObject<T>,
public
HttpControllerBase
60
{
61
public
:
62
static
constexpr
bool
isAutoCreation = AutoCreation;
63
64
protected
:
65
template
<
typename
FUNCTION>
66
static
void
registerMethod(
67
FUNCTION &&function,
68
const
std::string &pattern,
69
const
std::vector<internal::HttpConstraint> &constraints = {},
70
bool
classNameInPath =
true
,
71
const
std::string &handlerName =
""
)
72
{
73
if
(classNameInPath)
74
{
75
std::string path =
"/"
;
76
path.append(HttpController<T, AutoCreation>::classTypeName());
77
LOG_TRACE <<
"classname:"
78
<< HttpController<T, AutoCreation>::classTypeName();
79
80
// transform(path.begin(), path.end(), path.begin(), [](unsigned
81
// char c){ return tolower(c); });
82
std::string::size_type pos;
83
while
((pos = path.find(
"::"
)) != std::string::npos)
84
{
85
path.replace(pos, 2,
"/"
);
86
}
87
if
(pattern.empty() || pattern[0] ==
'/'
)
88
app
().
registerHandler
(path + pattern,
89
std::forward<FUNCTION>(function),
90
constraints,
91
handlerName);
92
else
93
app
().
registerHandler
(path +
"/"
+ pattern,
94
std::forward<FUNCTION>(function),
95
constraints,
96
handlerName);
97
}
98
else
99
{
100
std::string path = pattern;
101
if
(path.empty() || path[0] !=
'/'
)
102
{
103
path =
"/"
+ path;
104
}
105
app
().
registerHandler
(path,
106
std::forward<FUNCTION>(function),
107
constraints,
108
handlerName);
109
}
110
}
111
112
template
<
typename
FUNCTION>
113
static
void
registerMethodViaRegex(
114
FUNCTION &&function,
115
const
std::string ®Exp,
116
const
std::vector<internal::HttpConstraint> &constraints =
117
std::vector<internal::HttpConstraint>{},
118
const
std::string &handlerName =
""
)
119
{
120
app
().
registerHandlerViaRegex
(regExp,
121
std::forward<FUNCTION>(function),
122
constraints,
123
handlerName);
124
}
125
126
private
:
127
class
methodRegistrator
128
{
129
public
:
130
methodRegistrator()
131
{
132
if
(AutoCreation)
133
T::initPathRouting();
134
}
135
};
136
137
// use static value to register controller method in framework before
138
// main();
139
static
methodRegistrator registrator_;
140
141
virtual
void
*touch()
142
{
143
return
®istrator_;
144
}
145
};
146
147
template
<
typename
T,
bool
AutoCreation>
148
typename
HttpController<T, AutoCreation>::methodRegistrator
149
HttpController<T, AutoCreation>::registrator_;
150
}
// namespace drogon
DrObject.h
HttpAppFramework.h
drogon::HttpAppFramework::registerHandlerViaRegex
HttpAppFramework & registerHandlerViaRegex(const std::string ®Exp, FUNCTION &&function, const std::vector< internal::HttpConstraint > &constraints={}, const std::string &handlerName="")
Register a handler into the framework via a regular expression.
Definition
HttpAppFramework.h:586
drogon::HttpAppFramework::registerHandler
HttpAppFramework & registerHandler(const std::string &pathPattern, FUNCTION &&function, const std::vector< internal::HttpConstraint > &constraints={}, const std::string &handlerName="")
Register a handler into the framework.
Definition
HttpAppFramework.h:535
drogon::HttpControllerBase
The base class for HTTP controllers.
Definition
HttpController.h:48
drogon::HttpController
The reflection base class template for HTTP controllers.
Definition
HttpController.h:60
drogon
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition
Attribute.h:23
drogon::app
HttpAppFramework & app()
A wrapper of the instance() method.
Definition
HttpAppFramework.h:1656
lib
inc
drogon
HttpController.h
Generated by
1.17.0