From 32d7633a0443b3a7a2299b8bb74bfdb4ed302253 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Wed, 4 Aug 2021 21:47:47 +0800 Subject: [PATCH] Rename RouterXXX to CtrlXXX --- server/web/controller.go | 15 ++++ server/web/filter.go | 4 +- server/web/namespace.go | 128 +++++++++++++++++------------------ server/web/namespace_test.go | 96 +++++++++++++------------- server/web/router.go | 77 +++++++++++---------- server/web/router_test.go | 92 ++++++++++++------------- server/web/server.go | 112 +++++++++++++++--------------- server/web/server_test.go | 42 ++++++------ 8 files changed, 295 insertions(+), 271 deletions(-) diff --git a/server/web/controller.go b/server/web/controller.go index 700bd03e..8db9e775 100644 --- a/server/web/controller.go +++ b/server/web/controller.go @@ -439,6 +439,21 @@ func (c *Controller) URLFor(endpoint string, values ...interface{}) string { 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 // By default response will be in JSON func (c *Controller) Resp(data interface{}) error { diff --git a/server/web/filter.go b/server/web/filter.go index 0baa269f..e989f620 100644 --- a/server/web/filter.go +++ b/server/web/filter.go @@ -26,7 +26,9 @@ import ( type FilterChain func(next FilterFunc) FilterFunc // 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. // It can match the URL against a pattern, and execute a filter function diff --git a/server/web/namespace.go b/server/web/namespace.go index 96037b4d..825aa4b8 100644 --- a/server/web/namespace.go +++ b/server/web/namespace.go @@ -119,56 +119,56 @@ func (n *Namespace) AutoPrefix(prefix string, c ControllerInterface) *Namespace // Get same as beego.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) return n } // Post same as beego.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) return n } // Delete same as beego.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) return n } // Put same as beego.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) return n } // Head same as beego.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) return n } // Options same as beego.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) return n } // Patch same as beego.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) return n } // Any same as beego.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) return n } @@ -187,51 +187,51 @@ func (n *Namespace) Include(cList ...ControllerInterface) *Namespace { return n } -// RouterGet same as beego.RouterGet -func (n *Namespace) RouterGet(rootpath string, f interface{}) *Namespace { - n.handlers.RouterGet(rootpath, f) +// CtrlGet same as beego.CtrlGet +func (n *Namespace) CtrlGet(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlGet(rootpath, f) return n } -// RouterPost same as beego.RouterPost -func (n *Namespace) RouterPost(rootpath string, f interface{}) *Namespace { - n.handlers.RouterPost(rootpath, f) +// CtrlPost same as beego.CtrlPost +func (n *Namespace) CtrlPost(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlPost(rootpath, f) return n } -// RouterDelete same as beego.RouterDelete -func (n *Namespace) RouterDelete(rootpath string, f interface{}) *Namespace { - n.handlers.RouterDelete(rootpath, f) +// CtrlDelete same as beego.CtrlDelete +func (n *Namespace) CtrlDelete(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlDelete(rootpath, f) return n } -// RouterPut same as beego.RouterPut -func (n *Namespace) RouterPut(rootpath string, f interface{}) *Namespace { - n.handlers.RouterPut(rootpath, f) +// CtrlPut same as beego.CtrlPut +func (n *Namespace) CtrlPut(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlPut(rootpath, f) return n } -// RouterHead same as beego.RouterHead -func (n *Namespace) RouterHead(rootpath string, f interface{}) *Namespace { - n.handlers.RouterHead(rootpath, f) +// CtrlHead same as beego.CtrlHead +func (n *Namespace) CtrlHead(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlHead(rootpath, f) return n } -// RouterOptions same as beego.RouterOptions -func (n *Namespace) RouterOptions(rootpath string, f interface{}) *Namespace { - n.handlers.RouterOptions(rootpath, f) +// CtrlOptions same as beego.CtrlOptions +func (n *Namespace) CtrlOptions(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlOptions(rootpath, f) return n } -// RouterPatch same as beego.RouterPatch -func (n *Namespace) RouterPatch(rootpath string, f interface{}) *Namespace { - n.handlers.RouterPatch(rootpath, f) +// CtrlPatch same as beego.CtrlPatch +func (n *Namespace) CtrlPatch(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlPatch(rootpath, f) return n } -// Any same as beego.RouterAny -func (n *Namespace) RouterAny(rootpath string, f interface{}) *Namespace { - n.handlers.RouterAny(rootpath, f) +// Any same as beego.CtrlAny +func (n *Namespace) CtrlAny(rootpath string, f interface{}) *Namespace { + n.handlers.CtrlAny(rootpath, f) return n } @@ -359,114 +359,114 @@ func NSRouter(rootpath string, c ControllerInterface, mappingMethods ...string) } // NSGet call Namespace Get -func NSGet(rootpath string, f FilterFunc) LinkNamespace { +func NSGet(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Get(rootpath, f) } } // NSPost call Namespace Post -func NSPost(rootpath string, f FilterFunc) LinkNamespace { +func NSPost(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Post(rootpath, f) } } // NSHead call Namespace Head -func NSHead(rootpath string, f FilterFunc) LinkNamespace { +func NSHead(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Head(rootpath, f) } } // NSPut call Namespace Put -func NSPut(rootpath string, f FilterFunc) LinkNamespace { +func NSPut(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Put(rootpath, f) } } // NSDelete call Namespace Delete -func NSDelete(rootpath string, f FilterFunc) LinkNamespace { +func NSDelete(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Delete(rootpath, f) } } // NSAny call Namespace Any -func NSAny(rootpath string, f FilterFunc) LinkNamespace { +func NSAny(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Any(rootpath, f) } } // NSOptions call Namespace Options -func NSOptions(rootpath string, f FilterFunc) LinkNamespace { +func NSOptions(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Options(rootpath, f) } } // NSPatch call Namespace Patch -func NSPatch(rootpath string, f FilterFunc) LinkNamespace { +func NSPatch(rootpath string, f HandleFunc) LinkNamespace { return func(ns *Namespace) { ns.Patch(rootpath, f) } } -// NSRouterGet call Namespace RouterGet -func NSRouterGet(rootpath string, f interface{}) LinkNamespace { +// NSCtrlGet call Namespace CtrlGet +func NSCtrlGet(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterGet(rootpath, f) + ns.CtrlGet(rootpath, f) } } -// NSRouterPost call Namespace RouterPost -func NSRouterPost(rootpath string, f interface{}) LinkNamespace { +// NSCtrlPost call Namespace CtrlPost +func NSCtrlPost(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterPost(rootpath, f) + ns.CtrlPost(rootpath, f) } } -// NSRouterHead call Namespace RouterHead -func NSRouterHead(rootpath string, f interface{}) LinkNamespace { +// NSCtrlHead call Namespace CtrlHead +func NSCtrlHead(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterHead(rootpath, f) + ns.CtrlHead(rootpath, f) } } -// NSRouterPut call Namespace RouterPut -func NSRouterPut(rootpath string, f interface{}) LinkNamespace { +// NSCtrlPut call Namespace CtrlPut +func NSCtrlPut(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterPut(rootpath, f) + ns.CtrlPut(rootpath, f) } } -// NSRouterDelete call Namespace RouterDelete -func NSRouterDelete(rootpath string, f interface{}) LinkNamespace { +// NSCtrlDelete call Namespace CtrlDelete +func NSCtrlDelete(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterDelete(rootpath, f) + ns.CtrlDelete(rootpath, f) } } -// NSRouterAny call Namespace RouterAny -func NSRouterAny(rootpath string, f interface{}) LinkNamespace { +// NSCtrlAny call Namespace CtrlAny +func NSCtrlAny(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterAny(rootpath, f) + ns.CtrlAny(rootpath, f) } } -// NSRouterOptions call Namespace RouterOptions -func NSRouterOptions(rootpath string, f interface{}) LinkNamespace { +// NSCtrlOptions call Namespace CtrlOptions +func NSCtrlOptions(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterOptions(rootpath, f) + ns.CtrlOptions(rootpath, f) } } -// NSRouterPatch call Namespace RouterPatch -func NSRouterPatch(rootpath string, f interface{}) LinkNamespace { +// NSCtrlPatch call Namespace CtrlPatch +func NSCtrlPatch(rootpath string, f interface{}) LinkNamespace { return func(ns *Namespace) { - ns.RouterPatch(rootpath, f) + ns.CtrlPatch(rootpath, f) } } diff --git a/server/web/namespace_test.go b/server/web/namespace_test.go index 30d17cb2..e0e15d6f 100644 --- a/server/web/namespace_test.go +++ b/server/web/namespace_test.go @@ -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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - ns.RouterGet(nsPath, ExampleController.Ping) + ns.CtrlGet(nsPath, ExampleController.Ping) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - ns.RouterPost(nsPath, ExampleController.Ping) + ns.CtrlPost(nsPath, ExampleController.Ping) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - ns.RouterDelete(nsPath, ExampleController.Ping) + ns.CtrlDelete(nsPath, ExampleController.Ping) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - ns.RouterPut(nsPath, ExampleController.Ping) + ns.CtrlPut(nsPath, ExampleController.Ping) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - ns.RouterHead(nsPath, ExampleController.Ping) + ns.CtrlHead(nsPath, ExampleController.Ping) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - ns.RouterOptions(nsPath, ExampleController.Ping) + ns.CtrlOptions(nsPath, ExampleController.Ping) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - ns.RouterPatch(nsPath, ExampleController.Ping) + ns.CtrlPatch(nsPath, ExampleController.Ping) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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.RouterAny(nsPath, ExampleController.Ping) + ns.CtrlAny(nsPath, ExampleController.Ping) AddNamespace(ns) for method := range HTTPMETHOD { @@ -303,105 +303,105 @@ func TestNamespaceRouterAny(t *testing.T) { r, _ := http.NewRequest(method, nsNamespacePath, nil) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - NSRouterGet(nsPath, ExampleController.Ping)(ns) + NSCtrlGet(nsPath, ExampleController.Ping)(ns) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace("/router") - NSRouterPost(nsPath, ExampleController.Ping)(ns) + NSCtrlPost(nsPath, ExampleController.Ping)(ns) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - NSRouterDelete(nsPath, ExampleController.Ping)(ns) + NSCtrlDelete(nsPath, ExampleController.Ping)(ns) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - NSRouterPut(nsPath, ExampleController.Ping)(ns) + NSCtrlPut(nsPath, ExampleController.Ping)(ns) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - NSRouterHead(nsPath, ExampleController.Ping)(ns) + NSCtrlHead(nsPath, ExampleController.Ping)(ns) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - NSRouterOptions(nsPath, ExampleController.Ping)(ns) + NSCtrlOptions(nsPath, ExampleController.Ping)(ns) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() ns := NewNamespace(nsNamespace) - NSRouterPatch("/user", ExampleController.Ping)(ns) + NSCtrlPatch("/user", ExampleController.Ping)(ns) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) 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) - NSRouterAny(nsPath, ExampleController.Ping)(ns) + NSCtrlAny(nsPath, ExampleController.Ping)(ns) AddNamespace(ns) for method := range HTTPMETHOD { @@ -409,7 +409,7 @@ func TestNamespaceNSRouterAny(t *testing.T) { r, _ := http.NewRequest(method, nsNamespacePath, nil) BeeApp.Handlers.ServeHTTP(w, r) 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()) } } } diff --git a/server/web/router.go b/server/web/router.go index 837fd93b..35bc506f 100644 --- a/server/web/router.go +++ b/server/web/router.go @@ -119,7 +119,7 @@ type ControllerInfo struct { controllerType reflect.Type methods map[string]string handler http.Handler - runFunction FilterFunc + runFunction HandleFunc routerType int initialize func() ControllerInterface methodParams []*param.MethodParam @@ -354,7 +354,7 @@ func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) { p.pool.Put(ctx) } -// RouterGet add get method +// CtrlGet add get method // usage: // type MyController struct { // web.Controller @@ -363,12 +363,13 @@ func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterGet("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterGet(pattern string, f interface{}) { +// CtrlGet("/api/:id", MyController.Ping) +// 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) } -// RouterPost add post method +// CtrlPost add post method // usage: // type MyController struct { // web.Controller @@ -377,12 +378,13 @@ func (p *ControllerRegister) RouterGet(pattern string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterPost("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterPost(pattern string, f interface{}) { +// CtrlPost("/api/:id", MyController.Ping) +// 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) } -// RouterHead add head method +// CtrlHead add head method // usage: // type MyController struct { // web.Controller @@ -391,12 +393,13 @@ func (p *ControllerRegister) RouterPost(pattern string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterHead("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterHead(pattern string, f interface{}) { +// CtrlHead("/api/:id", MyController.Ping) +// 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) } -// RouterPut add put method +// CtrlPut add put method // usage: // type MyController struct { // web.Controller @@ -405,12 +408,13 @@ func (p *ControllerRegister) RouterHead(pattern string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterPut("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterPut(pattern string, f interface{}) { +// CtrlPut("/api/:id", MyController.Ping) + +func (p *ControllerRegister) CtrlPut(pattern string, f interface{}) { p.AddRouterMethod(http.MethodPut, pattern, f) } -// RouterPatch add patch method +// CtrlPatch add patch method // usage: // type MyController struct { // web.Controller @@ -419,12 +423,12 @@ func (p *ControllerRegister) RouterPut(pattern string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterPatch("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterPatch(pattern string, f interface{}) { +// CtrlPatch("/api/:id", MyController.Ping) +func (p *ControllerRegister) CtrlPatch(pattern string, f interface{}) { p.AddRouterMethod(http.MethodPatch, pattern, f) } -// RouterDelete add delete method +// CtrlDelete add delete method // usage: // type MyController struct { // web.Controller @@ -433,12 +437,12 @@ func (p *ControllerRegister) RouterPatch(pattern string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterDelete("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterDelete(pattern string, f interface{}) { +// CtrlDelete("/api/:id", MyController.Ping) +func (p *ControllerRegister) CtrlDelete(pattern string, f interface{}) { p.AddRouterMethod(http.MethodDelete, pattern, f) } -// RouterOptions add options method +// CtrlOptions add options method // usage: // type MyController struct { // web.Controller @@ -447,12 +451,12 @@ func (p *ControllerRegister) RouterDelete(pattern string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterOptions("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterOptions(pattern string, f interface{}) { +// CtrlOptions("/api/:id", MyController.Ping) +func (p *ControllerRegister) CtrlOptions(pattern string, f interface{}) { p.AddRouterMethod(http.MethodOptions, pattern, f) } -// RouterAny add all method +// CtrlAny add all method // usage: // type MyController struct { // web.Controller @@ -461,8 +465,8 @@ func (p *ControllerRegister) RouterOptions(pattern string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterAny("/api/:id", MyController.Ping) -func (p *ControllerRegister) RouterAny(pattern string, f interface{}) { +// CtrlAny("/api/:id", MyController.Ping) +func (p *ControllerRegister) CtrlAny(pattern string, f interface{}) { 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 -func (p *ControllerRegister) createRestfulRouter(f FilterFunc, pattern string) *ControllerInfo { +func (p *ControllerRegister) createRestfulRouter(f HandleFunc, pattern string) *ControllerInfo { route := &ControllerInfo{} route.pattern = pattern route.routerType = routerTypeRESTFul @@ -609,12 +613,15 @@ func getReflectTypeAndMethod(f interface{}) (controllerType reflect.Type, method return } +// HandleFunc define how to process the request +type HandleFunc func(ctx *beecontext.Context) + // Get add get method // usage: // Get("/", func(ctx *context.Context){ // 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) } @@ -623,7 +630,7 @@ func (p *ControllerRegister) Get(pattern string, f FilterFunc) { // Post("/api", func(ctx *context.Context){ // 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) } @@ -632,7 +639,7 @@ func (p *ControllerRegister) Post(pattern string, f FilterFunc) { // Put("/api/:id", func(ctx *context.Context){ // 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) } @@ -641,7 +648,7 @@ func (p *ControllerRegister) Put(pattern string, f FilterFunc) { // Delete("/api/:id", func(ctx *context.Context){ // 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) } @@ -650,7 +657,7 @@ func (p *ControllerRegister) Delete(pattern string, f FilterFunc) { // Head("/api/:id", func(ctx *context.Context){ // 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) } @@ -659,7 +666,7 @@ func (p *ControllerRegister) Head(pattern string, f FilterFunc) { // Patch("/api/:id", func(ctx *context.Context){ // 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) } @@ -668,7 +675,7 @@ func (p *ControllerRegister) Patch(pattern string, f FilterFunc) { // Options("/api/:id", func(ctx *context.Context){ // 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) } @@ -677,7 +684,7 @@ func (p *ControllerRegister) Options(pattern string, f FilterFunc) { // Any("/api/:id", func(ctx *context.Context){ // 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) } @@ -686,7 +693,7 @@ func (p *ControllerRegister) Any(pattern string, f FilterFunc) { // AddMethod("get","/api/:id", func(ctx *context.Context){ // 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) route := p.createRestfulRouter(f, pattern) diff --git a/server/web/router_test.go b/server/web/router_test.go index 6840829c..5009d24e 100644 --- a/server/web/router_test.go +++ b/server/web/router_test.go @@ -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) w := httptest.NewRecorder() @@ -364,11 +364,11 @@ func TestRouterGet(t *testing.T) { }) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() @@ -378,7 +378,7 @@ func TestRouterPost(t *testing.T) { }) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterGet("/user", ExampleController.Ping) + handler.CtrlGet("/user", ExampleController.Ping) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterPost("/user", ExampleController.Ping) + handler.CtrlPost("/user", ExampleController.Ping) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterHead("/user", ExampleController.Ping) + handler.CtrlHead("/user", ExampleController.Ping) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterPut("/user", ExampleController.Ping) + handler.CtrlPut("/user", ExampleController.Ping) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterPatch("/user", ExampleController.Ping) + handler.CtrlPatch("/user", ExampleController.Ping) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterDelete("/user", ExampleController.Ping) + handler.CtrlDelete("/user", ExampleController.Ping) handler.ServeHTTP(w, r) 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.RouterAny("/user", ExampleController.Ping) + handler.CtrlAny("/user", ExampleController.Ping) for method := range HTTPMETHOD { w := httptest.NewRecorder() r, _ := http.NewRequest(method, "/user", nil) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterGet("/user", (*ExampleController).PingPointer) + handler.CtrlGet("/user", (*ExampleController).PingPointer) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterPost("/user", (*ExampleController).PingPointer) + handler.CtrlPost("/user", (*ExampleController).PingPointer) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterHead("/user", (*ExampleController).PingPointer) + handler.CtrlHead("/user", (*ExampleController).PingPointer) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterPut("/user", (*ExampleController).PingPointer) + handler.CtrlPut("/user", (*ExampleController).PingPointer) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterPatch("/user", (*ExampleController).PingPointer) + handler.CtrlPatch("/user", (*ExampleController).PingPointer) handler.ServeHTTP(w, r) 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) w := httptest.NewRecorder() handler := NewControllerRegister() - handler.RouterDelete("/user", (*ExampleController).PingPointer) + handler.CtrlDelete("/user", (*ExampleController).PingPointer) handler.ServeHTTP(w, r) 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.RouterAny("/user", (*ExampleController).PingPointer) + handler.CtrlAny("/user", (*ExampleController).PingPointer) for method := range HTTPMETHOD { w := httptest.NewRecorder() r, _ := http.NewRequest(method, "/user", nil) handler.ServeHTTP(w, r) 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()) } } } diff --git a/server/web/server.go b/server/web/server.go index 8abaf047..ae49dd25 100644 --- a/server/web/server.go +++ b/server/web/server.go @@ -458,12 +458,12 @@ func (app *HttpServer) AutoPrefix(prefix string, c ControllerInterface) *HttpSer return app } -// RouterGet see HttpServer.RouterGet -func RouterGet(rootpath string, f interface{}) { - BeeApp.RouterGet(rootpath, f) +// CtrlGet see HttpServer.CtrlGet +func CtrlGet(rootpath string, f interface{}) { + BeeApp.CtrlGet(rootpath, f) } -// RouterGet used to register router for RouterGet method +// CtrlGet used to register router for CtrlGet method // usage: // type MyController struct { // web.Controller @@ -472,18 +472,18 @@ func RouterGet(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterGet("/api/:id", MyController.Ping) -func (app *HttpServer) RouterGet(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterGet(rootpath, f) +// CtrlGet("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlGet(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlGet(rootpath, f) return app } -// RouterPost see HttpServer.RouterGet -func RouterPost(rootpath string, f interface{}) { - BeeApp.RouterPost(rootpath, f) +// CtrlPost see HttpServer.CtrlGet +func CtrlPost(rootpath string, f interface{}) { + BeeApp.CtrlPost(rootpath, f) } -// RouterPost used to register router for RouterPost method +// CtrlPost used to register router for CtrlPost method // usage: // type MyController struct { // web.Controller @@ -492,18 +492,18 @@ func RouterPost(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterPost("/api/:id", MyController.Ping) -func (app *HttpServer) RouterPost(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterPost(rootpath, f) +// CtrlPost("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlPost(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlPost(rootpath, f) return app } -// RouterHead see HttpServer.RouterHead -func RouterHead(rootpath string, f interface{}) { - BeeApp.RouterHead(rootpath, f) +// CtrlHead see HttpServer.CtrlHead +func CtrlHead(rootpath string, f interface{}) { + BeeApp.CtrlHead(rootpath, f) } -// RouterHead used to register router for RouterHead method +// CtrlHead used to register router for CtrlHead method // usage: // type MyController struct { // web.Controller @@ -512,18 +512,18 @@ func RouterHead(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterHead("/api/:id", MyController.Ping) -func (app *HttpServer) RouterHead(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterHead(rootpath, f) +// CtrlHead("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlHead(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlHead(rootpath, f) return app } -// RouterPut see HttpServer.RouterPut -func RouterPut(rootpath string, f interface{}) { - BeeApp.RouterPut(rootpath, f) +// CtrlPut see HttpServer.CtrlPut +func CtrlPut(rootpath string, f interface{}) { + BeeApp.CtrlPut(rootpath, f) } -// RouterPut used to register router for RouterPut method +// CtrlPut used to register router for CtrlPut method // usage: // type MyController struct { // web.Controller @@ -532,18 +532,18 @@ func RouterPut(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterPut("/api/:id", MyController.Ping) -func (app *HttpServer) RouterPut(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterPut(rootpath, f) +// CtrlPut("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlPut(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlPut(rootpath, f) return app } -// RouterPatch see HttpServer.RouterPatch -func RouterPatch(rootpath string, f interface{}) { - BeeApp.RouterPatch(rootpath, f) +// CtrlPatch see HttpServer.CtrlPatch +func CtrlPatch(rootpath string, f interface{}) { + BeeApp.CtrlPatch(rootpath, f) } -// RouterPatch used to register router for RouterPatch method +// CtrlPatch used to register router for CtrlPatch method // usage: // type MyController struct { // web.Controller @@ -552,18 +552,18 @@ func RouterPatch(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterPatch("/api/:id", MyController.Ping) -func (app *HttpServer) RouterPatch(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterPatch(rootpath, f) +// CtrlPatch("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlPatch(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlPatch(rootpath, f) return app } -// RouterDelete see HttpServer.RouterDelete -func RouterDelete(rootpath string, f interface{}) { - BeeApp.RouterDelete(rootpath, f) +// CtrlDelete see HttpServer.CtrlDelete +func CtrlDelete(rootpath string, f interface{}) { + BeeApp.CtrlDelete(rootpath, f) } -// RouterDelete used to register router for RouterDelete method +// CtrlDelete used to register router for CtrlDelete method // usage: // type MyController struct { // web.Controller @@ -572,18 +572,18 @@ func RouterDelete(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterDelete("/api/:id", MyController.Ping) -func (app *HttpServer) RouterDelete(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterDelete(rootpath, f) +// CtrlDelete("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlDelete(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlDelete(rootpath, f) return app } -// RouterOptions see HttpServer.RouterOptions -func RouterOptions(rootpath string, f interface{}) { - BeeApp.RouterOptions(rootpath, f) +// CtrlOptions see HttpServer.CtrlOptions +func CtrlOptions(rootpath string, f interface{}) { + BeeApp.CtrlOptions(rootpath, f) } -// RouterOptions used to register router for RouterOptions method +// CtrlOptions used to register router for CtrlOptions method // usage: // type MyController struct { // web.Controller @@ -592,18 +592,18 @@ func RouterOptions(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterOptions("/api/:id", MyController.Ping) -func (app *HttpServer) RouterOptions(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterOptions(rootpath, f) +// CtrlOptions("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlOptions(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlOptions(rootpath, f) return app } -// RouterAny see HttpServer.RouterAny -func RouterAny(rootpath string, f interface{}) { - BeeApp.RouterAny(rootpath, f) +// CtrlAny see HttpServer.CtrlAny +func CtrlAny(rootpath string, f interface{}) { + BeeApp.CtrlAny(rootpath, f) } -// RouterAny used to register router for RouterAny method +// CtrlAny used to register router for CtrlAny method // usage: // type MyController struct { // web.Controller @@ -612,9 +612,9 @@ func RouterAny(rootpath string, f interface{}) { // m.Ctx.Output.Body([]byte("hello world")) // } // -// RouterAny("/api/:id", MyController.Ping) -func (app *HttpServer) RouterAny(rootpath string, f interface{}) *HttpServer { - app.Handlers.RouterAny(rootpath, f) +// CtrlAny("/api/:id", MyController.Ping) +func (app *HttpServer) CtrlAny(rootpath string, f interface{}) *HttpServer { + app.Handlers.CtrlAny(rootpath, f) return app } diff --git a/server/web/server_test.go b/server/web/server_test.go index ed214e75..1ddf217e 100644 --- a/server/web/server_test.go +++ b/server/web/server_test.go @@ -29,81 +29,81 @@ func TestNewHttpServerWithCfg(t *testing.T) { assert.Equal(t, "hello", BConfig.AppName) } -func TestServerRouterGet(t *testing.T) { +func TestServerCtrlGet(t *testing.T) { r, _ := http.NewRequest(http.MethodGet, "/user", nil) w := httptest.NewRecorder() - RouterGet("/user", ExampleController.Ping) + CtrlGet("/user", ExampleController.Ping) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() - RouterPost("/user", ExampleController.Ping) + CtrlPost("/user", ExampleController.Ping) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() - RouterHead("/user", ExampleController.Ping) + CtrlHead("/user", ExampleController.Ping) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() - RouterPut("/user", ExampleController.Ping) + CtrlPut("/user", ExampleController.Ping) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() - RouterPatch("/user", ExampleController.Ping) + CtrlPatch("/user", ExampleController.Ping) BeeApp.Handlers.ServeHTTP(w, r) 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) w := httptest.NewRecorder() - RouterDelete("/user", ExampleController.Ping) + CtrlDelete("/user", ExampleController.Ping) BeeApp.Handlers.ServeHTTP(w, r) if w.Body.String() != exampleBody { - t.Errorf("TestServerRouterDelete can't run") + t.Errorf("TestServerCtrlDelete can't run") } } -func TestServerRouterAny(t *testing.T) { - RouterAny("/user", ExampleController.Ping) +func TestServerCtrlAny(t *testing.T) { + CtrlAny("/user", ExampleController.Ping) for method := range HTTPMETHOD { r, _ := http.NewRequest(method, "/user", nil) w := httptest.NewRecorder() BeeApp.Handlers.ServeHTTP(w, r) if w.Body.String() != exampleBody { - t.Errorf("TestServerRouterAny can't run") + t.Errorf("TestServerCtrlAny can't run") } } }