Skip to content

Commit

Permalink
support route mount on app directly
Browse files Browse the repository at this point in the history
  • Loading branch information
sumory committed Dec 22, 2017
1 parent fee47b1 commit 4b28b14
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
43 changes: 42 additions & 1 deletion ciao/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Middleware> ms) {
router->get("", ms);
return *this;
}
App& post(Middleware m) {
router->post("", m);
return *this;
}
App& post(std::vector<Middleware> ms) {
router->post("", ms);
return *this;
}
App& del(Middleware m) {
router->del("", m);
return *this;
}
App& del(std::vector<Middleware> ms) {
router->del("", ms);
return *this;
}
App& put(Middleware m) {
router->put("", m);
return *this;
}
App& put(std::vector<Middleware> ms) {
router->put("", ms);
return *this;
}
App& patch(Middleware m) {
router->patch("", m);
return *this;
}
App& patch(std::vector<Middleware> ms) {
router->patch("", ms);
return *this;
}
// basic methods end.
};

Expand Down
2 changes: 1 addition & 1 deletion ciao/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Group {
std::string name() { return _name; }
std::vector<GroupMethodHolder> get_apis() { return _apis; }

// 挂载到path指定的group子路由上
// 挂载到path指定的group子路由上,不使用模板和宏,显式定义各个方法
Group& get(const std::string& path, Middleware m) {
_set_api("get", path, m);
return *this;
Expand Down
12 changes: 12 additions & 0 deletions test/app/define_app_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {});
Expand Down

0 comments on commit 4b28b14

Please sign in to comment.