Rename RouterXXX to CtrlXXX

This commit is contained in:
Ming Deng 2021-08-04 21:47:47 +08:00
parent c6b001d16e
commit 32d7633a04
8 changed files with 295 additions and 271 deletions

View File

@ -439,6 +439,21 @@ func (c *Controller) URLFor(endpoint string, values ...interface{}) string {
return URLFor(endpoint, values...) return URLFor(endpoint, values...)
} }
func (c *Controller) JsonResp(data interface{}) error {
c.Data["json"]=data
return c.ServeJSON()
}
func (c *Controller) XmlResp(data interface{}) error {
c.Data["xml"] = data
return c.ServeXML()
}
func (c *Controller) YamlResp(data interface{}) error {
c.Data["yaml"] = data
return c.ServeYAML()
}
// Resp sends response based on the Accept Header // Resp sends response based on the Accept Header
// By default response will be in JSON // By default response will be in JSON
func (c *Controller) Resp(data interface{}) error { func (c *Controller) Resp(data interface{}) error {

View File

@ -26,7 +26,9 @@ import (
type FilterChain func(next FilterFunc) FilterFunc type FilterChain func(next FilterFunc) FilterFunc
// FilterFunc defines a filter function which is invoked before the controller handler is executed. // FilterFunc defines a filter function which is invoked before the controller handler is executed.
type FilterFunc func(ctx *context.Context) // It's a alias of HandleFunc
// In fact, the HandleFunc is the last Filter. This is the truth
type FilterFunc=HandleFunc
// FilterRouter defines a filter operation which is invoked before the controller handler is executed. // FilterRouter defines a filter operation which is invoked before the controller handler is executed.
// It can match the URL against a pattern, and execute a filter function // It can match the URL against a pattern, and execute a filter function

View File

@ -119,56 +119,56 @@ func (n *Namespace) AutoPrefix(prefix string, c ControllerInterface) *Namespace
// Get same as beego.Get // Get same as beego.Get
// refer: https://godoc.org/github.com/beego/beego/v2#Get // refer: https://godoc.org/github.com/beego/beego/v2#Get
func (n *Namespace) Get(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Get(rootpath string, f HandleFunc) *Namespace {
n.handlers.Get(rootpath, f) n.handlers.Get(rootpath, f)
return n return n
} }
// Post same as beego.Post // Post same as beego.Post
// refer: https://godoc.org/github.com/beego/beego/v2#Post // refer: https://godoc.org/github.com/beego/beego/v2#Post
func (n *Namespace) Post(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Post(rootpath string, f HandleFunc) *Namespace {
n.handlers.Post(rootpath, f) n.handlers.Post(rootpath, f)
return n return n
} }
// Delete same as beego.Delete // Delete same as beego.Delete
// refer: https://godoc.org/github.com/beego/beego/v2#Delete // refer: https://godoc.org/github.com/beego/beego/v2#Delete
func (n *Namespace) Delete(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Delete(rootpath string, f HandleFunc) *Namespace {
n.handlers.Delete(rootpath, f) n.handlers.Delete(rootpath, f)
return n return n
} }
// Put same as beego.Put // Put same as beego.Put
// refer: https://godoc.org/github.com/beego/beego/v2#Put // refer: https://godoc.org/github.com/beego/beego/v2#Put
func (n *Namespace) Put(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Put(rootpath string, f HandleFunc) *Namespace {
n.handlers.Put(rootpath, f) n.handlers.Put(rootpath, f)
return n return n
} }
// Head same as beego.Head // Head same as beego.Head
// refer: https://godoc.org/github.com/beego/beego/v2#Head // refer: https://godoc.org/github.com/beego/beego/v2#Head
func (n *Namespace) Head(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Head(rootpath string, f HandleFunc) *Namespace {
n.handlers.Head(rootpath, f) n.handlers.Head(rootpath, f)
return n return n
} }
// Options same as beego.Options // Options same as beego.Options
// refer: https://godoc.org/github.com/beego/beego/v2#Options // refer: https://godoc.org/github.com/beego/beego/v2#Options
func (n *Namespace) Options(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Options(rootpath string, f HandleFunc) *Namespace {
n.handlers.Options(rootpath, f) n.handlers.Options(rootpath, f)
return n return n
} }
// Patch same as beego.Patch // Patch same as beego.Patch
// refer: https://godoc.org/github.com/beego/beego/v2#Patch // refer: https://godoc.org/github.com/beego/beego/v2#Patch
func (n *Namespace) Patch(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Patch(rootpath string, f HandleFunc) *Namespace {
n.handlers.Patch(rootpath, f) n.handlers.Patch(rootpath, f)
return n return n
} }
// Any same as beego.Any // Any same as beego.Any
// refer: https://godoc.org/github.com/beego/beego/v2#Any // refer: https://godoc.org/github.com/beego/beego/v2#Any
func (n *Namespace) Any(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Any(rootpath string, f HandleFunc) *Namespace {
n.handlers.Any(rootpath, f) n.handlers.Any(rootpath, f)
return n return n
} }
@ -187,51 +187,51 @@ func (n *Namespace) Include(cList ...ControllerInterface) *Namespace {
return n return n
} }
// RouterGet same as beego.RouterGet // CtrlGet same as beego.CtrlGet
func (n *Namespace) RouterGet(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlGet(rootpath string, f interface{}) *Namespace {
n.handlers.RouterGet(rootpath, f) n.handlers.CtrlGet(rootpath, f)
return n return n
} }
// RouterPost same as beego.RouterPost // CtrlPost same as beego.CtrlPost
func (n *Namespace) RouterPost(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlPost(rootpath string, f interface{}) *Namespace {
n.handlers.RouterPost(rootpath, f) n.handlers.CtrlPost(rootpath, f)
return n return n
} }
// RouterDelete same as beego.RouterDelete // CtrlDelete same as beego.CtrlDelete
func (n *Namespace) RouterDelete(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlDelete(rootpath string, f interface{}) *Namespace {
n.handlers.RouterDelete(rootpath, f) n.handlers.CtrlDelete(rootpath, f)
return n return n
} }
// RouterPut same as beego.RouterPut // CtrlPut same as beego.CtrlPut
func (n *Namespace) RouterPut(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlPut(rootpath string, f interface{}) *Namespace {
n.handlers.RouterPut(rootpath, f) n.handlers.CtrlPut(rootpath, f)
return n return n
} }
// RouterHead same as beego.RouterHead // CtrlHead same as beego.CtrlHead
func (n *Namespace) RouterHead(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlHead(rootpath string, f interface{}) *Namespace {
n.handlers.RouterHead(rootpath, f) n.handlers.CtrlHead(rootpath, f)
return n return n
} }
// RouterOptions same as beego.RouterOptions // CtrlOptions same as beego.CtrlOptions
func (n *Namespace) RouterOptions(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlOptions(rootpath string, f interface{}) *Namespace {
n.handlers.RouterOptions(rootpath, f) n.handlers.CtrlOptions(rootpath, f)
return n return n
} }
// RouterPatch same as beego.RouterPatch // CtrlPatch same as beego.CtrlPatch
func (n *Namespace) RouterPatch(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlPatch(rootpath string, f interface{}) *Namespace {
n.handlers.RouterPatch(rootpath, f) n.handlers.CtrlPatch(rootpath, f)
return n return n
} }
// Any same as beego.RouterAny // Any same as beego.CtrlAny
func (n *Namespace) RouterAny(rootpath string, f interface{}) *Namespace { func (n *Namespace) CtrlAny(rootpath string, f interface{}) *Namespace {
n.handlers.RouterAny(rootpath, f) n.handlers.CtrlAny(rootpath, f)
return n return n
} }
@ -359,114 +359,114 @@ func NSRouter(rootpath string, c ControllerInterface, mappingMethods ...string)
} }
// NSGet call Namespace Get // NSGet call Namespace Get
func NSGet(rootpath string, f FilterFunc) LinkNamespace { func NSGet(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Get(rootpath, f) ns.Get(rootpath, f)
} }
} }
// NSPost call Namespace Post // NSPost call Namespace Post
func NSPost(rootpath string, f FilterFunc) LinkNamespace { func NSPost(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Post(rootpath, f) ns.Post(rootpath, f)
} }
} }
// NSHead call Namespace Head // NSHead call Namespace Head
func NSHead(rootpath string, f FilterFunc) LinkNamespace { func NSHead(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Head(rootpath, f) ns.Head(rootpath, f)
} }
} }
// NSPut call Namespace Put // NSPut call Namespace Put
func NSPut(rootpath string, f FilterFunc) LinkNamespace { func NSPut(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Put(rootpath, f) ns.Put(rootpath, f)
} }
} }
// NSDelete call Namespace Delete // NSDelete call Namespace Delete
func NSDelete(rootpath string, f FilterFunc) LinkNamespace { func NSDelete(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Delete(rootpath, f) ns.Delete(rootpath, f)
} }
} }
// NSAny call Namespace Any // NSAny call Namespace Any
func NSAny(rootpath string, f FilterFunc) LinkNamespace { func NSAny(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Any(rootpath, f) ns.Any(rootpath, f)
} }
} }
// NSOptions call Namespace Options // NSOptions call Namespace Options
func NSOptions(rootpath string, f FilterFunc) LinkNamespace { func NSOptions(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Options(rootpath, f) ns.Options(rootpath, f)
} }
} }
// NSPatch call Namespace Patch // NSPatch call Namespace Patch
func NSPatch(rootpath string, f FilterFunc) LinkNamespace { func NSPatch(rootpath string, f HandleFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Patch(rootpath, f) ns.Patch(rootpath, f)
} }
} }
// NSRouterGet call Namespace RouterGet // NSCtrlGet call Namespace CtrlGet
func NSRouterGet(rootpath string, f interface{}) LinkNamespace { func NSCtrlGet(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterGet(rootpath, f) ns.CtrlGet(rootpath, f)
} }
} }
// NSRouterPost call Namespace RouterPost // NSCtrlPost call Namespace CtrlPost
func NSRouterPost(rootpath string, f interface{}) LinkNamespace { func NSCtrlPost(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterPost(rootpath, f) ns.CtrlPost(rootpath, f)
} }
} }
// NSRouterHead call Namespace RouterHead // NSCtrlHead call Namespace CtrlHead
func NSRouterHead(rootpath string, f interface{}) LinkNamespace { func NSCtrlHead(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterHead(rootpath, f) ns.CtrlHead(rootpath, f)
} }
} }
// NSRouterPut call Namespace RouterPut // NSCtrlPut call Namespace CtrlPut
func NSRouterPut(rootpath string, f interface{}) LinkNamespace { func NSCtrlPut(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterPut(rootpath, f) ns.CtrlPut(rootpath, f)
} }
} }
// NSRouterDelete call Namespace RouterDelete // NSCtrlDelete call Namespace CtrlDelete
func NSRouterDelete(rootpath string, f interface{}) LinkNamespace { func NSCtrlDelete(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterDelete(rootpath, f) ns.CtrlDelete(rootpath, f)
} }
} }
// NSRouterAny call Namespace RouterAny // NSCtrlAny call Namespace CtrlAny
func NSRouterAny(rootpath string, f interface{}) LinkNamespace { func NSCtrlAny(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterAny(rootpath, f) ns.CtrlAny(rootpath, f)
} }
} }
// NSRouterOptions call Namespace RouterOptions // NSCtrlOptions call Namespace CtrlOptions
func NSRouterOptions(rootpath string, f interface{}) LinkNamespace { func NSCtrlOptions(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterOptions(rootpath, f) ns.CtrlOptions(rootpath, f)
} }
} }
// NSRouterPatch call Namespace RouterPatch // NSCtrlPatch call Namespace CtrlPatch
func NSRouterPatch(rootpath string, f interface{}) LinkNamespace { func NSCtrlPatch(rootpath string, f interface{}) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.RouterPatch(rootpath, f) ns.CtrlPatch(rootpath, f)
} }
} }

