2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
2013-04-11 06:35:43 +00:00
|
|
|
"fmt"
|
2012-12-18 07:18:43 +00:00
|
|
|
"net/http"
|
2014-06-08 12:24:01 +00:00
|
|
|
"path"
|
2014-06-09 02:11:37 +00:00
|
|
|
"path/filepath"
|
2012-12-18 07:18:43 +00:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2013-07-27 02:25:14 +00:00
|
|
|
"strconv"
|
2012-12-18 07:18:43 +00:00
|
|
|
"strings"
|
2015-12-10 13:59:54 +00:00
|
|
|
"sync"
|
2013-11-13 13:11:03 +00:00
|
|
|
"time"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
|
|
|
beecontext "github.com/astaxie/beego/context"
|
2017-04-25 15:39:42 +00:00
|
|
|
"github.com/astaxie/beego/context/param"
|
2016-03-24 09:39:29 +00:00
|
|
|
"github.com/astaxie/beego/logs"
|
2013-12-03 13:37:39 +00:00
|
|
|
"github.com/astaxie/beego/toolbox"
|
2013-12-12 14:25:08 +00:00
|
|
|
"github.com/astaxie/beego/utils"
|
2012-12-18 07:18:43 +00:00
|
|
|
)
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// default filter execution points
|
2013-11-25 07:59:40 +00:00
|
|
|
const (
|
2014-08-04 07:31:27 +00:00
|
|
|
BeforeStatic = iota
|
|
|
|
BeforeRouter
|
2013-11-25 07:59:40 +00:00
|
|
|
BeforeExec
|
|
|
|
AfterExec
|
|
|
|
FinishRouter
|
|
|
|
)
|
|
|
|
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
const (
|
2017-09-13 00:03:46 +00:00
|
|
|
routerTypeBeego = iota
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
routerTypeRESTFul
|
|
|
|
routerTypeHandler
|
|
|
|
)
|
|
|
|
|
2013-12-15 18:17:27 +00:00
|
|
|
var (
|
2015-09-08 14:01:13 +00:00
|
|
|
// HTTPMETHOD list the supported http methods.
|
2014-06-10 17:11:32 +00:00
|
|
|
HTTPMETHOD = map[string]string{
|
2017-02-10 08:45:47 +00:00
|
|
|
"GET": "GET",
|
|
|
|
"POST": "POST",
|
|
|
|
"PUT": "PUT",
|
|
|
|
"DELETE": "DELETE",
|
|
|
|
"PATCH": "PATCH",
|
|
|
|
"OPTIONS": "OPTIONS",
|
|
|
|
"HEAD": "HEAD",
|
|
|
|
"TRACE": "TRACE",
|
|
|
|
"CONNECT": "CONNECT",
|
|
|
|
"MKCOL": "MKCOL",
|
|
|
|
"COPY": "COPY",
|
|
|
|
"MOVE": "MOVE",
|
|
|
|
"PROPFIND": "PROPFIND",
|
|
|
|
"PROPPATCH": "PROPPATCH",
|
|
|
|
"LOCK": "LOCK",
|
|
|
|
"UNLOCK": "UNLOCK",
|
2014-06-10 17:11:32 +00:00
|
|
|
}
|
2014-01-01 09:57:57 +00:00
|
|
|
// these beego.Controller's methods shouldn't reflect to AutoRouter
|
2015-09-18 21:41:10 +00:00
|
|
|
exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString",
|
2016-01-21 06:44:17 +00:00
|
|
|
"RenderBytes", "Redirect", "Abort", "StopRun", "UrlFor", "ServeJSON", "ServeJSONP",
|
|
|
|
"ServeXML", "Input", "ParseForm", "GetString", "GetStrings", "GetInt", "GetBool",
|
2015-09-18 21:41:10 +00:00
|
|
|
"GetFloat", "GetFile", "SaveToFile", "StartSession", "SetSession", "GetSession",
|
|
|
|
"DelSession", "SessionRegenerateID", "DestroySession", "IsAjax", "GetSecureCookie",
|
|
|
|
"SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml",
|
2016-01-21 06:44:17 +00:00
|
|
|
"GetControllerAndAction", "ServeFormatted"}
|
2014-07-03 15:40:21 +00:00
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
urlPlaceholder = "{{placeholder}}"
|
|
|
|
// DefaultAccessLogFilter will skip the accesslog if return true
|
|
|
|
DefaultAccessLogFilter FilterHandler = &logFilter{}
|
2013-12-15 18:17:27 +00:00
|
|
|
)
|
support user define function
+//Add("/user",&UserController{})
+//Add("/api/list",&RestController{},"*:ListFood")
+//Add("/api/create",&RestController{},"post:CreateFood")
+//Add("/api/update",&RestController{},"put:UpdateFood")
+//Add("/api/delete",&RestController{},"delete:DeleteFood")
+//Add("/api",&RestController{},"get,post:ApiFunc")
+//Add("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
2013-07-25 07:17:09 +00:00
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// FilterHandler is an interface for
|
2014-11-03 08:14:40 +00:00
|
|
|
type FilterHandler interface {
|
|
|
|
Filter(*beecontext.Context) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// default log filter static file will not show
|
|
|
|
type logFilter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logFilter) Filter(ctx *beecontext.Context) bool {
|
2015-12-10 13:59:54 +00:00
|
|
|
requestPath := path.Clean(ctx.Request.URL.Path)
|
2014-11-03 08:14:40 +00:00
|
|
|
if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
|
|
|
|
return true
|
|
|
|
}
|
2015-12-09 15:35:04 +00:00
|
|
|
for prefix := range BConfig.WebConfig.StaticDir {
|
2014-11-03 08:14:40 +00:00
|
|
|
if strings.HasPrefix(requestPath, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// ExceptMethodAppend to append a slice's value into "exceptMethod", for controller's methods shouldn't reflect to AutoRouter
|
2014-03-12 09:20:53 +00:00
|
|
|
func ExceptMethodAppend(action string) {
|
|
|
|
exceptMethod = append(exceptMethod, action)
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:07:12 +00:00
|
|
|
// ControllerInfo holds information about the controller.
|
|
|
|
type ControllerInfo struct {
|
2014-06-10 10:09:07 +00:00
|
|
|
pattern string
|
2012-12-18 07:18:43 +00:00
|
|
|
controllerType reflect.Type
|
support user define function
+//Add("/user",&UserController{})
+//Add("/api/list",&RestController{},"*:ListFood")
+//Add("/api/create",&RestController{},"post:CreateFood")
+//Add("/api/update",&RestController{},"put:UpdateFood")
+//Add("/api/delete",&RestController{},"delete:DeleteFood")
+//Add("/api",&RestController{},"get,post:ApiFunc")
+//Add("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
2013-07-25 07:17:09 +00:00
|
|
|
methods map[string]string
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
handler http.Handler
|
2015-09-18 10:34:07 +00:00
|
|
|
runFunction FilterFunc
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
routerType int
|
2017-05-19 23:06:28 +00:00
|
|
|
initialize func() ControllerInterface
|
2017-04-21 12:26:41 +00:00
|
|
|
methodParams []*param.MethodParam
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 00:44:58 +00:00
|
|
|
// ControllerRegister containers registered router rules, controller handlers and filters.
|
|
|
|
type ControllerRegister struct {
|
2014-06-09 02:11:37 +00:00
|
|
|
routers map[string]*Tree
|
2016-09-28 18:21:07 +00:00
|
|
|
enablePolicy bool
|
|
|
|
policies map[string]*Tree
|
2013-12-20 16:34:59 +00:00
|
|
|
enableFilter bool
|
2016-03-15 10:37:54 +00:00
|
|
|
filters [FinishRouter + 1][]*FilterRouter
|
2015-12-10 13:59:54 +00:00
|
|
|
pool sync.Pool
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 00:44:58 +00:00
|
|
|
// NewControllerRegister returns a new ControllerRegister.
|
|
|
|
func NewControllerRegister() *ControllerRegister {
|
2015-12-10 13:59:54 +00:00
|
|
|
cr := &ControllerRegister{
|
2016-09-28 18:21:07 +00:00
|
|
|
routers: make(map[string]*Tree),
|
|
|
|
policies: make(map[string]*Tree),
|
2013-07-27 02:25:14 +00:00
|
|
|
}
|
2015-12-10 13:59:54 +00:00
|
|
|
cr.pool.New = func() interface{} {
|
|
|
|
return beecontext.NewContext()
|
|
|
|
}
|
|
|
|
return cr
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 00:44:58 +00:00
|
|
|
// Add controller handler and pattern rules to ControllerRegister.
|
2013-12-21 05:19:24 +00:00
|
|
|
// usage:
|
|
|
|
// default methods is the same name as method
|
|
|
|
// Add("/user",&UserController{})
|
|
|
|
// Add("/api/list",&RestController{},"*:ListFood")
|
|
|
|
// Add("/api/create",&RestController{},"post:CreateFood")
|
|
|
|
// Add("/api/update",&RestController{},"put:UpdateFood")
|
|
|
|
// Add("/api/delete",&RestController{},"delete:DeleteFood")
|
2015-12-10 13:59:54 +00:00
|
|
|
// Add("/api",&RestController{},"get,post:ApiFunc"
|
2013-12-21 05:19:24 +00:00
|
|
|
// Add("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingMethods ...string) {
|
2017-04-21 12:26:41 +00:00
|
|
|
p.addWithMethodParams(pattern, c, nil, mappingMethods...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ControllerRegister) addWithMethodParams(pattern string, c ControllerInterface, methodParams []*param.MethodParam, mappingMethods ...string) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
reflectVal := reflect.ValueOf(c)
|
|
|
|
t := reflect.Indirect(reflectVal).Type()
|
|
|
|
methods := make(map[string]string)
|
|
|
|
if len(mappingMethods) > 0 {
|
|
|
|
semi := strings.Split(mappingMethods[0], ";")
|
|
|
|
for _, v := range semi {
|
|
|
|
colon := strings.Split(v, ":")
|
|
|
|
if len(colon) != 2 {
|
|
|
|
panic("method mapping format is invalid")
|
|
|
|
}
|
|
|
|
comma := strings.Split(colon[0], ",")
|
|
|
|
for _, m := range comma {
|
2014-06-10 17:11:32 +00:00
|
|
|
if _, ok := HTTPMETHOD[strings.ToUpper(m)]; m == "*" || ok {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
if val := reflectVal.MethodByName(colon[1]); val.IsValid() {
|
2014-06-10 17:11:32 +00:00
|
|
|
methods[strings.ToUpper(m)] = colon[1]
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
} else {
|
2014-12-08 06:57:45 +00:00
|
|
|
panic("'" + colon[1] + "' method doesn't exist in the controller " + t.Name())
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(v + " is an invalid method mapping. Method doesn't exist " + m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:07:12 +00:00
|
|
|
route := &ControllerInfo{}
|
2014-06-10 10:09:07 +00:00
|
|
|
route.pattern = pattern
|
2014-06-08 12:24:01 +00:00
|
|
|
route.methods = methods
|
|
|
|
route.routerType = routerTypeBeego
|
|
|
|
route.controllerType = t
|
2017-05-19 23:06:28 +00:00
|
|
|
route.initialize = func() ControllerInterface {
|
|
|
|
vc := reflect.New(route.controllerType)
|
|
|
|
execController, ok := vc.Interface().(ControllerInterface)
|
|
|
|
if !ok {
|
|
|
|
panic("controller is not ControllerInterface")
|
|
|
|
}
|
|
|
|
|
|
|
|
elemVal := reflect.ValueOf(c).Elem()
|
|
|
|
elemType := reflect.TypeOf(c).Elem()
|
|
|
|
execElem := reflect.ValueOf(execController).Elem()
|
|
|
|
|
|
|
|
numOfFields := elemVal.NumField()
|
|
|
|
for i := 0; i < numOfFields; i++ {
|
|
|
|
fieldVal := elemVal.Field(i)
|
|
|
|
fieldType := elemType.Field(i)
|
|
|
|
execElem.FieldByName(fieldType.Name).Set(fieldVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
return execController
|
|
|
|
}
|
|
|
|
|
2017-04-21 12:26:41 +00:00
|
|
|
route.methodParams = methodParams
|
2014-06-09 02:11:37 +00:00
|
|
|
if len(methods) == 0 {
|
|
|
|
for _, m := range HTTPMETHOD {
|
|
|
|
p.addToRouter(m, pattern, route)
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-20 05:29:01 +00:00
|
|
|
for k := range methods {
|
2014-06-09 02:11:37 +00:00
|
|
|
if k == "*" {
|
|
|
|
for _, m := range HTTPMETHOD {
|
|
|
|
p.addToRouter(m, pattern, route)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.addToRouter(k, pattern, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:07:12 +00:00
|
|
|
func (p *ControllerRegister) addToRouter(method, pattern string, r *ControllerInfo) {
|
2015-12-09 15:35:04 +00:00
|
|
|
if !BConfig.RouterCaseSensitive {
|
2014-09-28 14:10:43 +00:00
|
|
|
pattern = strings.ToLower(pattern)
|
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
if t, ok := p.routers[method]; ok {
|
|
|
|
t.AddRouter(pattern, r)
|
|
|
|
} else {
|
|
|
|
t := NewTree()
|
|
|
|
t.AddRouter(pattern, r)
|
|
|
|
p.routers[method] = t
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Include only when the Runmode is dev will generate router file in the router/auto.go from the controller
|
2014-06-08 12:24:01 +00:00
|
|
|
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Include(cList ...ControllerInterface) {
|
2015-12-29 18:32:37 +00:00
|
|
|
if BConfig.RunMode == DEV {
|
2014-06-09 02:11:37 +00:00
|
|
|
skip := make(map[string]bool, 10)
|
2014-06-08 12:24:01 +00:00
|
|
|
for _, c := range cList {
|
|
|
|
reflectVal := reflect.ValueOf(c)
|
|
|
|
t := reflect.Indirect(reflectVal).Type()
|
2017-04-24 13:10:03 +00:00
|
|
|
wgopath := utils.GetGOPATHs()
|
|
|
|
if len(wgopath) == 0 {
|
2014-06-09 02:11:37 +00:00
|
|
|
panic("you are in dev mode. So please set gopath")
|
|
|
|
}
|
|
|
|
pkgpath := ""
|
|
|
|
for _, wg := range wgopath {
|
|
|
|
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", t.PkgPath()))
|
|
|
|
if utils.FileExists(wg) {
|
|
|
|
pkgpath = wg
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pkgpath != "" {
|
|
|
|
if _, ok := skip[pkgpath]; !ok {
|
|
|
|
skip[pkgpath] = true
|
2014-06-09 09:33:04 +00:00
|
|
|
parserPkg(pkgpath, t.PkgPath())
|
2014-06-09 02:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, c := range cList {
|
|
|
|
reflectVal := reflect.ValueOf(c)
|
|
|
|
t := reflect.Indirect(reflectVal).Type()
|
|
|
|
key := t.PkgPath() + ":" + t.Name()
|
|
|
|
if comm, ok := GlobalControllerRouter[key]; ok {
|
2014-06-09 09:33:04 +00:00
|
|
|
for _, a := range comm {
|
2017-04-21 12:26:41 +00:00
|
|
|
p.addWithMethodParams(a.Router, c, a.MethodParams, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Get add get method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Get("/", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Get(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("get", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Post add post method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Post("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Post(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("post", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Put add put method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Put("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Put(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("put", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Delete add delete method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Delete("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Delete(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("delete", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Head add head method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Head("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Head(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("head", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Patch add patch method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Patch("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Patch(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("patch", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Options add options method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Options("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Options(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("options", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Any add all method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// Any("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Any(pattern string, f FilterFunc) {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
p.AddMethod("*", pattern, f)
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// AddMethod add http method router
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// usage:
|
|
|
|
// AddMethod("get","/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
|
2016-01-18 07:17:42 +00:00
|
|
|
method = strings.ToUpper(method)
|
|
|
|
if _, ok := HTTPMETHOD[method]; method != "*" && !ok {
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
panic("not support http method: " + method)
|
|
|
|
}
|
2017-04-23 17:07:12 +00:00
|
|
|
route := &ControllerInfo{}
|
2014-06-10 10:09:07 +00:00
|
|
|
route.pattern = pattern
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
route.routerType = routerTypeRESTFul
|
2015-09-18 10:31:06 +00:00
|
|
|
route.runFunction = f
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
methods := make(map[string]string)
|
|
|
|
if method == "*" {
|
|
|
|
for _, val := range HTTPMETHOD {
|
|
|
|
methods[val] = val
|
|
|
|
}
|
|
|
|
} else {
|
2016-01-18 07:17:42 +00:00
|
|
|
methods[method] = method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
|
|
|
route.methods = methods
|
2015-03-20 05:29:01 +00:00
|
|
|
for k := range methods {
|
2014-06-09 02:11:37 +00:00
|
|
|
if k == "*" {
|
|
|
|
for _, m := range HTTPMETHOD {
|
|
|
|
p.addToRouter(m, pattern, route)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.addToRouter(k, pattern, route)
|
|
|
|
}
|
|
|
|
}
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// Handler add user defined Handler
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ...interface{}) {
|
2017-04-23 17:07:12 +00:00
|
|
|
route := &ControllerInfo{}
|
2014-06-10 10:09:07 +00:00
|
|
|
route.pattern = pattern
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
route.routerType = routerTypeHandler
|
|
|
|
route.handler = h
|
2014-05-16 15:47:15 +00:00
|
|
|
if len(options) > 0 {
|
2014-06-08 12:24:01 +00:00
|
|
|
if _, ok := options[0].(bool); ok {
|
2015-10-12 13:26:18 +00:00
|
|
|
pattern = path.Join(pattern, "?:all(.*)")
|
2013-09-13 03:22:14 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
for _, m := range HTTPMETHOD {
|
|
|
|
p.addToRouter(m, pattern, route)
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// AddAuto router to ControllerRegister.
|
2013-12-21 05:19:24 +00:00
|
|
|
// example beego.AddAuto(&MainContorlller{}),
|
|
|
|
// MainController has method List and Page.
|
2014-01-01 09:57:57 +00:00
|
|
|
// visit the url /main/list to execute List function
|
|
|
|
// /main/page to execute Page function.
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) AddAuto(c ControllerInterface) {
|
2014-06-08 12:24:01 +00:00
|
|
|
p.AddAutoPrefix("/", c)
|
2014-01-01 09:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// AddAutoPrefix Add auto router to ControllerRegister with prefix.
|
2014-01-01 09:57:57 +00:00
|
|
|
// example beego.AddAutoPrefix("/admin",&MainContorlller{}),
|
|
|
|
// MainController has method List and Page.
|
|
|
|
// visit the url /admin/main/list to execute List function
|
|
|
|
// /admin/main/page to execute Page function.
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface) {
|
2014-01-01 09:57:57 +00:00
|
|
|
reflectVal := reflect.ValueOf(c)
|
|
|
|
rt := reflectVal.Type()
|
|
|
|
ct := reflect.Indirect(reflectVal).Type()
|
2014-07-01 08:55:23 +00:00
|
|
|
controllerName := strings.TrimSuffix(ct.Name(), "Controller")
|
2014-01-01 09:57:57 +00:00
|
|
|
for i := 0; i < rt.NumMethod(); i++ {
|
|
|
|
if !utils.InSlice(rt.Method(i).Name, exceptMethod) {
|
2017-04-23 17:07:12 +00:00
|
|
|
route := &ControllerInfo{}
|
2014-06-08 12:24:01 +00:00
|
|
|
route.routerType = routerTypeBeego
|
|
|
|
route.methods = map[string]string{"*": rt.Method(i).Name}
|
|
|
|
route.controllerType = ct
|
2014-07-01 08:55:23 +00:00
|
|
|
pattern := path.Join(prefix, strings.ToLower(controllerName), strings.ToLower(rt.Method(i).Name), "*")
|
|
|
|
patternInit := path.Join(prefix, controllerName, rt.Method(i).Name, "*")
|
2016-01-03 07:40:44 +00:00
|
|
|
patternFix := path.Join(prefix, strings.ToLower(controllerName), strings.ToLower(rt.Method(i).Name))
|
|
|
|
patternFixInit := path.Join(prefix, controllerName, rt.Method(i).Name)
|
2014-06-10 10:09:07 +00:00
|
|
|
route.pattern = pattern
|
2014-06-09 02:11:37 +00:00
|
|
|
for _, m := range HTTPMETHOD {
|
|
|
|
p.addToRouter(m, pattern, route)
|
2014-07-01 08:55:23 +00:00
|
|
|
p.addToRouter(m, patternInit, route)
|
2016-01-03 07:40:44 +00:00
|
|
|
p.addToRouter(m, patternFix, route)
|
|
|
|
p.addToRouter(m, patternFixInit, route)
|
2014-06-09 02:11:37 +00:00
|
|
|
}
|
2014-01-01 09:57:57 +00:00
|
|
|
}
|
2013-07-27 02:25:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// InsertFilter Add a FilterFunc with pattern rule and action constant.
|
2016-08-05 23:20:56 +00:00
|
|
|
// params is for:
|
|
|
|
// 1. setting the returnOnOutput value (false allows multiple filters to execute)
|
|
|
|
// 2. determining whether or not params need to be reset.
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) error {
|
2016-08-05 23:20:56 +00:00
|
|
|
mr := &FilterRouter{
|
|
|
|
tree: NewTree(),
|
|
|
|
pattern: pattern,
|
|
|
|
filterFunc: filter,
|
|
|
|
returnOnOutput: true,
|
|
|
|
}
|
2015-12-09 15:35:04 +00:00
|
|
|
if !BConfig.RouterCaseSensitive {
|
2016-08-05 23:20:56 +00:00
|
|
|
mr.pattern = strings.ToLower(pattern)
|
2014-09-28 14:10:43 +00:00
|
|
|
}
|
2016-08-05 23:20:56 +00:00
|
|
|
|
|
|
|
paramsLen := len(params)
|
|
|
|
if paramsLen > 0 {
|
2014-10-07 20:35:30 +00:00
|
|
|
mr.returnOnOutput = params[0]
|
|
|
|
}
|
2016-08-05 23:20:56 +00:00
|
|
|
if paramsLen > 1 {
|
|
|
|
mr.resetParams = params[1]
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
mr.tree.AddRouter(pattern, true)
|
|
|
|
return p.insertFilterRouter(pos, mr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add Filter into
|
2016-03-15 03:49:23 +00:00
|
|
|
func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) (err error) {
|
|
|
|
if pos < BeforeStatic || pos > FinishRouter {
|
2016-08-17 15:52:34 +00:00
|
|
|
err = fmt.Errorf("can not find your filter position")
|
2016-03-15 03:49:23 +00:00
|
|
|
return
|
|
|
|
}
|
2013-11-25 07:59:40 +00:00
|
|
|
p.enableFilter = true
|
2016-03-15 03:49:23 +00:00
|
|
|
p.filters[pos] = append(p.filters[pos], mr)
|
2013-12-30 07:06:51 +00:00
|
|
|
return nil
|
2013-08-11 16:14:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:01:13 +00:00
|
|
|
// URLFor does another controller handler in this request function.
|
2013-12-21 05:19:24 +00:00
|
|
|
// it can access any controller method.
|
2015-09-08 14:01:13 +00:00
|
|
|
func (p *ControllerRegister) URLFor(endpoint string, values ...interface{}) string {
|
2013-11-10 15:05:07 +00:00
|
|
|
paths := strings.Split(endpoint, ".")
|
|
|
|
if len(paths) <= 1 {
|
2016-03-24 09:39:29 +00:00
|
|
|
logs.Warn("urlfor endpoint must like path.controller.method")
|
2013-11-10 15:05:07 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(values)%2 != 0 {
|
2016-03-24 09:39:29 +00:00
|
|
|
logs.Warn("urlfor params must key-value pair")
|
2013-11-10 15:05:07 +00:00
|
|
|
return ""
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
params := make(map[string]string)
|
2013-11-10 15:05:07 +00:00
|
|
|
if len(values) > 0 {
|
|
|
|
key := ""
|
|
|
|
for k, v := range values {
|
|
|
|
if k%2 == 0 {
|
2014-12-17 07:52:48 +00:00
|
|
|
key = fmt.Sprint(v)
|
2013-11-10 15:05:07 +00:00
|
|
|
} else {
|
2014-12-17 07:52:48 +00:00
|
|
|
params[key] = fmt.Sprint(v)
|
2013-11-10 15:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 16:26:07 +00:00
|
|
|
controllName := strings.Join(paths[:len(paths)-1], "/")
|
2013-11-10 15:05:07 +00:00
|
|
|
methodName := paths[len(paths)-1]
|
2014-11-04 07:29:33 +00:00
|
|
|
for m, t := range p.routers {
|
|
|
|
ok, url := p.geturl(t, "/", controllName, methodName, params, m)
|
2014-06-09 02:11:37 +00:00
|
|
|
if ok {
|
|
|
|
return url
|
|
|
|
}
|
2013-11-10 15:05:07 +00:00
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
return ""
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName string, params map[string]string, httpMethod string) (bool, string) {
|
2015-12-16 15:11:03 +00:00
|
|
|
for _, subtree := range t.fixrouters {
|
|
|
|
u := path.Join(url, subtree.prefix)
|
2014-11-04 07:29:33 +00:00
|
|
|
ok, u := p.geturl(subtree, u, controllName, methodName, params, httpMethod)
|
2014-06-08 12:24:01 +00:00
|
|
|
if ok {
|
|
|
|
return ok, u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.wildcard != nil {
|
2015-09-08 14:01:13 +00:00
|
|
|
u := path.Join(url, urlPlaceholder)
|
2014-11-04 07:29:33 +00:00
|
|
|
ok, u := p.geturl(t.wildcard, u, controllName, methodName, params, httpMethod)
|
2014-06-08 12:24:01 +00:00
|
|
|
if ok {
|
|
|
|
return ok, u
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 15:08:05 +00:00
|
|
|
for _, l := range t.leaves {
|
2017-04-23 17:07:12 +00:00
|
|
|
if c, ok := l.runObject.(*ControllerInfo); ok {
|
2014-09-22 16:26:07 +00:00
|
|
|
if c.routerType == routerTypeBeego &&
|
|
|
|
strings.HasSuffix(path.Join(c.controllerType.PkgPath(), c.controllerType.Name()), controllName) {
|
2014-06-08 12:24:01 +00:00
|
|
|
find := false
|
2014-06-10 17:11:32 +00:00
|
|
|
if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok {
|
2014-06-18 15:32:47 +00:00
|
|
|
if len(c.methods) == 0 {
|
|
|
|
find = true
|
|
|
|
} else if m, ok := c.methods[strings.ToUpper(methodName)]; ok && m == strings.ToUpper(methodName) {
|
2014-06-11 14:45:54 +00:00
|
|
|
find = true
|
|
|
|
} else if m, ok = c.methods["*"]; ok && m == methodName {
|
2014-06-08 12:24:01 +00:00
|
|
|
find = true
|
2013-11-10 15:05:07 +00:00
|
|
|
}
|
2014-06-11 14:45:54 +00:00
|
|
|
}
|
|
|
|
if !find {
|
2014-11-04 07:29:33 +00:00
|
|
|
for m, md := range c.methods {
|
|
|
|
if (m == "*" || m == httpMethod) && md == methodName {
|
2014-06-08 12:24:01 +00:00
|
|
|
find = true
|
|
|
|
}
|
2013-11-10 15:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
if find {
|
2014-06-12 15:08:05 +00:00
|
|
|
if l.regexps == nil {
|
|
|
|
if len(l.wildcards) == 0 {
|
2016-01-17 16:18:21 +00:00
|
|
|
return true, strings.Replace(url, "/"+urlPlaceholder, "", 1) + toURL(params)
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
2014-06-12 15:08:05 +00:00
|
|
|
if len(l.wildcards) == 1 {
|
|
|
|
if v, ok := params[l.wildcards[0]]; ok {
|
|
|
|
delete(params, l.wildcards[0])
|
2016-01-17 16:18:21 +00:00
|
|
|
return true, strings.Replace(url, urlPlaceholder, v, 1) + toURL(params)
|
2013-12-16 14:54:29 +00:00
|
|
|
}
|
2015-09-08 14:01:13 +00:00
|
|
|
return false, ""
|
2013-12-16 14:54:29 +00:00
|
|
|
}
|
2014-06-12 15:08:05 +00:00
|
|
|
if len(l.wildcards) == 3 && l.wildcards[0] == "." {
|
2014-06-08 12:24:01 +00:00
|
|
|
if p, ok := params[":path"]; ok {
|
|
|
|
if e, isok := params[":ext"]; isok {
|
|
|
|
delete(params, ":path")
|
|
|
|
delete(params, ":ext")
|
2016-01-17 16:18:21 +00:00
|
|
|
return true, strings.Replace(url, urlPlaceholder, p+"."+e, -1) + toURL(params)
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
2013-11-26 00:46:46 +00:00
|
|
|
}
|
2013-05-06 16:17:25 +00:00
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
canskip := false
|
2014-06-12 15:08:05 +00:00
|
|
|
for _, v := range l.wildcards {
|
2014-06-08 12:24:01 +00:00
|
|
|
if v == ":" {
|
|
|
|
canskip = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if u, ok := params[v]; ok {
|
2014-07-03 15:40:21 +00:00
|
|
|
delete(params, v)
|
2015-09-08 14:01:13 +00:00
|
|
|
url = strings.Replace(url, urlPlaceholder, u, 1)
|
2014-06-08 12:24:01 +00:00
|
|
|
} else {
|
|
|
|
if canskip {
|
|
|
|
canskip = false
|
|
|
|
continue
|
|
|
|
}
|
2015-09-08 14:01:13 +00:00
|
|
|
return false, ""
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-17 16:18:21 +00:00
|
|
|
return true, url + toURL(params)
|
2015-09-08 14:01:13 +00:00
|
|
|
}
|
|
|
|
var i int
|
|
|
|
var startreg bool
|
|
|
|
regurl := ""
|
|
|
|
for _, v := range strings.Trim(l.regexps.String(), "^$") {
|
|
|
|
if v == '(' {
|
|
|
|
startreg = true
|
|
|
|
continue
|
|
|
|
} else if v == ')' {
|
|
|
|
startreg = false
|
|
|
|
if v, ok := params[l.wildcards[i]]; ok {
|
|
|
|
delete(params, l.wildcards[i])
|
|
|
|
regurl = regurl + v
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
break
|
2013-12-13 13:25:25 +00:00
|
|
|
}
|
2015-09-08 14:01:13 +00:00
|
|
|
} else if !startreg {
|
|
|
|
regurl = string(append([]rune(regurl), v))
|
2013-05-06 16:17:25 +00:00
|
|
|
}
|
2015-09-08 14:01:13 +00:00
|
|
|
}
|
|
|
|
if l.regexps.MatchString(regurl) {
|
|
|
|
ps := strings.Split(regurl, "/")
|
|
|
|
for _, p := range ps {
|
|
|
|
url = strings.Replace(url, urlPlaceholder, p, 1)
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
2016-01-17 16:18:21 +00:00
|
|
|
return true, url + toURL(params)
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
2013-04-11 06:35:43 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
2014-06-12 15:08:05 +00:00
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
2016-03-15 10:37:54 +00:00
|
|
|
func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, pos int) (started bool) {
|
2016-08-05 23:20:56 +00:00
|
|
|
var preFilterParams map[string]string
|
2016-03-15 10:37:54 +00:00
|
|
|
for _, filterR := range p.filters[pos] {
|
2016-03-15 03:49:23 +00:00
|
|
|
if filterR.returnOnOutput && context.ResponseWriter.Started {
|
|
|
|
return true
|
|
|
|
}
|
2016-08-05 23:20:56 +00:00
|
|
|
if filterR.resetParams {
|
|
|
|
preFilterParams = context.Input.Params()
|
|
|
|
}
|
2016-03-15 03:49:23 +00:00
|
|
|
if ok := filterR.ValidRouter(urlPath, context); ok {
|
|
|
|
filterR.filterFunc(context)
|
2016-08-05 23:20:56 +00:00
|
|
|
if filterR.resetParams {
|
|
|
|
context.Input.ResetParams()
|
|
|
|
for k, v := range preFilterParams {
|
|
|
|
context.Input.SetParam(k, v)
|
|
|
|
}
|
|
|
|
}
|
2016-03-15 03:49:23 +00:00
|
|
|
}
|
|
|
|
if filterR.returnOnOutput && context.ResponseWriter.Started {
|
|
|
|
return true
|
2015-12-10 16:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// Implement http.Handler interface.
|
2015-07-27 00:44:58 +00:00
|
|
|
func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2016-01-03 07:40:44 +00:00
|
|
|
startTime := time.Now()
|
2015-12-10 13:59:54 +00:00
|
|
|
var (
|
2017-04-21 12:26:41 +00:00
|
|
|
runRouter reflect.Type
|
|
|
|
findRouter bool
|
|
|
|
runMethod string
|
|
|
|
methodParams []*param.MethodParam
|
2017-04-24 15:23:58 +00:00
|
|
|
routerInfo *ControllerInfo
|
2017-04-21 12:26:41 +00:00
|
|
|
isRunnable bool
|
2015-12-10 13:59:54 +00:00
|
|
|
)
|
|
|
|
context := p.pool.Get().(*beecontext.Context)
|
2015-12-10 16:20:17 +00:00
|
|
|
context.Reset(rw, r)
|
2016-02-24 03:47:55 +00:00
|
|
|
|
2015-12-10 16:20:17 +00:00
|
|
|
defer p.pool.Put(context)
|
2016-09-15 04:16:24 +00:00
|
|
|
if BConfig.RecoverFunc != nil {
|
|
|
|
defer BConfig.RecoverFunc(context)
|
|
|
|
}
|
2015-02-26 15:34:43 +00:00
|
|
|
|
2016-01-12 13:55:02 +00:00
|
|
|
context.Output.EnableGzip = BConfig.EnableGzip
|
2016-01-04 01:27:58 +00:00
|
|
|
|
2015-12-29 18:32:37 +00:00
|
|
|
if BConfig.RunMode == DEV {
|
2015-12-10 16:20:17 +00:00
|
|
|
context.Output.Header("Server", BConfig.ServerName)
|
|
|
|
}
|
|
|
|
|
2016-03-15 03:49:23 +00:00
|
|
|
var urlPath = r.URL.Path
|
|
|
|
|
2015-12-09 15:35:04 +00:00
|
|
|
if !BConfig.RouterCaseSensitive {
|
2016-03-15 23:53:36 +00:00
|
|
|
urlPath = strings.ToLower(urlPath)
|
2014-09-28 14:10:43 +00:00
|
|
|
}
|
2013-12-12 15:26:28 +00:00
|
|
|
|
2016-01-03 07:40:44 +00:00
|
|
|
// filter wrong http method
|
2014-08-04 08:21:34 +00:00
|
|
|
if _, ok := HTTPMETHOD[r.Method]; !ok {
|
2015-12-10 16:20:17 +00:00
|
|
|
http.Error(rw, "Method Not Allowed", 405)
|
2014-08-04 08:21:34 +00:00
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter for static file
|
2016-03-15 10:50:18 +00:00
|
|
|
if len(p.filters[BeforeStatic]) > 0 && p.execFilter(context, urlPath, BeforeStatic) {
|
2014-08-04 07:31:27 +00:00
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
|
|
|
|
serverStaticRouter(context)
|
2016-03-15 10:37:54 +00:00
|
|
|
|
2015-12-10 16:20:17 +00:00
|
|
|
if context.ResponseWriter.Started {
|
2016-01-03 07:40:44 +00:00
|
|
|
findRouter = true
|
2014-08-04 07:31:27 +00:00
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:07:12 +00:00
|
|
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
2016-01-18 15:29:56 +00:00
|
|
|
if BConfig.CopyRequestBody && !context.Input.IsUpload() {
|
|
|
|
context.Input.CopyBody(BConfig.MaxMemory)
|
|
|
|
}
|
|
|
|
context.Input.ParseFormOrMulitForm(BConfig.MaxMemory)
|
|
|
|
}
|
|
|
|
|
2013-12-16 14:56:35 +00:00
|
|
|
// session init
|
2015-12-09 15:35:04 +00:00
|
|
|
if BConfig.WebConfig.Session.SessionOn {
|
2014-11-04 11:07:49 +00:00
|
|
|
var err error
|
2015-12-10 16:20:17 +00:00
|
|
|
context.Input.CruSession, err = GlobalSessions.SessionStart(rw, r)
|
2014-11-04 11:07:49 +00:00
|
|
|
if err != nil {
|
2016-03-24 09:39:29 +00:00
|
|
|
logs.Error(err)
|
2015-02-26 15:34:43 +00:00
|
|
|
exception("503", context)
|
2016-03-15 08:51:21 +00:00
|
|
|
goto Admin
|
2014-11-04 11:07:49 +00:00
|
|
|
}
|
2014-01-11 09:01:33 +00:00
|
|
|
defer func() {
|
2016-01-18 08:11:27 +00:00
|
|
|
if context.Input.CruSession != nil {
|
|
|
|
context.Input.CruSession.SessionRelease(rw)
|
|
|
|
}
|
2014-01-11 09:01:33 +00:00
|
|
|
}()
|
2013-12-16 14:56:35 +00:00
|
|
|
}
|
2016-03-15 10:50:18 +00:00
|
|
|
if len(p.filters[BeforeRouter]) > 0 && p.execFilter(context, urlPath, BeforeRouter) {
|
2013-12-12 15:26:28 +00:00
|
|
|
goto Admin
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
2016-09-15 12:04:45 +00:00
|
|
|
// User can define RunController and RunMethod in filter
|
|
|
|
if context.Input.RunController != nil && context.Input.RunMethod != "" {
|
|
|
|
findRouter = true
|
|
|
|
runMethod = context.Input.RunMethod
|
|
|
|
runRouter = context.Input.RunController
|
|
|
|
} else {
|
|
|
|
routerInfo, findRouter = p.FindRouter(context)
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
//if no matches to url, throw a not found exception
|
2016-01-03 07:40:44 +00:00
|
|
|
if !findRouter {
|
2016-06-05 06:03:20 +00:00
|
|
|
exception("404", context)
|
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
if splat := context.Input.Param(":splat"); splat != "" {
|
|
|
|
for k, v := range strings.Split(splat, "/") {
|
|
|
|
context.Input.SetParam(strconv.Itoa(k), v)
|
2013-12-17 03:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
//execute middleware filters
|
|
|
|
if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) {
|
2013-12-17 03:19:18 +00:00
|
|
|
goto Admin
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2016-09-28 18:21:07 +00:00
|
|
|
//check policies
|
|
|
|
if p.execPolicy(context, urlPath) {
|
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
if routerInfo != nil {
|
2016-10-28 03:44:16 +00:00
|
|
|
//store router pattern into context
|
|
|
|
context.Input.SetData("RouterPattern", routerInfo.pattern)
|
2016-06-05 06:03:20 +00:00
|
|
|
if routerInfo.routerType == routerTypeRESTFul {
|
|
|
|
if _, ok := routerInfo.methods[r.Method]; ok {
|
2015-11-10 06:07:26 +00:00
|
|
|
isRunnable = true
|
2016-06-05 06:03:20 +00:00
|
|
|
routerInfo.runFunction(context)
|
2014-06-08 12:24:01 +00:00
|
|
|
} else {
|
2016-06-05 06:03:20 +00:00
|
|
|
exception("405", context)
|
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
} else if routerInfo.routerType == routerTypeHandler {
|
|
|
|
isRunnable = true
|
|
|
|
routerInfo.handler.ServeHTTP(rw, r)
|
|
|
|
} else {
|
|
|
|
runRouter = routerInfo.controllerType
|
2017-04-21 12:26:41 +00:00
|
|
|
methodParams = routerInfo.methodParams
|
2016-06-05 06:03:20 +00:00
|
|
|
method := r.Method
|
2017-04-23 17:07:12 +00:00
|
|
|
if r.Method == http.MethodPost && context.Input.Query("_method") == http.MethodPost {
|
|
|
|
method = http.MethodPut
|
2016-06-05 06:03:20 +00:00
|
|
|
}
|
2017-04-23 17:07:12 +00:00
|
|
|
if r.Method == http.MethodPost && context.Input.Query("_method") == http.MethodDelete {
|
|
|
|
method = http.MethodDelete
|
2016-06-05 06:03:20 +00:00
|
|
|
}
|
|
|
|
if m, ok := routerInfo.methods[method]; ok {
|
|
|
|
runMethod = m
|
|
|
|
} else if m, ok = routerInfo.methods["*"]; ok {
|
|
|
|
runMethod = m
|
|
|
|
} else {
|
|
|
|
runMethod = method
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
2013-12-18 13:32:25 +00:00
|
|
|
}
|
2016-06-05 06:03:20 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
// also defined runRouter & runMethod from filter
|
2017-06-09 12:53:03 +00:00
|
|
|
if !isRunnable || findRouter {
|
2016-06-05 06:03:20 +00:00
|
|
|
//Invoke the request handler
|
2017-05-19 23:28:47 +00:00
|
|
|
var execController ControllerInterface
|
|
|
|
if routerInfo.initialize != nil {
|
|
|
|
execController = routerInfo.initialize()
|
|
|
|
} else {
|
|
|
|
vc := reflect.New(runRouter)
|
|
|
|
var ok bool
|
|
|
|
execController, ok = vc.Interface().(ControllerInterface)
|
|
|
|
if !ok {
|
|
|
|
panic("controller is not ControllerInterface")
|
|
|
|
}
|
2016-06-05 06:03:20 +00:00
|
|
|
}
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
//call the controller init function
|
2017-05-19 23:06:28 +00:00
|
|
|
execController.Init(context, runRouter.Name(), runMethod, execController)
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
//call prepare function
|
|
|
|
execController.Prepare()
|
2014-05-16 16:12:25 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
|
|
|
if BConfig.WebConfig.EnableXSRF {
|
|
|
|
execController.XSRFToken()
|
2017-04-23 17:07:12 +00:00
|
|
|
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)) {
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.CheckXSRFCookie()
|
2013-08-06 15:21:52 +00:00
|
|
|
}
|
2016-06-05 06:03:20 +00:00
|
|
|
}
|
2013-08-06 15:21:52 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.URLMapping()
|
|
|
|
|
|
|
|
if !context.ResponseWriter.Started {
|
|
|
|
//exec main logic
|
|
|
|
switch runMethod {
|
2017-04-23 17:07:12 +00:00
|
|
|
case http.MethodGet:
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.Get()
|
2017-04-23 17:07:12 +00:00
|
|
|
case http.MethodPost:
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.Post()
|
2017-04-23 17:07:12 +00:00
|
|
|
case http.MethodDelete:
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.Delete()
|
2017-04-23 17:07:12 +00:00
|
|
|
case http.MethodPut:
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.Put()
|
2017-04-23 17:07:12 +00:00
|
|
|
case http.MethodHead:
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.Head()
|
2017-04-23 17:07:12 +00:00
|
|
|
case http.MethodPatch:
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.Patch()
|
2017-04-23 17:07:12 +00:00
|
|
|
case http.MethodOptions:
|
2016-06-05 06:03:20 +00:00
|
|
|
execController.Options()
|
|
|
|
default:
|
|
|
|
if !execController.HandlerFunc(runMethod) {
|
2017-05-19 23:06:28 +00:00
|
|
|
vc := reflect.ValueOf(execController)
|
2016-06-05 06:03:20 +00:00
|
|
|
method := vc.MethodByName(runMethod)
|
2017-04-30 16:28:26 +00:00
|
|
|
in := param.ConvertParams(methodParams, method.Type(), context)
|
2017-04-21 12:26:41 +00:00
|
|
|
out := method.Call(in)
|
|
|
|
|
|
|
|
//For backward compatibility we only handle response if we had incoming methodParams
|
|
|
|
if methodParams != nil {
|
|
|
|
p.handleParamResponse(context, execController, out)
|
|
|
|
}
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
2016-06-05 06:03:20 +00:00
|
|
|
}
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
//render template
|
|
|
|
if !context.ResponseWriter.Started && context.Output.Status == 0 {
|
|
|
|
if BConfig.WebConfig.AutoRender {
|
|
|
|
if err := execController.Render(); err != nil {
|
|
|
|
logs.Error(err)
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
}
|
2013-12-12 15:26:28 +00:00
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
// finish all runRouter. release resource
|
|
|
|
execController.Finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
//execute middleware filters
|
|
|
|
if len(p.filters[AfterExec]) > 0 && p.execFilter(context, urlPath, AfterExec) {
|
|
|
|
goto Admin
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
2016-06-05 06:03:20 +00:00
|
|
|
|
2016-03-15 10:50:18 +00:00
|
|
|
if len(p.filters[FinishRouter]) > 0 && p.execFilter(context, urlPath, FinishRouter) {
|
2016-03-15 03:49:23 +00:00
|
|
|
goto Admin
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
|
2014-04-05 16:18:21 +00:00
|
|
|
Admin:
|
2017-09-13 00:03:46 +00:00
|
|
|
//admin module record QPS
|
|
|
|
|
|
|
|
statusCode := context.ResponseWriter.Status
|
|
|
|
if statusCode == 0 {
|
|
|
|
statusCode = 200
|
|
|
|
}
|
|
|
|
|
2016-01-12 13:55:02 +00:00
|
|
|
if BConfig.Listen.EnableAdmin {
|
2016-03-11 06:14:58 +00:00
|
|
|
timeDur := time.Since(startTime)
|
2017-06-05 23:21:31 +00:00
|
|
|
pattern := ""
|
|
|
|
if routerInfo != nil {
|
|
|
|
pattern = routerInfo.pattern
|
|
|
|
}
|
2017-09-13 00:03:46 +00:00
|
|
|
|
2017-06-05 23:21:31 +00:00
|
|
|
if FilterMonitorFunc(r.Method, r.URL.Path, timeDur, pattern, statusCode) {
|
2016-01-03 07:40:44 +00:00
|
|
|
if runRouter != nil {
|
|
|
|
go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, runRouter.Name(), timeDur)
|
2013-11-15 10:08:53 +00:00
|
|
|
} else {
|
2016-01-03 07:40:44 +00:00
|
|
|
go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, "", timeDur)
|
2013-11-15 10:08:53 +00:00
|
|
|
}
|
2013-11-13 13:37:17 +00:00
|
|
|
}
|
2013-11-13 13:11:03 +00:00
|
|
|
}
|
2014-06-10 10:09:07 +00:00
|
|
|
|
2015-12-29 18:32:37 +00:00
|
|
|
if BConfig.RunMode == DEV || BConfig.Log.AccessLogs {
|
2016-03-11 06:14:58 +00:00
|
|
|
timeDur := time.Since(startTime)
|
2016-01-03 07:40:44 +00:00
|
|
|
var devInfo string
|
2016-06-17 13:39:16 +00:00
|
|
|
|
2016-06-24 13:11:21 +00:00
|
|
|
iswin := (runtime.GOOS == "windows")
|
|
|
|
statusColor := logs.ColorByStatus(iswin, statusCode)
|
|
|
|
methodColor := logs.ColorByMethod(iswin, r.Method)
|
|
|
|
resetColor := logs.ColorByMethod(iswin, "")
|
2017-09-13 00:03:46 +00:00
|
|
|
if BConfig.Log.AccessLogsFormat != "" {
|
|
|
|
record := &logs.AccessLogRecord{
|
|
|
|
RemoteAddr: context.Input.IP(),
|
|
|
|
RequestTime: startTime,
|
|
|
|
RequestMethod: r.Method,
|
|
|
|
Request: fmt.Sprintf("%s %s %s", r.Method, r.RequestURI, r.Proto),
|
|
|
|
ServerProtocol: r.Proto,
|
|
|
|
Host: r.Host,
|
|
|
|
Status: statusCode,
|
|
|
|
ElapsedTime: timeDur,
|
|
|
|
HttpReferrer: r.Header.Get("Referer"),
|
|
|
|
HttpUserAgent: r.Header.Get("User-Agent"),
|
|
|
|
RemoteUser: r.Header.Get("Remote-User"),
|
|
|
|
BodyBytesSent: 0, //@todo this one is missing!
|
|
|
|
}
|
|
|
|
logs.AccessLog(record, BConfig.Log.AccessLogsFormat)
|
|
|
|
}else {
|
|
|
|
if findRouter {
|
|
|
|
if routerInfo != nil {
|
|
|
|
devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s r:%s", context.Input.IP(), statusColor, statusCode,
|
|
|
|
resetColor, timeDur.String(), "match", methodColor, r.Method, resetColor, r.URL.Path,
|
|
|
|
routerInfo.pattern)
|
|
|
|
} else {
|
|
|
|
devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", context.Input.IP(), statusColor, statusCode, resetColor,
|
|
|
|
timeDur.String(), "match", methodColor, r.Method, resetColor, r.URL.Path)
|
|
|
|
}
|
2014-08-18 07:00:27 +00:00
|
|
|
} else {
|
2016-09-15 02:58:46 +00:00
|
|
|
devInfo = fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", context.Input.IP(), statusColor, statusCode, resetColor,
|
2017-09-13 00:03:46 +00:00
|
|
|
timeDur.String(), "nomatch", methodColor, r.Method, resetColor, r.URL.Path)
|
|
|
|
}
|
|
|
|
if iswin {
|
|
|
|
logs.W32Debug(devInfo)
|
|
|
|
} else {
|
|
|
|
logs.Debug(devInfo)
|
2014-08-18 07:00:27 +00:00
|
|
|
}
|
2016-06-24 13:11:21 +00:00
|
|
|
}
|
2014-06-10 10:09:07 +00:00
|
|
|
}
|
2014-07-08 20:20:55 +00:00
|
|
|
// Call WriteHeader if status code has been set changed
|
|
|
|
if context.Output.Status != 0 {
|
2015-12-10 16:20:17 +00:00
|
|
|
context.ResponseWriter.WriteHeader(context.Output.Status)
|
2014-07-08 20:20:55 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 12:26:41 +00:00
|
|
|
func (p *ControllerRegister) handleParamResponse(context *beecontext.Context, execController ControllerInterface, results []reflect.Value) {
|
|
|
|
//looping in reverse order for the case when both error and value are returned and error sets the response status code
|
|
|
|
for i := len(results) - 1; i >= 0; i-- {
|
|
|
|
result := results[i]
|
2017-04-23 18:37:09 +00:00
|
|
|
if result.Kind() != reflect.Interface || !result.IsNil() {
|
2017-04-21 12:26:41 +00:00
|
|
|
resultValue := result.Interface()
|
2017-05-17 17:38:59 +00:00
|
|
|
context.RenderMethodResult(resultValue)
|
2017-04-21 12:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 13:00:49 +00:00
|
|
|
if !context.ResponseWriter.Started && context.Output.Status == 0 {
|
|
|
|
context.Output.SetStatus(200)
|
|
|
|
}
|
2017-04-21 12:26:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 06:03:20 +00:00
|
|
|
// FindRouter Find Router info for URL
|
2017-04-23 17:07:12 +00:00
|
|
|
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *ControllerInfo, isFind bool) {
|
2016-06-05 06:03:20 +00:00
|
|
|
var urlPath = context.Input.URL()
|
|
|
|
if !BConfig.RouterCaseSensitive {
|
|
|
|
urlPath = strings.ToLower(urlPath)
|
|
|
|
}
|
|
|
|
httpMethod := context.Input.Method()
|
|
|
|
if t, ok := p.routers[httpMethod]; ok {
|
|
|
|
runObject := t.Match(urlPath, context)
|
2017-04-23 17:07:12 +00:00
|
|
|
if r, ok := runObject.(*ControllerInfo); ok {
|
2016-06-05 06:03:20 +00:00
|
|
|
return r, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-17 16:18:21 +00:00
|
|
|
func toURL(params map[string]string) string {
|
2014-06-08 12:24:01 +00:00
|
|
|
if len(params) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
u := "?"
|
|
|
|
for k, v := range params {
|
|
|
|
u += k + "=" + v + "&"
|
|
|
|
}
|
|
|
|
return strings.TrimRight(u, "&")
|
|
|
|
}
|