diff --git a/ciao/app.h b/ciao/app.h index 89f6848..b1c1510 100644 --- a/ciao/app.h +++ b/ciao/app.h @@ -199,7 +199,7 @@ class App { return *this; } - // basic methods start... + // basic methods start...,不使用模板和宏,显式定义各个方法,方便后续重构 App& get(const std::string& path, Middleware m) { router->get(path, m); return *this; @@ -240,6 +240,47 @@ class App { router->patch(path, ms); return *this; } + + App& get(Middleware m) { + router->get("", m); + return *this; + } + App& get(std::vector ms) { + router->get("", ms); + return *this; + } + App& post(Middleware m) { + router->post("", m); + return *this; + } + App& post(std::vector ms) { + router->post("", ms); + return *this; + } + App& del(Middleware m) { + router->del("", m); + return *this; + } + App& del(std::vector ms) { + router->del("", ms); + return *this; + } + App& put(Middleware m) { + router->put("", m); + return *this; + } + App& put(std::vector ms) { + router->put("", ms); + return *this; + } + App& patch(Middleware m) { + router->patch("", m); + return *this; + } + App& patch(std::vector ms) { + router->patch("", ms); + return *this; + } // basic methods end. }; diff --git a/ciao/group.h b/ciao/group.h index 78b8668..0954259 100644 --- a/ciao/group.h +++ b/ciao/group.h @@ -23,7 +23,7 @@ class Group { std::string name() { return _name; } std::vector get_apis() { return _apis; } - // 挂载到path指定的group子路由上 + // 挂载到path指定的group子路由上,不使用模板和宏,显式定义各个方法 Group& get(const std::string& path, Middleware m) { _set_api("get", path, m); return *this; diff --git a/test/app/define_app_test.cc b/test/app/define_app_test.cc index d87fba5..c885ce2 100644 --- a/test/app/define_app_test.cc +++ b/test/app/define_app_test.cc @@ -17,6 +17,18 @@ TEST_F(AppTest, DefineAppWithParamsTest) { ASSERT_NE(nullptr, app.router); } +TEST_F(AppTest, AppRootRouteTest) { + ciao::App app; + ciao::Middleware m = [](ciao::Request& req, ciao::Response& res, ciao::Next& next) {}; + app.get(m); + app.post(m); + app.put({m,m}); + + ASSERT_EQ(1, app.router->trie.add_node("")->handlers["get"].size()); + ASSERT_EQ(1, app.router->trie.add_node("")->handlers["post"].size()); + ASSERT_EQ(2, app.router->trie.add_node("")->handlers["put"].size()); +} + TEST_F(AppTest, DefineHttpMethodTest) { ciao::App app; app.get("/get", [](ciao::Request& req, ciao::Response& res, ciao::Next& next) {});