Merge pull request #4714 from flycash/comments-refactor

Add comments for BConfig, Rename RouterXXX functions and Define HandleFunc
This commit is contained in:
Ming Deng 2021-08-04 23:15:36 +08:00 committed by GitHub
commit bb598b28cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 692 additions and 359 deletions

View File

@ -54,6 +54,7 @@
- Init exceptMethod by using reflection. [4583](https://github.com/beego/beego/pull/4583) - Init exceptMethod by using reflection. [4583](https://github.com/beego/beego/pull/4583)
- Deprecated BeeMap and replace all usage with `sync.map` [4616](https://github.com/beego/beego/pull/4616) - Deprecated BeeMap and replace all usage with `sync.map` [4616](https://github.com/beego/beego/pull/4616)
- TaskManager support graceful shutdown [4635](https://github.com/beego/beego/pull/4635) - TaskManager support graceful shutdown [4635](https://github.com/beego/beego/pull/4635)
- Add comments to `web.Config`, rename `RouterXXX` to `CtrlXXX`, define `HandleFunc` [4714](https://github.com/beego/beego/pull/4714)
## Fix Sonar ## Fix Sonar

View File

@ -35,97 +35,401 @@ import (
// Config is the main struct for BConfig // Config is the main struct for BConfig
// TODO after supporting multiple servers, remove common config to somewhere else // TODO after supporting multiple servers, remove common config to somewhere else
type Config struct { type Config struct {
AppName string // Application name // AppName
RunMode string // Running Mode: dev | prod // @Description Application's name. You'd better set it because we use it to do some logging and tracing
// @Default beego
AppName string // Application name
// RunMode
// @Description it's the same as environment. In general, we have different run modes.
// For example, the most common case is using dev, test, prod three environments
// when you are developing the application, you should set it as dev
// when you completed coding and want QA to test your code, you should deploy your application to test environment
// and the RunMode should be set as test
// when you completed all tests, you want to deploy it to prod, you should set it to prod
// You should never set RunMode="dev" when you deploy the application to prod
// because Beego will do more things which need Go SDK and other tools when it found out the RunMode="dev"
// @Default dev
RunMode string // Running Mode: dev | prod
// RouterCaseSensitive
// @Description If it was true, it means that the router is case sensitive.
// For example, when you register a router with pattern "/hello",
// 1. If this is true, and the request URL is "/Hello", it won't match this pattern
// 2. If this is false and the request URL is "/Hello", it will match this pattern
// @Default true
RouterCaseSensitive bool RouterCaseSensitive bool
RecoverPanic bool // RecoverPanic
CopyRequestBody bool // @Description if it was true, Beego will try to recover from panic when it serves your http request
EnableGzip bool // So you should notice that it doesn't mean that Beego will recover all panic cases.
EnableErrorsShow bool // @Default true
EnableErrorsRender bool RecoverPanic bool
ServerName string // CopyRequestBody
RecoverFunc func(*context.Context, *Config) // @Description if it's true, Beego will copy the request body. But if the request body's size > MaxMemory,
// MaxMemory and MaxUploadSize are used to limit the request body // Beego will return 413 as http status
// If you are building RESTful API, please set it to true.
// And if you want to read data from request Body multiple times, please set it to true
// In general, if you don't meet any performance issue, you could set it to true
// @Default false
CopyRequestBody bool
// EnableGzip
// @Description If it was true, Beego will try to compress data by using zip algorithm.
// But there are two points:
// 1. Only static resources will be compressed
// 2. Only those static resource which has the extension specified by StaticExtensionsToGzip will be compressed
// @Default false
EnableGzip bool
// EnableErrorsShow
// @Description If it's true, Beego will show error message to page
// it will work with ErrorMaps which allows you register some error handler
// You may want to set it to false when application was deploy to prod environment
// because you may not want to expose your internal error msg to your users
// it's a little bit unsafe
// @Default true
EnableErrorsShow bool
// EnableErrorsRender
// @Description If it's true, it will output the error msg as a page. It's similar to EnableErrorsShow
// And this configure item only work in dev run mode (see RunMode)
// @Default true
EnableErrorsRender bool
// ServerName
// @Description server name. For example, in large scale system,
// you may want to deploy your application to several machines, so that each of them has a server name
// we suggest you'd better set value because Beego use this to output some DEBUG msg,
// or integrated with other tools such as tracing, metrics
// @Default
ServerName string
// RecoverFunc
// @Description when Beego want to recover from panic, it will use this func as callback
// see RecoverPanic
// @Default defaultRecoverPanic
RecoverFunc func(*context.Context, *Config)
// @Description MaxMemory and MaxUploadSize are used to limit the request body
// if the request is not uploading file, MaxMemory is the max size of request body // if the request is not uploading file, MaxMemory is the max size of request body
// if the request is uploading file, MaxUploadSize is the max size of request body // if the request is uploading file, MaxUploadSize is the max size of request body
MaxMemory int64 // if CopyRequestBody is true, this value will be used as the threshold of request body
// see CopyRequestBody
// the default value is 1 << 26 (64MB)
// @Default 67108864
MaxMemory int64
// MaxUploadSize
// @Description MaxMemory and MaxUploadSize are used to limit the request body
// if the request is not uploading file, MaxMemory is the max size of request body
// if the request is uploading file, MaxUploadSize is the max size of request body
// the default value is 1 << 30 (1GB)
// @Default 1073741824
MaxUploadSize int64 MaxUploadSize int64
Listen Listen // Listen
WebConfig WebConfig // @Description the configuration about socket or http protocol
Log LogConfig Listen Listen
// WebConfig
// @Description the configuration about Web
WebConfig WebConfig
// LogConfig
// @Description log configuration
Log LogConfig
} }
// Listen holds for http and https related config // Listen holds for http and https related config
type Listen struct { type Listen struct {
Graceful bool // Graceful means use graceful module to start the server // Graceful
ListenTCP4 bool // @Description means use graceful module to start the server
EnableHTTP bool // @Default false
AutoTLS bool Graceful bool
EnableHTTPS bool // ListenTCP4
// @Description if it's true, means that Beego only work for TCP4
// please check net.Listen function
// In general, you should not set it to true
// @Default false
ListenTCP4 bool
// EnableHTTP
// @Description if it's true, Beego will accept HTTP request.
// But if you want to use HTTPS only, please set it to false
// see EnableHTTPS
// @Default true
EnableHTTP bool
// AutoTLS
// @Description If it's true, Beego will use default value to initialize the TLS configure
// But those values could be override if you have custom value.
// see Domains, TLSCacheDir
// @Default false
AutoTLS bool
// EnableHTTPS
// @Description If it's true, Beego will accept HTTPS request.
// Now, you'd better use HTTPS protocol on prod environment to get better security
// In prod, the best option is EnableHTTPS=true and EnableHTTP=false
// see EnableHTTP
// @Default false
EnableHTTPS bool
// EnableMutualHTTPS
// @Description if it's true, Beego will handle requests on incoming mutual TLS connections
// see Server.ListenAndServeMutualTLS
// @Default false
EnableMutualHTTPS bool EnableMutualHTTPS bool
EnableAdmin bool // EnableAdmin
EnableFcgi bool // @Description if it's true, Beego will provide admin service.
EnableStdIo bool // EnableStdIo works with EnableFcgi Use FCGI via standard I/O // You can visit the admin service via browser.
ServerTimeOut int64 // The default port is 8088
HTTPAddr string // see AdminPort
HTTPPort int // @Default false
Domains []string EnableAdmin bool
TLSCacheDir string // EnableFcgi
HTTPSAddr string // @Description
HTTPSPort int // @Default false
HTTPSCertFile string EnableFcgi bool
HTTPSKeyFile string // EnableStdIo
TrustCaFile string // @Description EnableStdIo works with EnableFcgi Use FCGI via standard I/O
AdminAddr string // @Default false
AdminPort int EnableStdIo bool
ClientAuth int // ServerTimeOut
// @Description Beego use this as ReadTimeout and WriteTimeout
// The unit is second.
// see http.Server.ReadTimeout, WriteTimeout
// @Default 0
ServerTimeOut int64
// HTTPAddr
// @Description Beego listen to this address when the application start up.
// @Default ""
HTTPAddr string
// HTTPPort
// @Description Beego listen to this port
// you'd better change this value when you deploy to prod environment
// @Default 8080
HTTPPort int
// Domains
// @Description Beego use this to configure TLS. Those domains are "white list" domain
// @Default []
Domains []string
// TLSCacheDir
// @Description Beego use this as cache dir to store TLS cert data
// @Default ""
TLSCacheDir string
// HTTPSAddr
// @Description Beego will listen to this address to accept HTTPS request
// see EnableHTTPS
// @Default ""
HTTPSAddr string
// HTTPSPort
// @Description Beego will listen to this port to accept HTTPS request
// @Default 10443
HTTPSPort int
// HTTPSCertFile
// @Description Beego read this file as cert file
// When you are using HTTPS protocol, please configure it
// see HTTPSKeyFile
// @Default ""
HTTPSCertFile string
// HTTPSKeyFile
// @Description Beego read this file as key file
// When you are using HTTPS protocol, please configure it
// see HTTPSCertFile
// @Default ""
HTTPSKeyFile string
// TrustCaFile
// @Description Beego read this file as CA file
// @Default ""
TrustCaFile string
// AdminAddr
// @Description Beego will listen to this address to provide admin service
// In general, it should be the same with your application address, HTTPAddr or HTTPSAddr
// @Default ""
AdminAddr string
// AdminPort
// @Description Beego will listen to this port to provide admin service
// @Default 8088
AdminPort int
// @Description Beego use this tls.ClientAuthType to initialize TLS connection
// The default value is tls.RequireAndVerifyClientCert
// @Default 4
ClientAuth int
} }
// WebConfig holds web related config // WebConfig holds web related config
type WebConfig struct { type WebConfig struct {
AutoRender bool // AutoRender
EnableDocs bool // @Description If it's true, Beego will render the page based on your template and data
EnableXSRF bool // In general, keep it as true.
DirectoryIndex bool // But if you are building RESTFul API and you don't have any page,
FlashName string // you can set it to false
FlashSeparator string // @Default true
StaticDir map[string]string AutoRender bool
// Deprecated: Beego didn't use it anymore
EnableDocs bool
// EnableXSRF
// @Description If it's true, Beego will help to provide XSRF support
// But you should notice that, now Beego only work for HTTPS protocol with XSRF
// because it's not safe if using HTTP protocol
// And, the cookie storing XSRF token has two more flags HttpOnly and Secure
// It means that you must use HTTPS protocol and you can not read the token from JS script
// This is completed different from Beego 1.x because we got many security reports
// And if you are in dev environment, you could set it to false
// @Default false
EnableXSRF bool
// DirectoryIndex
// @Description When Beego serves static resources request, it will look up the file.
// If the file is directory, Beego will try to find the index.html as the response
// But if the index.html is not exist or it's a directory,
// Beego will return 403 response if DirectoryIndex is **false**
// @Default false
DirectoryIndex bool
// FlashName
// @Description the cookie's name when Beego try to store the flash data into cookie
// @Default BEEGO_FLASH
FlashName string
// FlashSeparator
// @Description When Beego read flash data from request, it uses this as the separator
// @Default BEEGOFLASH
FlashSeparator string
// StaticDir
// @Description Beego uses this as static resources' root directory.
// It means that Beego will try to search static resource from this start point
// It's a map, the key is the path and the value is the directory
// For example, the default value is /static => static,
// which means that when Beego got a request with path /static/xxx
// Beego will try to find the resource from static directory
// @Default /static => static
StaticDir map[string]string
// StaticExtensionsToGzip
// @Description The static resources with those extension will be compressed if EnableGzip is true
// @Default [".css", ".js" ]
StaticExtensionsToGzip []string StaticExtensionsToGzip []string
StaticCacheFileSize int // StaticCacheFileSize
StaticCacheFileNum int // @Description If the size of static resource < StaticCacheFileSize, Beego will try to handle it by itself,
TemplateLeft string // it means that Beego will compressed the file data (if enable) and cache this file.
TemplateRight string // But if the file size > StaticCacheFileSize, Beego just simply delegate the request to http.ServeFile
ViewsPath string // the default value is 100KB.
CommentRouterPath string // the max memory size of caching static files is StaticCacheFileSize * StaticCacheFileNum
XSRFKey string // see StaticCacheFileNum
XSRFExpire int // @Default 102400
Session SessionConfig StaticCacheFileSize int
// StaticCacheFileNum
// @Description Beego use it to control the memory usage of caching static resource file
// If the caching files > StaticCacheFileNum, Beego use LRU algorithm to remove caching file
// the max memory size of caching static files is StaticCacheFileSize * StaticCacheFileNum
// see StaticCacheFileSize
// @Default 1000
StaticCacheFileNum int
// TemplateLeft
// @Description Beego use this to render page
// see TemplateRight
// @Default {{
TemplateLeft string
// TemplateRight
// @Description Beego use this to render page
// see TemplateLeft
// @Default }}
TemplateRight string
// ViewsPath
// @Description The directory of Beego application storing template
// @Default views
ViewsPath string
// CommentRouterPath
// @Description Beego scans this directory and its sub directory to generate router
// Beego only scans this directory when it's in dev environment
// @Default controllers
CommentRouterPath string
// XSRFKey
// @Description the name of cookie storing XSRF token
// see EnableXSRF
// @Default beegoxsrf
XSRFKey string
// XSRFExpire
// @Description the expiration time of XSRF token cookie
// second
// @Default 0
XSRFExpire int
// @Description session related config
Session SessionConfig
} }
// SessionConfig holds session related config // SessionConfig holds session related config
type SessionConfig struct { type SessionConfig struct {
SessionOn bool // SessionOn
SessionAutoSetCookie bool // @Description if it's true, Beego will auto manage session
SessionDisableHTTPOnly bool // used to allow for cross domain cookies/javascript cookies. // @Default false
SessionEnableSidInHTTPHeader bool // enable store/get the sessionId into/from http headers SessionOn bool
SessionEnableSidInURLQuery bool // enable get the sessionId from Url Query params // SessionAutoSetCookie
SessionProvider string // @Description if it's true, Beego will put the session token into cookie too
SessionName string // @Default true
SessionGCMaxLifetime int64 SessionAutoSetCookie bool
SessionProviderConfig string // SessionDisableHTTPOnly
SessionCookieLifeTime int // @Description used to allow for cross domain cookies/javascript cookies
SessionDomain string // In general, you should not set it to true unless you understand the risk
SessionNameInHTTPHeader string // @Default false
SessionCookieSameSite http.SameSite SessionDisableHTTPOnly bool
// SessionEnableSidInHTTPHeader
// @Description enable store/get the sessionId into/from http headers
// @Default false
SessionEnableSidInHTTPHeader bool
// SessionEnableSidInURLQuery
// @Description enable get the sessionId from Url Query params
// @Default false
SessionEnableSidInURLQuery bool
// SessionProvider
// @Description session provider's name.
// You should confirm that this provider has been register via session.Register method
// the default value is memory. This is not suitable for distributed system
// @Default memory
SessionProvider string
// SessionName
// @Description If SessionAutoSetCookie is true, we use this value as the cookie's name
// @Default beegosessionID
SessionName string
// SessionGCMaxLifetime
// @Description Beego will GC session to clean useless session.
// unit: second
// @Default 3600
SessionGCMaxLifetime int64
// SessionProviderConfig
// @Description the config of session provider
// see SessionProvider
// you should read the document of session provider to learn how to set this value
// @Default ""
SessionProviderConfig string
// SessionCookieLifeTime
// @Description If SessionAutoSetCookie is true,
// we use this value as the expiration time and max age of the cookie
// unit second
// @Default 0
SessionCookieLifeTime int
// SessionDomain
// @Description If SessionAutoSetCookie is true, we use this value as the cookie's domain
// @Default ""
SessionDomain string
// SessionNameInHTTPHeader
// @Description if SessionEnableSidInHTTPHeader is true, this value will be used as the http header
// @Default Beegosessionid
SessionNameInHTTPHeader string
// SessionCookieSameSite
// @Description If SessionAutoSetCookie is true, we use this value as the cookie's same site policy
// the default value is http.SameSiteDefaultMode
// @Default 1
SessionCookieSameSite http.SameSite
} }
// LogConfig holds Log related config // LogConfig holds Log related config
type LogConfig struct { type LogConfig struct {
AccessLogs bool // AccessLogs
EnableStaticLogs bool // log static files requests default: false // @Description If it's true, Beego will log the HTTP request info
FileLineNum bool // @Default false
AccessLogsFormat string // access log format: JSON_FORMAT, APACHE_FORMAT or empty string AccessLogs bool
Outputs map[string]string // Store Adaptor : config // EnableStaticLogs
// @Description log static files requests
// @Default false
EnableStaticLogs bool
// FileLineNum
// @Description if it's true, it will log the line number
// @Default true
FileLineNum bool
// AccessLogsFormat
// @Description access log format: JSON_FORMAT, APACHE_FORMAT or empty string
// @Default APACHE_FORMAT
AccessLogsFormat string
// Outputs
// @Description the destination of access log
// the key is log adapter and the value is adapter's configure
// @Default "console" => ""
Outputs map[string]string // Store Adaptor : config
} }
var ( var (

View File

@ -32,6 +32,10 @@ func TestDefaults(t *testing.T) {
} }
} }
func TestLoadAppConfig(t *testing.T) {
println(1 << 30)
}
func TestAssignConfig_01(t *testing.T) { func TestAssignConfig_01(t *testing.T) {
_BConfig := &Config{} _BConfig := &Config{}
_BConfig.AppName = "beego_test" _BConfig.AppName = "beego_test"

View File

@ -247,7 +247,7 @@ func (c *Controller) URLMapping() {}
func (c *Controller) Bind(obj interface{}) error { func (c *Controller) Bind(obj interface{}) error {
ct, exist := c.Ctx.Request.Header["Content-Type"] ct, exist := c.Ctx.Request.Header["Content-Type"]
if !exist || len(ct) == 0 { if !exist || len(ct) == 0 {
return c.BindJson(obj) return c.BindJSON(obj)
} }
i, l := 0, len(ct[0]) i, l := 0, len(ct[0])
for i < l && ct[0][i] != ';' { for i < l && ct[0][i] != ';' {
@ -255,7 +255,7 @@ func (c *Controller) Bind(obj interface{}) error {
} }
switch ct[0][0:i] { switch ct[0][0:i] {
case "application/json": case "application/json":
return c.BindJson(obj) return c.BindJSON(obj)
case "application/xml", "text/xml": case "application/xml", "text/xml":
return c.BindXML(obj) return c.BindXML(obj)
case "application/x-www-form-urlencoded": case "application/x-www-form-urlencoded":
@ -277,7 +277,7 @@ func (c *Controller) BindForm(obj interface{}) error {
return c.ParseForm(obj) return c.ParseForm(obj)
} }
func (c *Controller) BindJson(obj interface{}) error { func (c *Controller) BindJSON(obj interface{}) error {
return json.Unmarshal(c.Ctx.Input.RequestBody, obj) return json.Unmarshal(c.Ctx.Input.RequestBody, obj)
} }
@ -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,14 +612,14 @@ 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
} }
// Get see HttpServer.Get // Get see HttpServer.Get
func Get(rootpath string, f FilterFunc) *HttpServer { func Get(rootpath string, f HandleFunc) *HttpServer {
return BeeApp.Get(rootpath, f) return BeeApp.Get(rootpath, f)
} }
@ -628,13 +628,13 @@ func Get(rootpath string, f FilterFunc) *HttpServer {
// beego.Get("/", func(ctx *context.Context){ // beego.Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Get(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Get(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Get(rootpath, f) app.Handlers.Get(rootpath, f)
return app return app
} }
// Post see HttpServer.Post // Post see HttpServer.Post
func Post(rootpath string, f FilterFunc) *HttpServer { func Post(rootpath string, f HandleFunc) *HttpServer {
return BeeApp.Post(rootpath, f) return BeeApp.Post(rootpath, f)
} }
@ -643,13 +643,13 @@ func Post(rootpath string, f FilterFunc) *HttpServer {
// beego.Post("/api", func(ctx *context.Context){ // beego.Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Post(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Post(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Post(rootpath, f) app.Handlers.Post(rootpath, f)
return app return app
} }
// Delete see HttpServer.Delete // Delete see HttpServer.Delete
func Delete(rootpath string, f FilterFunc) *HttpServer { func Delete(rootpath string, f HandleFunc) *HttpServer {
return BeeApp.Delete(rootpath, f) return BeeApp.Delete(rootpath, f)
} }
@ -658,13 +658,13 @@ func Delete(rootpath string, f FilterFunc) *HttpServer {
// beego.Delete("/api", func(ctx *context.Context){ // beego.Delete("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Delete(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Delete(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Delete(rootpath, f) app.Handlers.Delete(rootpath, f)
return app return app
} }
// Put see HttpServer.Put // Put see HttpServer.Put
func Put(rootpath string, f FilterFunc) *HttpServer { func Put(rootpath string, f HandleFunc) *HttpServer {
return BeeApp.Put(rootpath, f) return BeeApp.Put(rootpath, f)
} }
@ -673,13 +673,13 @@ func Put(rootpath string, f FilterFunc) *HttpServer {
// beego.Put("/api", func(ctx *context.Context){ // beego.Put("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Put(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Put(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Put(rootpath, f) app.Handlers.Put(rootpath, f)
return app return app
} }
// Head see HttpServer.Head // Head see HttpServer.Head
func Head(rootpath string, f FilterFunc) *HttpServer { func Head(rootpath string, f HandleFunc) *HttpServer {
return BeeApp.Head(rootpath, f) return BeeApp.Head(rootpath, f)
} }
@ -688,13 +688,13 @@ func Head(rootpath string, f FilterFunc) *HttpServer {
// beego.Head("/api", func(ctx *context.Context){ // beego.Head("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Head(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Head(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Head(rootpath, f) app.Handlers.Head(rootpath, f)
return app return app
} }
// Options see HttpServer.Options // Options see HttpServer.Options
func Options(rootpath string, f FilterFunc) *HttpServer { func Options(rootpath string, f HandleFunc) *HttpServer {
BeeApp.Handlers.Options(rootpath, f) BeeApp.Handlers.Options(rootpath, f)
return BeeApp return BeeApp
} }
@ -704,13 +704,13 @@ func Options(rootpath string, f FilterFunc) *HttpServer {
// beego.Options("/api", func(ctx *context.Context){ // beego.Options("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Options(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Options(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Options(rootpath, f) app.Handlers.Options(rootpath, f)
return app return app
} }
// Patch see HttpServer.Patch // Patch see HttpServer.Patch
func Patch(rootpath string, f FilterFunc) *HttpServer { func Patch(rootpath string, f HandleFunc) *HttpServer {
return BeeApp.Patch(rootpath, f) return BeeApp.Patch(rootpath, f)
} }
@ -719,13 +719,13 @@ func Patch(rootpath string, f FilterFunc) *HttpServer {
// beego.Patch("/api", func(ctx *context.Context){ // beego.Patch("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Patch(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Patch(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Patch(rootpath, f) app.Handlers.Patch(rootpath, f)
return app return app
} }
// Any see HttpServer.Any // Any see HttpServer.Any
func Any(rootpath string, f FilterFunc) *HttpServer { func Any(rootpath string, f HandleFunc) *HttpServer {
return BeeApp.Any(rootpath, f) return BeeApp.Any(rootpath, f)
} }
@ -734,7 +734,7 @@ func Any(rootpath string, f FilterFunc) *HttpServer {
// beego.Any("/api", func(ctx *context.Context){ // beego.Any("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func (app *HttpServer) Any(rootpath string, f FilterFunc) *HttpServer { func (app *HttpServer) Any(rootpath string, f HandleFunc) *HttpServer {
app.Handlers.Any(rootpath, f) app.Handlers.Any(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")
} }
} }
} }