From 1e3ef46862449de5788f3011c1ead3c588e88a01 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Nov 2017 11:58:09 -0500 Subject: [PATCH] Changed to return 'Server&' from 'get' and 'post' --- httplib.h | 10 ++++++---- test/test.cc | 52 ++++++++++++++++++++++++---------------------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/httplib.h b/httplib.h index 1e0259d..abb39ca 100644 --- a/httplib.h +++ b/httplib.h @@ -133,8 +133,8 @@ public: Server(); virtual ~Server(); - void get(const char* pattern, Handler handler); - void post(const char* pattern, Handler handler); + Server& get(const char* pattern, Handler handler); + Server& post(const char* pattern, Handler handler); 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)); + 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)); + return *this; } inline bool Server::set_base_dir(const char* path) diff --git a/test/test.cc b/test/test.cc index 98f0954..b27e72b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -122,34 +122,30 @@ protected: svr_.set_base_dir("./www"); svr_.get("/hi", [&](const Request& req, Response& res) { - res.set_content("Hello World!", "text/plain"); - }); - - svr_.get("/", [&](const Request& req, Response& res) { - res.set_redirect("/hi"); - }); - - svr_.post("/person", [&](const Request& req, Response& res) { - if (req.has_param("name") && req.has_param("note")) { - persons_[req.params.at("name")] = req.params.at("note"); - } else { - res.status = 400; - } - }); - - svr_.get("/person/(.*)", [&](const Request& req, Response& res) { - string name = req.matches[1]; - if (persons_.find(name) != persons_.end()) { - auto note = persons_[name]; - res.set_content(note, "text/plain"); - } else { - res.status = 404; - } - }); - - svr_.get("/stop", [&](const Request& req, Response& res) { - svr_.stop(); - }); + res.set_content("Hello World!", "text/plain"); + }) + .get("/", [&](const Request& req, Response& res) { + res.set_redirect("/hi"); + }) + .post("/person", [&](const Request& req, Response& res) { + if (req.has_param("name") && req.has_param("note")) { + persons_[req.params.at("name")] = req.params.at("note"); + } else { + res.status = 400; + } + }) + .get("/person/(.*)", [&](const Request& req, Response& res) { + string name = req.matches[1]; + if (persons_.find(name) != persons_.end()) { + auto note = persons_[name]; + res.set_content(note, "text/plain"); + } else { + res.status = 404; + } + }) + .get("/stop", [&](const Request& req, Response& res) { + svr_.stop(); + }); persons_["john"] = "programmer";