View File

@ -202,100 +202,100 @@ func TestNamespaceInside(t *testing.T) {
} }
} }
func TestNamespaceRouterGet(t *testing.T) { func TestNamespaceCtrlGet(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodGet, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterGet(nsPath, ExampleController.Ping) ns.CtrlGet(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterGet can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlGet can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceRouterPost(t *testing.T) { func TestNamespaceCtrlPost(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodPost, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterPost(nsPath, ExampleController.Ping) ns.CtrlPost(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterPost can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlPost can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceRouterDelete(t *testing.T) { func TestNamespaceCtrlDelete(t *testing.T) {
r, _ := http.NewRequest(http.MethodDelete, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodDelete, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterDelete(nsPath, ExampleController.Ping) ns.CtrlDelete(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterDelete can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlDelete can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceRouterPut(t *testing.T) { func TestNamespaceCtrlPut(t *testing.T) {
r, _ := http.NewRequest(http.MethodPut, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodPut, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterPut(nsPath, ExampleController.Ping) ns.CtrlPut(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterPut can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlPut can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceRouterHead(t *testing.T) { func TestNamespaceCtrlHead(t *testing.T) {
r, _ := http.NewRequest(http.MethodHead, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodHead, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterHead(nsPath, ExampleController.Ping) ns.CtrlHead(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterHead can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlHead can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceRouterOptions(t *testing.T) { func TestNamespaceCtrlOptions(t *testing.T) {
r, _ := http.NewRequest(http.MethodOptions, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodOptions, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterOptions(nsPath, ExampleController.Ping) ns.CtrlOptions(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterOptions can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlOptions can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceRouterPatch(t *testing.T) { func TestNamespaceCtrlPatch(t *testing.T) {
r, _ := http.NewRequest(http.MethodPatch, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodPatch, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterPatch(nsPath, ExampleController.Ping) ns.CtrlPatch(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterPatch can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlPatch can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceRouterAny(t *testing.T) { func TestNamespaceCtrlAny(t *testing.T) {
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
ns.RouterAny(nsPath, ExampleController.Ping) ns.CtrlAny(nsPath, ExampleController.Ping)
AddNamespace(ns) AddNamespace(ns)
for method := range HTTPMETHOD { for method := range HTTPMETHOD {
@ -303,105 +303,105 @@ func TestNamespaceRouterAny(t *testing.T) {
r, _ := http.NewRequest(method, nsNamespacePath, nil) r, _ := http.NewRequest(method, nsNamespacePath, nil)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceRouterAny can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceCtrlAny can't run, get the response is " + w.Body.String())
} }
} }
} }
func TestNamespaceNSRouterGet(t *testing.T) { func TestNamespaceNSCtrlGet(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodGet, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
NSRouterGet(nsPath, ExampleController.Ping)(ns) NSCtrlGet(nsPath, ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterGet can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlGet can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceNSRouterPost(t *testing.T) { func TestNamespaceNSCtrlPost(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodPost, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace("/router") ns := NewNamespace("/router")
NSRouterPost(nsPath, ExampleController.Ping)(ns) NSCtrlPost(nsPath, ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterPost can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlPost can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceNSRouterDelete(t *testing.T) { func TestNamespaceNSCtrlDelete(t *testing.T) {
r, _ := http.NewRequest(http.MethodDelete, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodDelete, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
NSRouterDelete(nsPath, ExampleController.Ping)(ns) NSCtrlDelete(nsPath, ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterDelete can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlDelete can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceNSRouterPut(t *testing.T) { func TestNamespaceNSCtrlPut(t *testing.T) {
r, _ := http.NewRequest(http.MethodPut, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodPut, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
NSRouterPut(nsPath, ExampleController.Ping)(ns) NSCtrlPut(nsPath, ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterPut can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlPut can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceNSRouterHead(t *testing.T) { func TestNamespaceNSCtrlHead(t *testing.T) {
r, _ := http.NewRequest(http.MethodHead, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodHead, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
NSRouterHead(nsPath, ExampleController.Ping)(ns) NSCtrlHead(nsPath, ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterHead can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlHead can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceNSRouterOptions(t *testing.T) { func TestNamespaceNSCtrlOptions(t *testing.T) {
r, _ := http.NewRequest(http.MethodOptions, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodOptions, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
NSRouterOptions(nsPath, ExampleController.Ping)(ns) NSCtrlOptions(nsPath, ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterOptions can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlOptions can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceNSRouterPatch(t *testing.T) { func TestNamespaceNSCtrlPatch(t *testing.T) {
r, _ := http.NewRequest(http.MethodPatch, nsNamespacePath, nil) r, _ := http.NewRequest(http.MethodPatch, nsNamespacePath, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
NSRouterPatch("/user", ExampleController.Ping)(ns) NSCtrlPatch("/user", ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterPatch can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlPatch can't run, get the response is " + w.Body.String())
} }
} }
func TestNamespaceNSRouterAny(t *testing.T) { func TestNamespaceNSCtrlAny(t *testing.T) {
ns := NewNamespace(nsNamespace) ns := NewNamespace(nsNamespace)
NSRouterAny(nsPath, ExampleController.Ping)(ns) NSCtrlAny(nsPath, ExampleController.Ping)(ns)
AddNamespace(ns) AddNamespace(ns)
for method := range HTTPMETHOD { for method := range HTTPMETHOD {
@ -409,7 +409,7 @@ func TestNamespaceNSRouterAny(t *testing.T) {
r, _ := http.NewRequest(method, nsNamespacePath, nil) r, _ := http.NewRequest(method, nsNamespacePath, nil)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestNamespaceNSRouterAny can't run, get the response is " + w.Body.String()) t.Errorf("TestNamespaceNSCtrlAny can't run, get the response is " + w.Body.String())
} }
} }
} }

View File

@ -119,7 +119,7 @@ type ControllerInfo struct {
controllerType reflect.Type controllerType reflect.Type
methods map[string]string methods map[string]string
handler http.Handler handler http.Handler
runFunction FilterFunc runFunction HandleFunc
routerType int routerType int
initialize func() ControllerInterface initialize func() ControllerInterface
methodParams []*param.MethodParam methodParams []*param.MethodParam
@ -354,7 +354,7 @@ func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) {
p.pool.Put(ctx) p.pool.Put(ctx)
} }
// RouterGet add get method // CtrlGet add get method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -363,12 +363,13 @@ func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterGet("/api/:id", MyController.Ping) // CtrlGet("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterGet(pattern string, f interface{}) { // If the receiver of function Ping is pointer, you should use CtrlGet("/api/:id", (*MyController).Ping)
func (p *ControllerRegister) CtrlGet(pattern string, f interface{}) {
p.AddRouterMethod(http.MethodGet, pattern, f) p.AddRouterMethod(http.MethodGet, pattern, f)
} }
// RouterPost add post method // CtrlPost add post method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -377,12 +378,13 @@ func (p *ControllerRegister) RouterGet(pattern string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterPost("/api/:id", MyController.Ping) // CtrlPost("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterPost(pattern string, f interface{}) { // If the receiver of function Ping is pointer, you should use CtrlPost("/api/:id", (*MyController).Ping)
func (p *ControllerRegister) CtrlPost(pattern string, f interface{}) {
p.AddRouterMethod(http.MethodPost, pattern, f) p.AddRouterMethod(http.MethodPost, pattern, f)
} }
// RouterHead add head method // CtrlHead add head method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -391,12 +393,13 @@ func (p *ControllerRegister) RouterPost(pattern string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterHead("/api/:id", MyController.Ping) // CtrlHead("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterHead(pattern string, f interface{}) { // If the receiver of function Ping is pointer, you should use CtrlHead("/api/:id", (*MyController).Ping)
func (p *ControllerRegister) CtrlHead(pattern string, f interface{}) {
p.AddRouterMethod(http.MethodHead, pattern, f) p.AddRouterMethod(http.MethodHead, pattern, f)
} }
// RouterPut add put method // CtrlPut add put method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -405,12 +408,13 @@ func (p *ControllerRegister) RouterHead(pattern string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterPut("/api/:id", MyController.Ping) // CtrlPut("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterPut(pattern string, f interface{}) {
func (p *ControllerRegister) CtrlPut(pattern string, f interface{}) {
p.AddRouterMethod(http.MethodPut, pattern, f) p.AddRouterMethod(http.MethodPut, pattern, f)
} }
// RouterPatch add patch method // CtrlPatch add patch method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -419,12 +423,12 @@ func (p *ControllerRegister) RouterPut(pattern string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterPatch("/api/:id", MyController.Ping) // CtrlPatch("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterPatch(pattern string, f interface{}) { func (p *ControllerRegister) CtrlPatch(pattern string, f interface{}) {
p.AddRouterMethod(http.MethodPatch, pattern, f) p.AddRouterMethod(http.MethodPatch, pattern, f)
} }
// RouterDelete add delete method // CtrlDelete add delete method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -433,12 +437,12 @@ func (p *ControllerRegister) RouterPatch(pattern string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterDelete("/api/:id", MyController.Ping) // CtrlDelete("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterDelete(pattern string, f interface{}) { func (p *ControllerRegister) CtrlDelete(pattern string, f interface{}) {
p.AddRouterMethod(http.MethodDelete, pattern, f) p.AddRouterMethod(http.MethodDelete, pattern, f)
} }
// RouterOptions add options method // CtrlOptions add options method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -447,12 +451,12 @@ func (p *ControllerRegister) RouterDelete(pattern string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterOptions("/api/:id", MyController.Ping) // CtrlOptions("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterOptions(pattern string, f interface{}) { func (p *ControllerRegister) CtrlOptions(pattern string, f interface{}) {
p.AddRouterMethod(http.MethodOptions, pattern, f) p.AddRouterMethod(http.MethodOptions, pattern, f)
} }
// RouterAny add all method // CtrlAny add all method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -461,8 +465,8 @@ func (p *ControllerRegister) RouterOptions(pattern string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterAny("/api/:id", MyController.Ping) // CtrlAny("/api/:id", MyController.Ping)
func (p *ControllerRegister) RouterAny(pattern string, f interface{}) { func (p *ControllerRegister) CtrlAny(pattern string, f interface{}) {
p.AddRouterMethod("*", pattern, f) p.AddRouterMethod("*", pattern, f)
} }
@ -503,7 +507,7 @@ func (p *ControllerRegister) createBeegoRouter(ct reflect.Type, pattern string)
} }
// createRestfulRouter create restful router with filter function and pattern // createRestfulRouter create restful router with filter function and pattern
func (p *ControllerRegister) createRestfulRouter(f FilterFunc, pattern string) *ControllerInfo { func (p *ControllerRegister) createRestfulRouter(f HandleFunc, pattern string) *ControllerInfo {
route := &ControllerInfo{} route := &ControllerInfo{}
route.pattern = pattern route.pattern = pattern
route.routerType = routerTypeRESTFul route.routerType = routerTypeRESTFul
@ -609,12 +613,15 @@ func getReflectTypeAndMethod(f interface{}) (controllerType reflect.Type, method
return return
} }
// HandleFunc define how to process the request
type HandleFunc func(ctx *beecontext.Context)
// Get add get method // Get add get method
// usage: // usage:
// Get("/", func(ctx *context.Context){ // Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Get(pattern string, f FilterFunc) { func (p *ControllerRegister) Get(pattern string, f HandleFunc) {
p.AddMethod("get", pattern, f) p.AddMethod("get", pattern, f)
} }
@ -623,7 +630,7 @@ func (p *ControllerRegister) Get(pattern string, f FilterFunc) {
// Post("/api", func(ctx *context.Context){ // Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Post(pattern string, f FilterFunc) { func (p *ControllerRegister) Post(pattern string, f HandleFunc) {
p.AddMethod("post", pattern, f) p.AddMethod("post", pattern, f)
} }
@ -632,7 +639,7 @@ func (p *ControllerRegister) Post(pattern string, f FilterFunc) {
// Put("/api/:id", func(ctx *context.Context){ // Put("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Put(pattern string, f FilterFunc) { func (p *ControllerRegister) Put(pattern string, f HandleFunc) {
p.AddMethod("put", pattern, f) p.AddMethod("put", pattern, f)
} }
@ -641,7 +648,7 @@ func (p *ControllerRegister) Put(pattern string, f FilterFunc) {
// Delete("/api/:id", func(ctx *context.Context){ // Delete("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Delete(pattern string, f FilterFunc) { func (p *ControllerRegister) Delete(pattern string, f HandleFunc) {
p.AddMethod("delete", pattern, f) p.AddMethod("delete", pattern, f)
} }
@ -650,7 +657,7 @@ func (p *ControllerRegister) Delete(pattern string, f FilterFunc) {
// Head("/api/:id", func(ctx *context.Context){ // Head("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Head(pattern string, f FilterFunc) { func (p *ControllerRegister) Head(pattern string, f HandleFunc) {
p.AddMethod("head", pattern, f) p.AddMethod("head", pattern, f)
} }
@ -659,7 +666,7 @@ func (p *ControllerRegister) Head(pattern string, f FilterFunc) {
// Patch("/api/:id", func(ctx *context.Context){ // Patch("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Patch(pattern string, f FilterFunc) { func (p *ControllerRegister) Patch(pattern string, f HandleFunc) {
p.AddMethod("patch", pattern, f) p.AddMethod("patch", pattern, f)
} }
@ -668,7 +675,7 @@ func (p *ControllerRegister) Patch(pattern string, f FilterFunc) {
// Options("/api/:id", func(ctx *context.Context){ // Options("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Options(pattern string, f FilterFunc) { func (p *ControllerRegister) Options(pattern string, f HandleFunc) {
p.AddMethod("options", pattern, f) p.AddMethod("options", pattern, f)
} }
@ -677,7 +684,7 @@ func (p *ControllerRegister) Options(pattern string, f FilterFunc) {
// Any("/api/:id", func(ctx *context.Context){ // Any("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) Any(pattern string, f FilterFunc) { func (p *ControllerRegister) Any(pattern string, f HandleFunc) {
p.AddMethod("*", pattern, f) p.AddMethod("*", pattern, f)
} }
@ -686,7 +693,7 @@ func (p *ControllerRegister) Any(pattern string, f FilterFunc) {
// AddMethod("get","/api/:id", func(ctx *context.Context){ // AddMethod("get","/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) { func (p *ControllerRegister) AddMethod(method, pattern string, f HandleFunc) {
method = p.getUpperMethodString(method) method = p.getUpperMethodString(method)
route := p.createRestfulRouter(f, pattern) route := p.createRestfulRouter(f, pattern)

View File

@ -354,7 +354,7 @@ func TestAutoPrefix(t *testing.T) {
} }
} }
func TestRouterGet(t *testing.T) { func TestCtrlGet(t *testing.T) {
r, _ := http.NewRequest("GET", "/user", nil) r, _ := http.NewRequest("GET", "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -364,11 +364,11 @@ func TestRouterGet(t *testing.T) {
}) })
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != "Get userlist" { if w.Body.String() != "Get userlist" {
t.Errorf("TestRouterGet can't run") t.Errorf("TestCtrlGet can't run")
} }
} }
func TestRouterPost(t *testing.T) { func TestCtrlPost(t *testing.T) {
r, _ := http.NewRequest("POST", "/user/123", nil) r, _ := http.NewRequest("POST", "/user/123", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -378,7 +378,7 @@ func TestRouterPost(t *testing.T) {
}) })
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != "123" { if w.Body.String() != "123" {
t.Errorf("TestRouterPost can't run") t.Errorf("TestCtrlPost can't run")
} }
} }
@ -836,174 +836,174 @@ func TestRouterSessionSet(t *testing.T) {
} }
} }
func TestRouterRouterGet(t *testing.T) { func TestRouterCtrlGet(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/user", nil) r, _ := http.NewRequest(http.MethodGet, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterGet("/user", ExampleController.Ping) handler.CtrlGet("/user", ExampleController.Ping)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestRouterRouterGet can't run") t.Errorf("TestRouterCtrlGet can't run")
} }
} }
func TestRouterRouterPost(t *testing.T) { func TestRouterCtrlPost(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, "/user", nil) r, _ := http.NewRequest(http.MethodPost, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterPost("/user", ExampleController.Ping) handler.CtrlPost("/user", ExampleController.Ping)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestRouterRouterPost can't run") t.Errorf("TestRouterCtrlPost can't run")
} }
} }
func TestRouterRouterHead(t *testing.T) { func TestRouterCtrlHead(t *testing.T) {
r, _ := http.NewRequest(http.MethodHead, "/user", nil) r, _ := http.NewRequest(http.MethodHead, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterHead("/user", ExampleController.Ping) handler.CtrlHead("/user", ExampleController.Ping)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestRouterRouterHead can't run") t.Errorf("TestRouterCtrlHead can't run")
} }
} }
func TestRouterRouterPut(t *testing.T) { func TestRouterCtrlPut(t *testing.T) {
r, _ := http.NewRequest(http.MethodPut, "/user", nil) r, _ := http.NewRequest(http.MethodPut, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterPut("/user", ExampleController.Ping) handler.CtrlPut("/user", ExampleController.Ping)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestRouterRouterPut can't run") t.Errorf("TestRouterCtrlPut can't run")
} }
} }
func TestRouterRouterPatch(t *testing.T) { func TestRouterCtrlPatch(t *testing.T) {
r, _ := http.NewRequest(http.MethodPatch, "/user", nil) r, _ := http.NewRequest(http.MethodPatch, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterPatch("/user", ExampleController.Ping) handler.CtrlPatch("/user", ExampleController.Ping)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestRouterRouterPatch can't run") t.Errorf("TestRouterCtrlPatch can't run")
} }
} }
func TestRouterRouterDelete(t *testing.T) { func TestRouterCtrlDelete(t *testing.T) {
r, _ := http.NewRequest(http.MethodDelete, "/user", nil) r, _ := http.NewRequest(http.MethodDelete, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterDelete("/user", ExampleController.Ping) handler.CtrlDelete("/user", ExampleController.Ping)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestRouterRouterDelete can't run") t.Errorf("TestRouterCtrlDelete can't run")
} }
} }
func TestRouterRouterAny(t *testing.T) { func TestRouterCtrlAny(t *testing.T) {
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterAny("/user", ExampleController.Ping) handler.CtrlAny("/user", ExampleController.Ping)
for method := range HTTPMETHOD { for method := range HTTPMETHOD {
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest(method, "/user", nil) r, _ := http.NewRequest(method, "/user", nil)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestRouterRouterAny can't run, get the response is " + w.Body.String()) t.Errorf("TestRouterCtrlAny can't run, get the response is " + w.Body.String())
} }
} }
} }
func TestRouterRouterGetPointerMethod(t *testing.T) { func TestRouterCtrlGetPointerMethod(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/user", nil) r, _ := http.NewRequest(http.MethodGet, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterGet("/user", (*ExampleController).PingPointer) handler.CtrlGet("/user", (*ExampleController).PingPointer)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != examplePointerBody { if w.Body.String() != examplePointerBody {
t.Errorf("TestRouterRouterGetPointerMethod can't run") t.Errorf("TestRouterCtrlGetPointerMethod can't run")
} }
} }
func TestRouterRouterPostPointerMethod(t *testing.T) { func TestRouterCtrlPostPointerMethod(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, "/user", nil) r, _ := http.NewRequest(http.MethodPost, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterPost("/user", (*ExampleController).PingPointer) handler.CtrlPost("/user", (*ExampleController).PingPointer)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != examplePointerBody { if w.Body.String() != examplePointerBody {
t.Errorf("TestRouterRouterPostPointerMethod can't run") t.Errorf("TestRouterCtrlPostPointerMethod can't run")
} }
} }
func TestRouterRouterHeadPointerMethod(t *testing.T) { func TestRouterCtrlHeadPointerMethod(t *testing.T) {
r, _ := http.NewRequest(http.MethodHead, "/user", nil) r, _ := http.NewRequest(http.MethodHead, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterHead("/user", (*ExampleController).PingPointer) handler.CtrlHead("/user", (*ExampleController).PingPointer)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != examplePointerBody { if w.Body.String() != examplePointerBody {
t.Errorf("TestRouterRouterHeadPointerMethod can't run") t.Errorf("TestRouterCtrlHeadPointerMethod can't run")
} }
} }
func TestRouterRouterPutPointerMethod(t *testing.T) { func TestRouterCtrlPutPointerMethod(t *testing.T) {
r, _ := http.NewRequest(http.MethodPut, "/user", nil) r, _ := http.NewRequest(http.MethodPut, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterPut("/user", (*ExampleController).PingPointer) handler.CtrlPut("/user", (*ExampleController).PingPointer)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != examplePointerBody { if w.Body.String() != examplePointerBody {
t.Errorf("TestRouterRouterPutPointerMethod can't run") t.Errorf("TestRouterCtrlPutPointerMethod can't run")
} }
} }
func TestRouterRouterPatchPointerMethod(t *testing.T) { func TestRouterCtrlPatchPointerMethod(t *testing.T) {
r, _ := http.NewRequest(http.MethodPatch, "/user", nil) r, _ := http.NewRequest(http.MethodPatch, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterPatch("/user", (*ExampleController).PingPointer) handler.CtrlPatch("/user", (*ExampleController).PingPointer)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != examplePointerBody { if w.Body.String() != examplePointerBody {
t.Errorf("TestRouterRouterPatchPointerMethod can't run") t.Errorf("TestRouterCtrlPatchPointerMethod can't run")
} }
} }
func TestRouterRouterDeletePointerMethod(t *testing.T) { func TestRouterCtrlDeletePointerMethod(t *testing.T) {
r, _ := http.NewRequest(http.MethodDelete, "/user", nil) r, _ := http.NewRequest(http.MethodDelete, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterDelete("/user", (*ExampleController).PingPointer) handler.CtrlDelete("/user", (*ExampleController).PingPointer)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != examplePointerBody { if w.Body.String() != examplePointerBody {
t.Errorf("TestRouterRouterDeletePointerMethod can't run") t.Errorf("TestRouterCtrlDeletePointerMethod can't run")
} }
} }
func TestRouterRouterAnyPointerMethod(t *testing.T) { func TestRouterCtrlAnyPointerMethod(t *testing.T) {
handler := NewControllerRegister() handler := NewControllerRegister()
handler.RouterAny("/user", (*ExampleController).PingPointer) handler.CtrlAny("/user", (*ExampleController).PingPointer)
for method := range HTTPMETHOD { for method := range HTTPMETHOD {
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest(method, "/user", nil) r, _ := http.NewRequest(method, "/user", nil)
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != examplePointerBody { if w.Body.String() != examplePointerBody {
t.Errorf("TestRouterRouterAnyPointerMethod can't run, get the response is " + w.Body.String()) t.Errorf("TestRouterCtrlAnyPointerMethod can't run, get the response is " + w.Body.String())
} }
} }
} }

View File

@ -458,12 +458,12 @@ func (app *HttpServer) AutoPrefix(prefix string, c ControllerInterface) *HttpSer
return app return app
} }
// RouterGet see HttpServer.RouterGet // CtrlGet see HttpServer.CtrlGet
func RouterGet(rootpath string, f interface{}) { func CtrlGet(rootpath string, f interface{}) {
BeeApp.RouterGet(rootpath, f) BeeApp.CtrlGet(rootpath, f)
} }
// RouterGet used to register router for RouterGet method // CtrlGet used to register router for CtrlGet method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -472,18 +472,18 @@ func RouterGet(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterGet("/api/:id", MyController.Ping) // CtrlGet("/api/:id", MyController.Ping)
func (app *HttpServer) RouterGet(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlGet(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterGet(rootpath, f) app.Handlers.CtrlGet(rootpath, f)
return app return app
} }
// RouterPost see HttpServer.RouterGet // CtrlPost see HttpServer.CtrlGet
func RouterPost(rootpath string, f interface{}) { func CtrlPost(rootpath string, f interface{}) {
BeeApp.RouterPost(rootpath, f) BeeApp.CtrlPost(rootpath, f)
} }
// RouterPost used to register router for RouterPost method // CtrlPost used to register router for CtrlPost method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -492,18 +492,18 @@ func RouterPost(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterPost("/api/:id", MyController.Ping) // CtrlPost("/api/:id", MyController.Ping)
func (app *HttpServer) RouterPost(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlPost(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterPost(rootpath, f) app.Handlers.CtrlPost(rootpath, f)
return app return app
} }
// RouterHead see HttpServer.RouterHead // CtrlHead see HttpServer.CtrlHead
func RouterHead(rootpath string, f interface{}) { func CtrlHead(rootpath string, f interface{}) {
BeeApp.RouterHead(rootpath, f) BeeApp.CtrlHead(rootpath, f)
} }
// RouterHead used to register router for RouterHead method // CtrlHead used to register router for CtrlHead method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -512,18 +512,18 @@ func RouterHead(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterHead("/api/:id", MyController.Ping) // CtrlHead("/api/:id", MyController.Ping)
func (app *HttpServer) RouterHead(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlHead(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterHead(rootpath, f) app.Handlers.CtrlHead(rootpath, f)
return app return app
} }
// RouterPut see HttpServer.RouterPut // CtrlPut see HttpServer.CtrlPut
func RouterPut(rootpath string, f interface{}) { func CtrlPut(rootpath string, f interface{}) {
BeeApp.RouterPut(rootpath, f) BeeApp.CtrlPut(rootpath, f)
} }
// RouterPut used to register router for RouterPut method // CtrlPut used to register router for CtrlPut method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -532,18 +532,18 @@ func RouterPut(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterPut("/api/:id", MyController.Ping) // CtrlPut("/api/:id", MyController.Ping)
func (app *HttpServer) RouterPut(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlPut(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterPut(rootpath, f) app.Handlers.CtrlPut(rootpath, f)
return app return app
} }
// RouterPatch see HttpServer.RouterPatch // CtrlPatch see HttpServer.CtrlPatch
func RouterPatch(rootpath string, f interface{}) { func CtrlPatch(rootpath string, f interface{}) {
BeeApp.RouterPatch(rootpath, f) BeeApp.CtrlPatch(rootpath, f)
} }
// RouterPatch used to register router for RouterPatch method // CtrlPatch used to register router for CtrlPatch method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -552,18 +552,18 @@ func RouterPatch(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterPatch("/api/:id", MyController.Ping) // CtrlPatch("/api/:id", MyController.Ping)
func (app *HttpServer) RouterPatch(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlPatch(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterPatch(rootpath, f) app.Handlers.CtrlPatch(rootpath, f)
return app return app
} }
// RouterDelete see HttpServer.RouterDelete // CtrlDelete see HttpServer.CtrlDelete
func RouterDelete(rootpath string, f interface{}) { func CtrlDelete(rootpath string, f interface{}) {
BeeApp.RouterDelete(rootpath, f) BeeApp.CtrlDelete(rootpath, f)
} }
// RouterDelete used to register router for RouterDelete method // CtrlDelete used to register router for CtrlDelete method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -572,18 +572,18 @@ func RouterDelete(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterDelete("/api/:id", MyController.Ping) // CtrlDelete("/api/:id", MyController.Ping)
func (app *HttpServer) RouterDelete(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlDelete(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterDelete(rootpath, f) app.Handlers.CtrlDelete(rootpath, f)
return app return app
} }
// RouterOptions see HttpServer.RouterOptions // CtrlOptions see HttpServer.CtrlOptions
func RouterOptions(rootpath string, f interface{}) { func CtrlOptions(rootpath string, f interface{}) {
BeeApp.RouterOptions(rootpath, f) BeeApp.CtrlOptions(rootpath, f)
} }
// RouterOptions used to register router for RouterOptions method // CtrlOptions used to register router for CtrlOptions method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -592,18 +592,18 @@ func RouterOptions(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterOptions("/api/:id", MyController.Ping) // CtrlOptions("/api/:id", MyController.Ping)
func (app *HttpServer) RouterOptions(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlOptions(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterOptions(rootpath, f) app.Handlers.CtrlOptions(rootpath, f)
return app return app
} }
// RouterAny see HttpServer.RouterAny // CtrlAny see HttpServer.CtrlAny
func RouterAny(rootpath string, f interface{}) { func CtrlAny(rootpath string, f interface{}) {
BeeApp.RouterAny(rootpath, f) BeeApp.CtrlAny(rootpath, f)
} }
// RouterAny used to register router for RouterAny method // CtrlAny used to register router for CtrlAny method
// usage: // usage:
// type MyController struct { // type MyController struct {
// web.Controller // web.Controller
@ -612,9 +612,9 @@ func RouterAny(rootpath string, f interface{}) {
// m.Ctx.Output.Body([]byte("hello world")) // m.Ctx.Output.Body([]byte("hello world"))
// } // }
// //
// RouterAny("/api/:id", MyController.Ping) // CtrlAny("/api/:id", MyController.Ping)
func (app *HttpServer) RouterAny(rootpath string, f interface{}) *HttpServer { func (app *HttpServer) CtrlAny(rootpath string, f interface{}) *HttpServer {
app.Handlers.RouterAny(rootpath, f) app.Handlers.CtrlAny(rootpath, f)
return app return app
} }

View File

@ -29,81 +29,81 @@ func TestNewHttpServerWithCfg(t *testing.T) {
assert.Equal(t, "hello", BConfig.AppName) assert.Equal(t, "hello", BConfig.AppName)
} }
func TestServerRouterGet(t *testing.T) { func TestServerCtrlGet(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/user", nil) r, _ := http.NewRequest(http.MethodGet, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
RouterGet("/user", ExampleController.Ping) CtrlGet("/user", ExampleController.Ping)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestServerRouterGet can't run") t.Errorf("TestServerCtrlGet can't run")
} }
} }
func TestServerRouterPost(t *testing.T) { func TestServerCtrlPost(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, "/user", nil) r, _ := http.NewRequest(http.MethodPost, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
RouterPost("/user", ExampleController.Ping) CtrlPost("/user", ExampleController.Ping)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestServerRouterPost can't run") t.Errorf("TestServerCtrlPost can't run")
} }
} }
func TestServerRouterHead(t *testing.T) { func TestServerCtrlHead(t *testing.T) {
r, _ := http.NewRequest(http.MethodHead, "/user", nil) r, _ := http.NewRequest(http.MethodHead, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
RouterHead("/user", ExampleController.Ping) CtrlHead("/user", ExampleController.Ping)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestServerRouterHead can't run") t.Errorf("TestServerCtrlHead can't run")
} }
} }
func TestServerRouterPut(t *testing.T) { func TestServerCtrlPut(t *testing.T) {
r, _ := http.NewRequest(http.MethodPut, "/user", nil) r, _ := http.NewRequest(http.MethodPut, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
RouterPut("/user", ExampleController.Ping) CtrlPut("/user", ExampleController.Ping)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestServerRouterPut can't run") t.Errorf("TestServerCtrlPut can't run")
} }
} }
func TestServerRouterPatch(t *testing.T) { func TestServerCtrlPatch(t *testing.T) {
r, _ := http.NewRequest(http.MethodPatch, "/user", nil) r, _ := http.NewRequest(http.MethodPatch, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
RouterPatch("/user", ExampleController.Ping) CtrlPatch("/user", ExampleController.Ping)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestServerRouterPatch can't run") t.Errorf("TestServerCtrlPatch can't run")
} }
} }
func TestServerRouterDelete(t *testing.T) { func TestServerCtrlDelete(t *testing.T) {
r, _ := http.NewRequest(http.MethodDelete, "/user", nil) r, _ := http.NewRequest(http.MethodDelete, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
RouterDelete("/user", ExampleController.Ping) CtrlDelete("/user", ExampleController.Ping)
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestServerRouterDelete can't run") t.Errorf("TestServerCtrlDelete can't run")
} }
} }
func TestServerRouterAny(t *testing.T) { func TestServerCtrlAny(t *testing.T) {
RouterAny("/user", ExampleController.Ping) CtrlAny("/user", ExampleController.Ping)
for method := range HTTPMETHOD { for method := range HTTPMETHOD {
r, _ := http.NewRequest(method, "/user", nil) r, _ := http.NewRequest(method, "/user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
BeeApp.Handlers.ServeHTTP(w, r) BeeApp.Handlers.ServeHTTP(w, r)
if w.Body.String() != exampleBody { if w.Body.String() != exampleBody {
t.Errorf("TestServerRouterAny can't run") t.Errorf("TestServerCtrlAny can't run")
} }
} }
} }