mirror of
https://github.com/astaxie/beego.git
synced 2025-07-04 09:30:17 +00:00
Fixes #2587
Fixes warnings and errors raised by gometalinter and gosimple.
This commit is contained in:
49
router.go
49
router.go
@ -109,7 +109,8 @@ func ExceptMethodAppend(action string) {
|
||||
exceptMethod = append(exceptMethod, action)
|
||||
}
|
||||
|
||||
type controllerInfo struct {
|
||||
// ControllerInfo holds information about the controller.
|
||||
type ControllerInfo struct {
|
||||
pattern string
|
||||
controllerType reflect.Type
|
||||
methods map[string]string
|
||||
@ -176,7 +177,7 @@ func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingM
|
||||
}
|
||||
}
|
||||
|
||||
route := &controllerInfo{}
|
||||
route := &ControllerInfo{}
|
||||
route.pattern = pattern
|
||||
route.methods = methods
|
||||
route.routerType = routerTypeBeego
|
||||
@ -198,7 +199,7 @@ func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingM
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ControllerRegister) addToRouter(method, pattern string, r *controllerInfo) {
|
||||
func (p *ControllerRegister) addToRouter(method, pattern string, r *ControllerInfo) {
|
||||
if !BConfig.RouterCaseSensitive {
|
||||
pattern = strings.ToLower(pattern)
|
||||
}
|
||||
@ -335,7 +336,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
|
||||
if _, ok := HTTPMETHOD[method]; method != "*" && !ok {
|
||||
panic("not support http method: " + method)
|
||||
}
|
||||
route := &controllerInfo{}
|
||||
route := &ControllerInfo{}
|
||||
route.pattern = pattern
|
||||
route.routerType = routerTypeRESTFul
|
||||
route.runFunction = f
|
||||
@ -361,7 +362,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
|
||||
|
||||
// Handler add user defined Handler
|
||||
func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ...interface{}) {
|
||||
route := &controllerInfo{}
|
||||
route := &ControllerInfo{}
|
||||
route.pattern = pattern
|
||||
route.routerType = routerTypeHandler
|
||||
route.handler = h
|
||||
@ -396,7 +397,7 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface)
|
||||
controllerName := strings.TrimSuffix(ct.Name(), "Controller")
|
||||
for i := 0; i < rt.NumMethod(); i++ {
|
||||
if !utils.InSlice(rt.Method(i).Name, exceptMethod) {
|
||||
route := &controllerInfo{}
|
||||
route := &ControllerInfo{}
|
||||
route.routerType = routerTypeBeego
|
||||
route.methods = map[string]string{"*": rt.Method(i).Name}
|
||||
route.controllerType = ct
|
||||
@ -502,7 +503,7 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
|
||||
}
|
||||
}
|
||||
for _, l := range t.leaves {
|
||||
if c, ok := l.runObject.(*controllerInfo); ok {
|
||||
if c, ok := l.runObject.(*ControllerInfo); ok {
|
||||
if c.routerType == routerTypeBeego &&
|
||||
strings.HasSuffix(path.Join(c.controllerType.PkgPath(), c.controllerType.Name()), controllName) {
|
||||
find := false
|
||||
@ -629,7 +630,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
runRouter reflect.Type
|
||||
findRouter bool
|
||||
runMethod string
|
||||
routerInfo *controllerInfo
|
||||
routerInfo *ControllerInfo
|
||||
isRunnable bool
|
||||
)
|
||||
context := p.pool.Get().(*beecontext.Context)
|
||||
@ -670,7 +671,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
goto Admin
|
||||
}
|
||||
|
||||
if r.Method != "GET" && r.Method != "HEAD" {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
if BConfig.CopyRequestBody && !context.Input.IsUpload() {
|
||||
context.Input.CopyBody(BConfig.MaxMemory)
|
||||
}
|
||||
@ -743,11 +744,11 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
} else {
|
||||
runRouter = routerInfo.controllerType
|
||||
method := r.Method
|
||||
if r.Method == "POST" && context.Input.Query("_method") == "PUT" {
|
||||
method = "PUT"
|
||||
if r.Method == http.MethodPost && context.Input.Query("_method") == http.MethodPost {
|
||||
method = http.MethodPut
|
||||
}
|
||||
if r.Method == "POST" && context.Input.Query("_method") == "DELETE" {
|
||||
method = "DELETE"
|
||||
if r.Method == http.MethodPost && context.Input.Query("_method") == http.MethodDelete {
|
||||
method = http.MethodDelete
|
||||
}
|
||||
if m, ok := routerInfo.methods[method]; ok {
|
||||
runMethod = m
|
||||
@ -777,8 +778,8 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
||||
if BConfig.WebConfig.EnableXSRF {
|
||||
execController.XSRFToken()
|
||||
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
||||
(r.Method == "POST" && (context.Input.Query("_method") == "DELETE" || context.Input.Query("_method") == "PUT")) {
|
||||
if r.Method == http.MethodPost || r.Method == http.MethodDelete || r.Method == http.MethodPut ||
|
||||
(r.Method == http.MethodPost && (context.Input.Query("_method") == http.MethodDelete || context.Input.Query("_method") == http.MethodPut)) {
|
||||
execController.CheckXSRFCookie()
|
||||
}
|
||||
}
|
||||
@ -788,19 +789,19 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
if !context.ResponseWriter.Started {
|
||||
//exec main logic
|
||||
switch runMethod {
|
||||
case "GET":
|
||||
case http.MethodGet:
|
||||
execController.Get()
|
||||
case "POST":
|
||||
case http.MethodPost:
|
||||
execController.Post()
|
||||
case "DELETE":
|
||||
case http.MethodDelete:
|
||||
execController.Delete()
|
||||
case "PUT":
|
||||
case http.MethodPut:
|
||||
execController.Put()
|
||||
case "HEAD":
|
||||
case http.MethodHead:
|
||||
execController.Head()
|
||||
case "PATCH":
|
||||
case http.MethodPatch:
|
||||
execController.Patch()
|
||||
case "OPTIONS":
|
||||
case http.MethodOptions:
|
||||
execController.Options()
|
||||
default:
|
||||
if !execController.HandlerFunc(runMethod) {
|
||||
@ -887,7 +888,7 @@ Admin:
|
||||
}
|
||||
|
||||
// FindRouter Find Router info for URL
|
||||
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *controllerInfo, isFind bool) {
|
||||
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *ControllerInfo, isFind bool) {
|
||||
var urlPath = context.Input.URL()
|
||||
if !BConfig.RouterCaseSensitive {
|
||||
urlPath = strings.ToLower(urlPath)
|
||||
@ -895,7 +896,7 @@ func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo
|
||||
httpMethod := context.Input.Method()
|
||||
if t, ok := p.routers[httpMethod]; ok {
|
||||
runObject := t.Match(urlPath, context)
|
||||
if r, ok := runObject.(*controllerInfo); ok {
|
||||
if r, ok := runObject.(*ControllerInfo); ok {
|
||||
return r, true
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user