Changed to return 'Server&' from 'get' and 'post'
This commit is contained in:
parent
4d62b15d94
commit
1e3ef46862
10
httplib.h
10
httplib.h
@ -133,8 +133,8 @@ public:
|
|||||||
Server();
|
Server();
|
||||||
virtual ~Server();
|
virtual ~Server();
|
||||||
|
|
||||||
void get(const char* pattern, Handler handler);
|
Server& get(const char* pattern, Handler handler);
|
||||||
void post(const char* pattern, Handler handler);
|
Server& post(const char* pattern, Handler handler);
|
||||||
|
|
||||||
bool set_base_dir(const char* path);
|
bool set_base_dir(const char* path);
|
||||||
|
|
||||||
@ -846,14 +846,16 @@ inline Server::~Server()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Server::get(const char* pattern, Handler handler)
|
inline Server& Server::get(const char* pattern, Handler handler)
|
||||||
{
|
{
|
||||||
get_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
|
get_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Server::post(const char* pattern, Handler handler)
|
inline Server& Server::post(const char* pattern, Handler handler)
|
||||||
{
|
{
|
||||||
post_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
|
post_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Server::set_base_dir(const char* path)
|
inline bool Server::set_base_dir(const char* path)
|
||||||
|
20
test/test.cc
20
test/test.cc
@ -123,21 +123,18 @@ protected:
|
|||||||
|
|
||||||
svr_.get("/hi", [&](const Request& req, Response& res) {
|
svr_.get("/hi", [&](const Request& req, Response& res) {
|
||||||
res.set_content("Hello World!", "text/plain");
|
res.set_content("Hello World!", "text/plain");
|
||||||
});
|
})
|
||||||
|
.get("/", [&](const Request& req, Response& res) {
|
||||||
svr_.get("/", [&](const Request& req, Response& res) {
|
|
||||||
res.set_redirect("/hi");
|
res.set_redirect("/hi");
|
||||||
});
|
})
|
||||||
|
.post("/person", [&](const Request& req, Response& res) {
|
||||||
svr_.post("/person", [&](const Request& req, Response& res) {
|
|
||||||
if (req.has_param("name") && req.has_param("note")) {
|
if (req.has_param("name") && req.has_param("note")) {
|
||||||
persons_[req.params.at("name")] = req.params.at("note");
|
persons_[req.params.at("name")] = req.params.at("note");
|
||||||
} else {
|
} else {
|
||||||
res.status = 400;
|
res.status = 400;
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
.get("/person/(.*)", [&](const Request& req, Response& res) {
|
||||||
svr_.get("/person/(.*)", [&](const Request& req, Response& res) {
|
|
||||||
string name = req.matches[1];
|
string name = req.matches[1];
|
||||||
if (persons_.find(name) != persons_.end()) {
|
if (persons_.find(name) != persons_.end()) {
|
||||||
auto note = persons_[name];
|
auto note = persons_[name];
|
||||||
@ -145,9 +142,8 @@ protected:
|
|||||||
} else {
|
} else {
|
||||||
res.status = 404;
|
res.status = 404;
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
.get("/stop", [&](const Request& req, Response& res) {
|
||||||
svr_.get("/stop", [&](const Request& req, Response& res) {
|
|
||||||
svr_.stop();
|
svr_.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user