1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 18:20:54 +00:00

make golint happy router.go

This commit is contained in:
astaxie 2015-09-08 22:01:13 +08:00
parent 21fffc446b
commit c11740b647
2 changed files with 69 additions and 70 deletions

137
router.go
View File

@ -34,8 +34,8 @@ import (
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
) )
// default filter execution points
const ( const (
// default filter execution points
BeforeStatic = iota BeforeStatic = iota
BeforeRouter BeforeRouter
BeforeExec BeforeExec
@ -50,7 +50,7 @@ const (
) )
var ( var (
// supported http methods. // HTTPMETHOD list the supported http methods.
HTTPMETHOD = map[string]string{ HTTPMETHOD = map[string]string{
"GET": "GET", "GET": "GET",
"POST": "POST", "POST": "POST",
@ -71,10 +71,12 @@ var (
"SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml", "SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml",
"GetControllerAndAction"} "GetControllerAndAction"}
url_placeholder = "{{placeholder}}" urlPlaceholder = "{{placeholder}}"
DefaultLogFilter FilterHandler = &logFilter{} // DefaultAccessLogFilter will skip the accesslog if return true
DefaultAccessLogFilter FilterHandler = &logFilter{}
) )
// FilterHandler is an interface for
type FilterHandler interface { type FilterHandler interface {
Filter(*beecontext.Context) bool Filter(*beecontext.Context) bool
} }
@ -96,7 +98,7 @@ func (l *logFilter) Filter(ctx *beecontext.Context) bool {
return false return false
} }
// To append a slice's value into "exceptMethod", for controller's methods shouldn't reflect to AutoRouter // ExceptMethodAppend to append a slice's value into "exceptMethod", for controller's methods shouldn't reflect to AutoRouter
func ExceptMethodAppend(action string) { func ExceptMethodAppend(action string) {
exceptMethod = append(exceptMethod, action) exceptMethod = append(exceptMethod, action)
} }
@ -196,7 +198,7 @@ func (p *ControllerRegister) addToRouter(method, pattern string, r *controllerIn
} }
} }
// only when the Runmode is dev will generate router file in the router/auto.go from the controller // Include only when the Runmode is dev will generate router file in the router/auto.go from the controller
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{}) // Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
func (p *ControllerRegister) Include(cList ...ControllerInterface) { func (p *ControllerRegister) Include(cList ...ControllerInterface) {
if RunMode == "dev" { if RunMode == "dev" {
@ -238,7 +240,7 @@ func (p *ControllerRegister) Include(cList ...ControllerInterface) {
} }
} }
// 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")
@ -247,7 +249,7 @@ func (p *ControllerRegister) Get(pattern string, f FilterFunc) {
p.AddMethod("get", pattern, f) p.AddMethod("get", pattern, f)
} }
// add post method // Post add post method
// usage: // usage:
// Post("/api", func(ctx *context.Context){ // Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
@ -256,7 +258,7 @@ func (p *ControllerRegister) Post(pattern string, f FilterFunc) {
p.AddMethod("post", pattern, f) p.AddMethod("post", pattern, f)
} }
// add put method // Put add put method
// usage: // usage:
// Put("/api/:id", func(ctx *context.Context){ // Put("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
@ -265,7 +267,7 @@ func (p *ControllerRegister) Put(pattern string, f FilterFunc) {
p.AddMethod("put", pattern, f) p.AddMethod("put", pattern, f)
} }
// add delete method // Delete add delete method
// usage: // usage:
// Delete("/api/:id", func(ctx *context.Context){ // Delete("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
@ -274,7 +276,7 @@ func (p *ControllerRegister) Delete(pattern string, f FilterFunc) {
p.AddMethod("delete", pattern, f) p.AddMethod("delete", pattern, f)
} }
// add head method // Head add head method
// usage: // usage:
// Head("/api/:id", func(ctx *context.Context){ // Head("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
@ -283,7 +285,7 @@ func (p *ControllerRegister) Head(pattern string, f FilterFunc) {
p.AddMethod("head", pattern, f) p.AddMethod("head", pattern, f)
} }
// add patch method // Patch add patch method
// usage: // usage:
// Patch("/api/:id", func(ctx *context.Context){ // Patch("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
@ -292,7 +294,7 @@ func (p *ControllerRegister) Patch(pattern string, f FilterFunc) {
p.AddMethod("patch", pattern, f) p.AddMethod("patch", pattern, f)
} }
// add options method // Options add options method
// usage: // usage:
// Options("/api/:id", func(ctx *context.Context){ // Options("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
@ -301,7 +303,7 @@ func (p *ControllerRegister) Options(pattern string, f FilterFunc) {
p.AddMethod("options", pattern, f) p.AddMethod("options", pattern, f)
} }
// add all method // Any add all method
// usage: // usage:
// Any("/api/:id", func(ctx *context.Context){ // Any("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
@ -310,7 +312,7 @@ func (p *ControllerRegister) Any(pattern string, f FilterFunc) {
p.AddMethod("*", pattern, f) p.AddMethod("*", pattern, f)
} }
// add http method router // AddMethod add http method router
// usage: // usage:
// 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")
@ -343,7 +345,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
} }
} }
// add user defined Handler // Handler add user defined Handler
func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ...interface{}) { func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ...interface{}) {
route := &controllerInfo{} route := &controllerInfo{}
route.pattern = pattern route.pattern = pattern
@ -359,7 +361,7 @@ func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ...
} }
} }
// Add auto router to ControllerRegister. // AddAuto router to ControllerRegister.
// example beego.AddAuto(&MainContorlller{}), // example beego.AddAuto(&MainContorlller{}),
// MainController has method List and Page. // MainController has method List and Page.
// visit the url /main/list to execute List function // visit the url /main/list to execute List function
@ -368,7 +370,7 @@ func (p *ControllerRegister) AddAuto(c ControllerInterface) {
p.AddAutoPrefix("/", c) p.AddAutoPrefix("/", c)
} }
// Add auto router to ControllerRegister with prefix. // AddAutoPrefix Add auto router to ControllerRegister with prefix.
// example beego.AddAutoPrefix("/admin",&MainContorlller{}), // example beego.AddAutoPrefix("/admin",&MainContorlller{}),
// MainController has method List and Page. // MainController has method List and Page.
// visit the url /admin/main/list to execute List function // visit the url /admin/main/list to execute List function
@ -399,7 +401,7 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface)
} }
} }
// Add a FilterFunc with pattern rule and action constant. // InsertFilter Add a FilterFunc with pattern rule and action constant.
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute) // The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) error { func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) error {
@ -426,9 +428,9 @@ func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) error
return nil return nil
} }
// UrlFor does another controller handler in this request function. // URLFor does another controller handler in this request function.
// it can access any controller method. // it can access any controller method.
func (p *ControllerRegister) UrlFor(endpoint string, values ...interface{}) string { func (p *ControllerRegister) URLFor(endpoint string, values ...interface{}) string {
paths := strings.Split(endpoint, ".") paths := strings.Split(endpoint, ".")
if len(paths) <= 1 { if len(paths) <= 1 {
Warn("urlfor endpoint must like path.controller.method") Warn("urlfor endpoint must like path.controller.method")
@ -469,7 +471,7 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
} }
} }
if t.wildcard != nil { if t.wildcard != nil {
u := path.Join(url, url_placeholder) u := path.Join(url, urlPlaceholder)
ok, u := p.geturl(t.wildcard, u, controllName, methodName, params, httpMethod) ok, u := p.geturl(t.wildcard, u, controllName, methodName, params, httpMethod)
if ok { if ok {
return ok, u return ok, u
@ -499,22 +501,21 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
if find { if find {
if l.regexps == nil { if l.regexps == nil {
if len(l.wildcards) == 0 { if len(l.wildcards) == 0 {
return true, strings.Replace(url, "/"+url_placeholder, "", 1) + tourl(params) return true, strings.Replace(url, "/"+urlPlaceholder, "", 1) + tourl(params)
} }
if len(l.wildcards) == 1 { if len(l.wildcards) == 1 {
if v, ok := params[l.wildcards[0]]; ok { if v, ok := params[l.wildcards[0]]; ok {
delete(params, l.wildcards[0]) delete(params, l.wildcards[0])
return true, strings.Replace(url, url_placeholder, v, 1) + tourl(params) return true, strings.Replace(url, urlPlaceholder, v, 1) + tourl(params)
} else {
return false, ""
} }
return false, ""
} }
if len(l.wildcards) == 3 && l.wildcards[0] == "." { if len(l.wildcards) == 3 && l.wildcards[0] == "." {
if p, ok := params[":path"]; ok { if p, ok := params[":path"]; ok {
if e, isok := params[":ext"]; isok { if e, isok := params[":ext"]; isok {
delete(params, ":path") delete(params, ":path")
delete(params, ":ext") delete(params, ":ext")
return true, strings.Replace(url, url_placeholder, p+"."+e, -1) + tourl(params) return true, strings.Replace(url, urlPlaceholder, p+"."+e, -1) + tourl(params)
} }
} }
} }
@ -526,45 +527,43 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
} }
if u, ok := params[v]; ok { if u, ok := params[v]; ok {
delete(params, v) delete(params, v)
url = strings.Replace(url, url_placeholder, u, 1) url = strings.Replace(url, urlPlaceholder, u, 1)
} else { } else {
if canskip { if canskip {
canskip = false canskip = false
continue continue
} else {
return false, ""
} }
return false, ""
} }
} }
return true, url + tourl(params) return true, url + tourl(params)
} else { }
var i int var i int
var startreg bool var startreg bool
regurl := "" regurl := ""
for _, v := range strings.Trim(l.regexps.String(), "^$") { for _, v := range strings.Trim(l.regexps.String(), "^$") {
if v == '(' { if v == '(' {
startreg = true startreg = true
continue continue
} else if v == ')' { } else if v == ')' {
startreg = false startreg = false
if v, ok := params[l.wildcards[i]]; ok { if v, ok := params[l.wildcards[i]]; ok {
delete(params, l.wildcards[i]) delete(params, l.wildcards[i])
regurl = regurl + v regurl = regurl + v
i++ i++
} else { } else {
break break
}
} else if !startreg {
regurl = string(append([]rune(regurl), v))
} }
} else if !startreg {
regurl = string(append([]rune(regurl), v))
} }
if l.regexps.MatchString(regurl) { }
ps := strings.Split(regurl, "/") if l.regexps.MatchString(regurl) {
for _, p := range ps { ps := strings.Split(regurl, "/")
url = strings.Replace(url, url_placeholder, p, 1) for _, p := range ps {
} url = strings.Replace(url, urlPlaceholder, p, 1)
return true, url + tourl(params)
} }
return true, url + tourl(params)
} }
} }
} }
@ -607,7 +606,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
urlPath = r.URL.Path urlPath = r.URL.Path
} }
// defined filter function // defined filter function
do_filter := func(pos int) (started bool) { doFilter := func(pos int) (started bool) {
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters[pos]; ok { if l, ok := p.filters[pos]; ok {
for _, filterR := range l { for _, filterR := range l {
@ -639,7 +638,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
// filter for static file // filter for static file
if do_filter(BeforeStatic) { if doFilter(BeforeStatic) {
goto Admin goto Admin
} }
@ -670,7 +669,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context.Input.ParseFormOrMulitForm(MaxMemory) context.Input.ParseFormOrMulitForm(MaxMemory)
} }
if do_filter(BeforeRouter) { if doFilter(BeforeRouter) {
goto Admin goto Admin
} }
@ -681,17 +680,17 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
if !findrouter { if !findrouter {
http_method := r.Method httpMethod := r.Method
if http_method == "POST" && context.Input.Query("_method") == "PUT" { if httpMethod == "POST" && context.Input.Query("_method") == "PUT" {
http_method = "PUT" httpMethod = "PUT"
} }
if http_method == "POST" && context.Input.Query("_method") == "DELETE" { if httpMethod == "POST" && context.Input.Query("_method") == "DELETE" {
http_method = "DELETE" httpMethod = "DELETE"
} }
if t, ok := p.routers[http_method]; ok { if t, ok := p.routers[httpMethod]; ok {
runObject, p := t.Match(urlPath) runObject, p := t.Match(urlPath)
if r, ok := runObject.(*controllerInfo); ok { if r, ok := runObject.(*controllerInfo); ok {
routerInfo = r routerInfo = r
@ -718,7 +717,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if findrouter { if findrouter {
//execute middleware filters //execute middleware filters
if do_filter(BeforeExec) { if doFilter(BeforeExec) {
goto Admin goto Admin
} }
isRunable := false isRunable := false
@ -798,7 +797,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
execController.Options() execController.Options()
default: default:
if !execController.HandlerFunc(runMethod) { if !execController.HandlerFunc(runMethod) {
in := make([]reflect.Value, 0) var in []reflect.Value
method := vc.MethodByName(runMethod) method := vc.MethodByName(runMethod)
method.Call(in) method.Call(in)
} }
@ -819,12 +818,12 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
//execute middleware filters //execute middleware filters
if do_filter(AfterExec) { if doFilter(AfterExec) {
goto Admin goto Admin
} }
} }
do_filter(FinishRouter) doFilter(FinishRouter)
Admin: Admin:
timeend := time.Since(starttime) timeend := time.Since(starttime)
@ -850,7 +849,7 @@ Admin:
} else { } else {
devinfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeend.String(), "notmatch") devinfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeend.String(), "notmatch")
} }
if DefaultLogFilter == nil || !DefaultLogFilter.Filter(context) { if DefaultAccessLogFilter == nil || !DefaultAccessLogFilter.Filter(context) {
Debug(devinfo) Debug(devinfo)
} }
} }

View File

@ -255,7 +255,7 @@ func Htmlunquote(src string) string {
// //
// more detail http://beego.me/docs/mvc/controller/urlbuilding.md // more detail http://beego.me/docs/mvc/controller/urlbuilding.md
func URLFor(endpoint string, values ...interface{}) string { func URLFor(endpoint string, values ...interface{}) string {
return BeeApp.Handlers.UrlFor(endpoint, values...) return BeeApp.Handlers.URLFor(endpoint, values...)
} }
// returns script tag with src string. // returns script tag with src string.