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-12-31 12:47:48 +00:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
2013-04-11 06:35:43 +00:00
|
|
|
"fmt"
|
2013-12-31 12:47:48 +00:00
|
|
|
"net"
|
2012-12-18 07:18:43 +00:00
|
|
|
"net/http"
|
2014-06-09 02:11:37 +00:00
|
|
|
"os"
|
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"
|
2013-11-13 13:11:03 +00:00
|
|
|
"time"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
|
|
|
beecontext "github.com/astaxie/beego/context"
|
|
|
|
"github.com/astaxie/beego/middleware"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2013-11-25 07:59:40 +00:00
|
|
|
const (
|
2013-12-21 05:19:24 +00:00
|
|
|
// default filter execution points
|
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 (
|
|
|
|
routerTypeBeego = iota
|
|
|
|
routerTypeRESTFul
|
|
|
|
routerTypeHandler
|
|
|
|
)
|
|
|
|
|
2013-12-15 18:17:27 +00:00
|
|
|
var (
|
2013-12-21 05:19:24 +00:00
|
|
|
// supported http methods.
|
2014-06-10 17:11:32 +00:00
|
|
|
HTTPMETHOD = map[string]string{
|
|
|
|
"GET": "GET",
|
|
|
|
"POST": "POST",
|
|
|
|
"PUT": "PUT",
|
|
|
|
"DELETE": "DELETE",
|
|
|
|
"PATCH": "PATCH",
|
|
|
|
"OPTIONS": "OPTIONS",
|
|
|
|
"HEAD": "HEAD",
|
|
|
|
"TRACE": "TRACE",
|
|
|
|
"CONNECT": "CONNECT",
|
|
|
|
}
|
2014-01-01 09:57:57 +00:00
|
|
|
// these beego.Controller's methods shouldn't reflect to AutoRouter
|
|
|
|
exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString",
|
|
|
|
"RenderBytes", "Redirect", "Abort", "StopRun", "UrlFor", "ServeJson", "ServeJsonp",
|
|
|
|
"ServeXml", "Input", "ParseForm", "GetString", "GetStrings", "GetInt", "GetBool",
|
|
|
|
"GetFloat", "GetFile", "SaveToFile", "StartSession", "SetSession", "GetSession",
|
|
|
|
"DelSession", "SessionRegenerateID", "DestroySession", "IsAjax", "GetSecureCookie",
|
|
|
|
"SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml",
|
|
|
|
"GetControllerAndAction"}
|
2014-07-03 15:40:21 +00:00
|
|
|
|
|
|
|
url_placeholder = "{{placeholder}}"
|
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
|
|
|
|
2014-03-12 09:20:53 +00:00
|
|
|
// To append a slice's value into "exceptMethod", for controller's methods shouldn't reflect to AutoRouter
|
|
|
|
func ExceptMethodAppend(action string) {
|
|
|
|
exceptMethod = append(exceptMethod, action)
|
|
|
|
}
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
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
|
|
|
|
runfunction FilterFunc
|
|
|
|
routerType int
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// ControllerRegistor containers registered router rules, controller handlers and filters.
|
2012-12-18 07:18:43 +00:00
|
|
|
type ControllerRegistor struct {
|
2014-06-09 02:11:37 +00:00
|
|
|
routers map[string]*Tree
|
2013-12-20 16:34:59 +00:00
|
|
|
enableFilter bool
|
|
|
|
filters map[int][]*FilterRouter
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 12:12:57 +00:00
|
|
|
// NewControllerRegister returns a new ControllerRegistor.
|
|
|
|
func NewControllerRegister() *ControllerRegistor {
|
2013-07-27 02:25:14 +00:00
|
|
|
return &ControllerRegistor{
|
2014-06-09 02:11:37 +00:00
|
|
|
routers: make(map[string]*Tree),
|
2014-06-08 12:24:01 +00:00
|
|
|
filters: make(map[int][]*FilterRouter),
|
2013-07-27 02:25:14 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Add controller handler and pattern rules to ControllerRegistor.
|
|
|
|
// 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")
|
|
|
|
// Add("/api",&RestController{},"get,post:ApiFunc")
|
|
|
|
// Add("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
|
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
|
|
|
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, 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 {
|
|
|
|
panic(colon[1] + " method doesn't exist in the controller " + t.Name())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(v + " is an invalid method mapping. Method doesn't exist " + m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:24:01 +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
|
2014-06-09 02:11:37 +00:00
|
|
|
if len(methods) == 0 {
|
|
|
|
for _, m := range HTTPMETHOD {
|
|
|
|
p.addToRouter(m, pattern, route)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, _ := range methods {
|
|
|
|
if k == "*" {
|
|
|
|
for _, m := range HTTPMETHOD {
|
|
|
|
p.addToRouter(m, pattern, route)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.addToRouter(k, pattern, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ControllerRegistor) addToRouter(method, pattern string, r *controllerInfo) {
|
|
|
|
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
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// only when the Runmode is dev will generate router file in the router/auto.go from the controller
|
|
|
|
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
|
|
|
|
func (p *ControllerRegistor) Include(cList ...ControllerInterface) {
|
|
|
|
if 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()
|
2014-06-09 02:11:37 +00:00
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
|
|
|
panic("you are in dev mode. So please set gopath")
|
|
|
|
}
|
|
|
|
pkgpath := ""
|
|
|
|
|
|
|
|
wgopath := filepath.SplitList(gopath)
|
|
|
|
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 {
|
2014-06-09 09:46:13 +00:00
|
|
|
p.Add(a.Router, c, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add get method
|
|
|
|
// usage:
|
|
|
|
// Get("/", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Get(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("get", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add post method
|
|
|
|
// usage:
|
|
|
|
// Post("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Post(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("post", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add put method
|
|
|
|
// usage:
|
|
|
|
// Put("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Put(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("put", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add delete method
|
|
|
|
// usage:
|
|
|
|
// Delete("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Delete(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("delete", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add head method
|
|
|
|
// usage:
|
|
|
|
// Head("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Head(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("head", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add patch method
|
|
|
|
// usage:
|
|
|
|
// Patch("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Patch(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("patch", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add options method
|
|
|
|
// usage:
|
|
|
|
// Options("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Options(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("options", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add all method
|
|
|
|
// usage:
|
|
|
|
// Any("/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) Any(pattern string, f FilterFunc) {
|
|
|
|
p.AddMethod("*", pattern, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add http method router
|
|
|
|
// usage:
|
|
|
|
// AddMethod("get","/api/:id", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
|
|
|
func (p *ControllerRegistor) AddMethod(method, pattern string, f FilterFunc) {
|
2014-06-10 17:11:32 +00:00
|
|
|
if _, ok := HTTPMETHOD[strings.ToUpper(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)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
route.runfunction = f
|
|
|
|
methods := make(map[string]string)
|
|
|
|
if method == "*" {
|
|
|
|
for _, val := range HTTPMETHOD {
|
|
|
|
methods[val] = val
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-10 17:11:32 +00:00
|
|
|
methods[strings.ToUpper(method)] = strings.ToUpper(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
|
2014-06-09 02:11:37 +00:00
|
|
|
for k, _ := range methods {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// add user defined Handler
|
2014-05-16 15:47:15 +00:00
|
|
|
func (p *ControllerRegistor) Handler(pattern string, h http.Handler, options ...interface{}) {
|
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 := &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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Add auto router to ControllerRegistor.
|
|
|
|
// 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.
|
2013-07-27 02:25:14 +00:00
|
|
|
func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
|
2014-06-08 12:24:01 +00:00
|
|
|
p.AddAutoPrefix("/", c)
|
2014-01-01 09:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add auto router to ControllerRegistor with prefix.
|
|
|
|
// 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.
|
|
|
|
func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface) {
|
|
|
|
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) {
|
2014-06-08 12:24:01 +00:00
|
|
|
route := &controllerInfo{}
|
|
|
|
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, "*")
|
|
|
|
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)
|
2014-06-21 03:44:24 +00:00
|
|
|
p.addToRouter(m, patternfix, route)
|
2014-07-01 08:55:23 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Add a FilterFunc with pattern rule and action constant.
|
2013-12-30 07:06:51 +00:00
|
|
|
func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) error {
|
2014-06-08 12:24:01 +00:00
|
|
|
mr := new(FilterRouter)
|
|
|
|
mr.tree = NewTree()
|
|
|
|
mr.pattern = pattern
|
|
|
|
mr.filterFunc = filter
|
|
|
|
mr.tree.AddRouter(pattern, true)
|
|
|
|
return p.insertFilterRouter(pos, mr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add Filter into
|
|
|
|
func (p *ControllerRegistor) insertFilterRouter(pos int, mr *FilterRouter) error {
|
2013-11-25 08:04:02 +00:00
|
|
|
p.filters[pos] = append(p.filters[pos], mr)
|
2013-11-25 07:59:40 +00:00
|
|
|
p.enableFilter = true
|
2013-12-30 07:06:51 +00:00
|
|
|
return nil
|
2013-08-11 16:14:42 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// UrlFor does another controller handler in this request function.
|
|
|
|
// it can access any controller method.
|
2013-11-10 15:05:07 +00:00
|
|
|
func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
|
|
|
|
paths := strings.Split(endpoint, ".")
|
|
|
|
if len(paths) <= 1 {
|
|
|
|
Warn("urlfor endpoint must like path.controller.method")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(values)%2 != 0 {
|
|
|
|
Warn("urlfor params must key-value pair")
|
|
|
|
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 {
|
|
|
|
key = v
|
|
|
|
} else {
|
2014-06-08 12:24:01 +00:00
|
|
|
params[key] = 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-06-09 02:11:37 +00:00
|
|
|
for _, t := range p.routers {
|
|
|
|
ok, url := p.geturl(t, "/", controllName, methodName, params)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName string, params map[string]string) (bool, string) {
|
|
|
|
for k, subtree := range t.fixrouters {
|
|
|
|
u := path.Join(url, k)
|
|
|
|
ok, u := p.geturl(subtree, u, controllName, methodName, params)
|
|
|
|
if ok {
|
|
|
|
return ok, u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.wildcard != nil {
|
2014-07-03 15:40:21 +00:00
|
|
|
url = path.Join(url, url_placeholder)
|
2014-06-08 12:24:01 +00:00
|
|
|
ok, u := p.geturl(t.wildcard, url, controllName, methodName, params)
|
|
|
|
if ok {
|
|
|
|
return ok, u
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 15:08:05 +00:00
|
|
|
for _, l := range t.leaves {
|
|
|
|
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-06-08 12:24:01 +00:00
|
|
|
for _, md := range c.methods {
|
|
|
|
if md == methodName {
|
|
|
|
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 {
|
2014-08-11 14:19:59 +00:00
|
|
|
return true, strings.Replace(url, "/"+url_placeholder, "", 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])
|
2014-07-03 15:40:21 +00:00
|
|
|
return true, strings.Replace(url, url_placeholder, v, 1) + tourl(params)
|
|
|
|
} else {
|
|
|
|
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")
|
2014-07-03 15:40:21 +00:00
|
|
|
return true, strings.Replace(url, url_placeholder, 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)
|
|
|
|
url = strings.Replace(url, url_placeholder, u, 1)
|
2014-06-08 12:24:01 +00:00
|
|
|
} else {
|
|
|
|
if canskip {
|
|
|
|
canskip = false
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 15:40:21 +00:00
|
|
|
return true, url + tourl(params)
|
2013-12-13 13:25:25 +00:00
|
|
|
} else {
|
2014-06-08 12:24:01 +00:00
|
|
|
var i int
|
|
|
|
var startreg bool
|
2014-06-12 15:08:05 +00:00
|
|
|
regurl := ""
|
|
|
|
for _, v := range strings.Trim(l.regexps.String(), "^$") {
|
2014-06-08 12:24:01 +00:00
|
|
|
if v == '(' {
|
|
|
|
startreg = true
|
|
|
|
continue
|
|
|
|
} else if v == ')' {
|
|
|
|
startreg = false
|
2014-06-12 15:08:05 +00:00
|
|
|
if v, ok := params[l.wildcards[i]]; ok {
|
|
|
|
delete(params, l.wildcards[i])
|
|
|
|
regurl = regurl + v
|
2014-06-08 12:24:01 +00:00
|
|
|
i++
|
|
|
|
} else {
|
2013-12-13 13:25:25 +00:00
|
|
|
break
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
} else if !startreg {
|
2014-06-12 15:08:05 +00:00
|
|
|
regurl = string(append([]rune(regurl), v))
|
2013-12-13 13:25:25 +00:00
|
|
|
}
|
2013-05-06 16:17:25 +00:00
|
|
|
}
|
2014-06-12 15:08:05 +00:00
|
|
|
if l.regexps.MatchString(regurl) {
|
2014-07-03 15:40:21 +00:00
|
|
|
ps := strings.Split(regurl, "/")
|
|
|
|
for _, p := range ps {
|
|
|
|
url = strings.Replace(url, url_placeholder, p, 1)
|
2014-06-18 15:32:47 +00:00
|
|
|
}
|
2014-07-03 15:40:21 +00:00
|
|
|
return true, url + tourl(params)
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
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, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement http.Handler interface.
|
|
|
|
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
defer p.recoverPanic(rw, r)
|
2013-11-13 13:11:03 +00:00
|
|
|
starttime := time.Now()
|
2013-12-17 03:19:18 +00:00
|
|
|
var runrouter reflect.Type
|
2013-11-13 13:11:03 +00:00
|
|
|
var findrouter bool
|
2013-12-17 03:19:18 +00:00
|
|
|
var runMethod 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
|
|
|
var routerInfo *controllerInfo
|
2013-11-13 13:11:03 +00:00
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
w := &responseWriter{writer: rw}
|
2014-06-11 03:12:17 +00:00
|
|
|
|
|
|
|
if RunMode == "dev" {
|
|
|
|
w.Header().Set("Server", BeegoServerName)
|
|
|
|
}
|
2013-12-18 14:33:21 +00:00
|
|
|
|
|
|
|
// init context
|
2013-12-20 16:34:59 +00:00
|
|
|
context := &beecontext.Context{
|
|
|
|
ResponseWriter: w,
|
|
|
|
Request: r,
|
|
|
|
Input: beecontext.NewInput(r),
|
|
|
|
Output: beecontext.NewOutput(),
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
2013-12-20 16:34:59 +00:00
|
|
|
context.Output.Context = context
|
|
|
|
context.Output.EnableGzip = EnableGzip
|
2013-12-18 14:33:21 +00:00
|
|
|
|
|
|
|
// defined filter function
|
2013-12-12 15:26:28 +00:00
|
|
|
do_filter := func(pos int) (started bool) {
|
|
|
|
if p.enableFilter {
|
|
|
|
if l, ok := p.filters[pos]; ok {
|
|
|
|
for _, filterR := range l {
|
|
|
|
if ok, p := filterR.ValidRouter(r.URL.Path); ok {
|
|
|
|
context.Input.Params = p
|
|
|
|
filterR.filterFunc(context)
|
|
|
|
if w.started {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-15 18:17:27 +00:00
|
|
|
return false
|
2013-12-12 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 08:21:34 +00:00
|
|
|
// filter wrong httpmethod
|
|
|
|
if _, ok := HTTPMETHOD[r.Method]; !ok {
|
|
|
|
http.Error(w, "Method Not Allowed", 405)
|
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter for static file
|
2014-08-04 07:31:27 +00:00
|
|
|
if do_filter(BeforeStatic) {
|
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
|
|
|
|
serverStaticRouter(context)
|
|
|
|
if w.started {
|
2014-08-18 06:35:39 +00:00
|
|
|
findrouter = true
|
2014-08-04 07:31:27 +00:00
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
|
2013-12-16 14:56:35 +00:00
|
|
|
// session init
|
|
|
|
if SessionOn {
|
|
|
|
context.Input.CruSession = GlobalSessions.SessionStart(w, r)
|
2014-01-11 09:01:33 +00:00
|
|
|
defer func() {
|
|
|
|
context.Input.CruSession.SessionRelease(w)
|
|
|
|
}()
|
2013-12-16 14:56:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 17:11:32 +00:00
|
|
|
if r.Method != "GET" && r.Method != "HEAD" {
|
2014-04-05 16:18:21 +00:00
|
|
|
if CopyRequestBody && !context.Input.IsUpload() {
|
|
|
|
context.Input.CopyBody()
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
2014-04-05 16:18:21 +00:00
|
|
|
context.Input.ParseFormOrMulitForm(MaxMemory)
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 16:18:21 +00:00
|
|
|
if do_filter(BeforeRouter) {
|
2013-12-12 15:26:28 +00:00
|
|
|
goto Admin
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2014-03-29 06:59:55 +00:00
|
|
|
if context.Input.RunController != nil && context.Input.RunMethod != "" {
|
2014-03-29 06:55:34 +00:00
|
|
|
findrouter = true
|
|
|
|
runMethod = context.Input.RunMethod
|
|
|
|
runrouter = context.Input.RunController
|
|
|
|
}
|
|
|
|
|
|
|
|
if !findrouter {
|
2014-06-10 17:11:32 +00:00
|
|
|
if t, ok := p.routers[r.Method]; ok {
|
2014-07-01 08:55:23 +00:00
|
|
|
runObject, p := t.Match(r.URL.Path)
|
2014-06-09 02:11:37 +00:00
|
|
|
if r, ok := runObject.(*controllerInfo); ok {
|
|
|
|
routerInfo = r
|
|
|
|
findrouter = true
|
|
|
|
if splat, ok := p[":splat"]; ok {
|
|
|
|
splatlist := strings.Split(splat, "/")
|
|
|
|
for k, v := range splatlist {
|
|
|
|
p[strconv.Itoa(k)] = v
|
|
|
|
}
|
2013-12-17 03:19:18 +00:00
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
context.Input.Params = p
|
2013-12-17 03:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
|
2013-12-17 03:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//if no matches to url, throw a not found exception
|
|
|
|
if !findrouter {
|
|
|
|
middleware.Exception("404", rw, r, "")
|
|
|
|
goto Admin
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2013-12-17 03:19:18 +00:00
|
|
|
if findrouter {
|
2012-12-18 07:18:43 +00:00
|
|
|
//execute middleware filters
|
2013-12-12 15:26:28 +00:00
|
|
|
if do_filter(BeforeExec) {
|
|
|
|
goto Admin
|
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
|
|
|
isRunable := false
|
|
|
|
if routerInfo != nil {
|
|
|
|
if routerInfo.routerType == routerTypeRESTFul {
|
2014-06-10 17:11:32 +00:00
|
|
|
if _, ok := routerInfo.methods[r.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
|
|
|
isRunable = true
|
|
|
|
routerInfo.runfunction(context)
|
|
|
|
} else {
|
|
|
|
middleware.Exception("405", rw, r, "Method Not Allowed")
|
|
|
|
goto Admin
|
|
|
|
}
|
|
|
|
} else if routerInfo.routerType == routerTypeHandler {
|
|
|
|
isRunable = true
|
|
|
|
routerInfo.handler.ServeHTTP(rw, r)
|
2014-06-08 12:24:01 +00:00
|
|
|
} else {
|
|
|
|
runrouter = routerInfo.controllerType
|
2014-06-10 17:11:32 +00:00
|
|
|
method := r.Method
|
|
|
|
if r.Method == "POST" && context.Input.Query("_method") == "PUT" {
|
|
|
|
method = "PUT"
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
2014-06-10 17:11:32 +00:00
|
|
|
if r.Method == "POST" && context.Input.Query("_method") == "DELETE" {
|
|
|
|
method = "DELETE"
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
|
|
|
if m, ok := routerInfo.methods[method]; ok {
|
|
|
|
runMethod = m
|
|
|
|
} else if m, ok = routerInfo.methods["*"]; ok {
|
|
|
|
runMethod = m
|
|
|
|
} else {
|
2014-06-10 17:11:32 +00:00
|
|
|
runMethod = method
|
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
|
|
|
}
|
2013-12-18 13:32:25 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// also defined runrouter & runMethod from filter
|
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 !isRunable {
|
|
|
|
//Invoke the request handler
|
|
|
|
vc := reflect.New(runrouter)
|
|
|
|
execController, ok := vc.Interface().(ControllerInterface)
|
|
|
|
if !ok {
|
|
|
|
panic("controller is not ControllerInterface")
|
|
|
|
}
|
|
|
|
|
|
|
|
//call the controller init function
|
|
|
|
execController.Init(context, runrouter.Name(), runMethod, vc.Interface())
|
2012-12-18 07:18:43 +00:00
|
|
|
|
2014-05-16 16:12:25 +00:00
|
|
|
//call prepare function
|
|
|
|
execController.Prepare()
|
|
|
|
|
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 XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
|
|
|
if EnableXSRF {
|
|
|
|
execController.XsrfToken()
|
|
|
|
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
2014-09-03 01:25:34 +00:00
|
|
|
(r.Method == "POST" && (context.Input.Query("_method") == "DELETE" || context.Input.Query("_method") == "PUT")) {
|
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
|
|
|
execController.CheckXsrfCookie()
|
|
|
|
}
|
2013-08-06 15:21:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 03:02:41 +00:00
|
|
|
execController.URLMapping()
|
|
|
|
|
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 !w.started {
|
|
|
|
//exec main logic
|
|
|
|
switch runMethod {
|
2014-06-10 17:11:32 +00:00
|
|
|
case "GET":
|
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
|
|
|
execController.Get()
|
2014-06-10 17:11:32 +00:00
|
|
|
case "POST":
|
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
|
|
|
execController.Post()
|
2014-06-10 17:11:32 +00:00
|
|
|
case "DELETE":
|
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
|
|
|
execController.Delete()
|
2014-06-10 17:11:32 +00:00
|
|
|
case "PUT":
|
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
|
|
|
execController.Put()
|
2014-06-10 17:11:32 +00:00
|
|
|
case "HEAD":
|
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
|
|
|
execController.Head()
|
2014-06-10 17:11:32 +00:00
|
|
|
case "PATCH":
|
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
|
|
|
execController.Patch()
|
2014-06-10 17:11:32 +00:00
|
|
|
case "OPTIONS":
|
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
|
|
|
execController.Options()
|
|
|
|
default:
|
2014-06-10 03:02:41 +00:00
|
|
|
if !execController.HandlerFunc(runMethod) {
|
|
|
|
in := make([]reflect.Value, 0)
|
|
|
|
method := vc.MethodByName(runMethod)
|
|
|
|
method.Call(in)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//render template
|
2014-07-18 05:29:54 +00:00
|
|
|
if !w.started && context.Output.Status == 0 {
|
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 AutoRender {
|
|
|
|
if err := execController.Render(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-09 16:00:11 +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
|
|
|
// finish all runrouter. release resource
|
|
|
|
execController.Finish()
|
|
|
|
}
|
2013-12-12 15:26:28 +00:00
|
|
|
|
2013-08-11 16:14:42 +00:00
|
|
|
//execute middleware filters
|
2013-12-12 15:26:28 +00:00
|
|
|
if do_filter(AfterExec) {
|
|
|
|
goto Admin
|
2013-08-11 16:14:42 +00:00
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 15:26:28 +00:00
|
|
|
do_filter(FinishRouter)
|
2014-06-08 12:24:01 +00:00
|
|
|
|
2014-04-05 16:18:21 +00:00
|
|
|
Admin:
|
2014-06-10 10:09:07 +00:00
|
|
|
timeend := time.Since(starttime)
|
2013-11-13 13:11:03 +00:00
|
|
|
//admin module record QPS
|
|
|
|
if EnableAdmin {
|
2014-06-10 17:11:32 +00:00
|
|
|
if FilterMonitorFunc(r.Method, r.URL.Path, timeend) {
|
2013-11-15 10:08:53 +00:00
|
|
|
if runrouter != nil {
|
2014-06-10 17:11:32 +00:00
|
|
|
go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, runrouter.Name(), timeend)
|
2013-11-15 10:08:53 +00:00
|
|
|
} else {
|
2014-06-10 17:11:32 +00:00
|
|
|
go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, "", timeend)
|
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
|
|
|
|
|
|
|
if RunMode == "dev" {
|
2014-07-09 01:38:36 +00:00
|
|
|
var devinfo string
|
2014-06-10 10:09:07 +00:00
|
|
|
if findrouter {
|
2014-08-18 07:00:27 +00:00
|
|
|
if routerInfo != nil {
|
|
|
|
devinfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s | % -40s |", r.Method, r.URL.Path, timeend.String(), "match", routerInfo.pattern)
|
|
|
|
} else {
|
|
|
|
devinfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeend.String(), "match")
|
|
|
|
}
|
2014-06-10 10:09:07 +00:00
|
|
|
} else {
|
2014-08-18 06:35:39 +00:00
|
|
|
devinfo = fmt.Sprintf("| % -10s | % -40s | % -16s | % -10s |", r.Method, r.URL.Path, timeend.String(), "notmatch")
|
2014-06-10 10:09:07 +00:00
|
|
|
}
|
2014-08-20 03:54:25 +00:00
|
|
|
Debug(devinfo)
|
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 {
|
|
|
|
w.writer.WriteHeader(context.Output.Status)
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
func (p *ControllerRegistor) recoverPanic(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
if err == USERSTOPRUN {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := err.(middleware.HTTPException); ok {
|
|
|
|
// catch intented errors, only for HTTP 4XX and 5XX
|
|
|
|
} else {
|
|
|
|
if RunMode == "dev" {
|
|
|
|
if !RecoverPanic {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
if ErrorsShow {
|
|
|
|
if handler, ok := middleware.ErrorMaps[fmt.Sprint(err)]; ok {
|
|
|
|
handler(rw, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var stack string
|
|
|
|
Critical("the request url is ", r.URL.Path)
|
|
|
|
Critical("Handler crashed with error", err)
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2014-08-30 19:27:21 +00:00
|
|
|
Critical(fmt.Sprintf("%s:%d", file, line))
|
|
|
|
stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
|
|
|
middleware.ShowErr(err, rw, r, stack)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !RecoverPanic {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
// in production model show all infomation
|
|
|
|
if ErrorsShow {
|
|
|
|
handler := p.getErrorHandler(fmt.Sprint(err))
|
|
|
|
handler(rw, r)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
Critical("the request url is ", r.URL.Path)
|
|
|
|
Critical("Handler crashed with error", err)
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2014-08-30 19:27:21 +00:00
|
|
|
Critical(fmt.Sprintf("%s:%d", file, line))
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// there always should be error handler that sets error code accordingly for all unhandled errors.
|
|
|
|
// in order to have custom UI for error page it's necessary to override "500" error.
|
2013-12-06 05:47:34 +00:00
|
|
|
func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
handler := middleware.SimpleServerError
|
|
|
|
ok := true
|
|
|
|
if errorCode != "" {
|
|
|
|
handler, ok = middleware.ErrorMaps[errorCode]
|
|
|
|
if !ok {
|
|
|
|
handler, ok = middleware.ErrorMaps["500"]
|
|
|
|
}
|
|
|
|
if !ok || handler == nil {
|
|
|
|
handler = middleware.SimpleServerError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
//responseWriter is a wrapper for the http.ResponseWriter
|
|
|
|
//started set to true if response was written to then don't execute other handler
|
|
|
|
type responseWriter struct {
|
2014-04-05 16:18:21 +00:00
|
|
|
writer http.ResponseWriter
|
|
|
|
started bool
|
|
|
|
status int
|
2012-12-18 07:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns the header map that will be sent by WriteHeader.
|
|
|
|
func (w *responseWriter) Header() http.Header {
|
|
|
|
return w.writer.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the data to the connection as part of an HTTP reply,
|
2013-12-21 05:19:24 +00:00
|
|
|
// and sets `started` to true.
|
|
|
|
// started means the response has sent out.
|
2012-12-18 07:18:43 +00:00
|
|
|
func (w *responseWriter) Write(p []byte) (int, error) {
|
|
|
|
w.started = true
|
|
|
|
return w.writer.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader sends an HTTP response header with status code,
|
2013-12-21 05:19:24 +00:00
|
|
|
// and sets `started` to true.
|
2012-12-18 07:18:43 +00:00
|
|
|
func (w *responseWriter) WriteHeader(code int) {
|
|
|
|
w.status = code
|
|
|
|
w.started = true
|
|
|
|
w.writer.WriteHeader(code)
|
|
|
|
}
|
2013-12-31 12:47:48 +00:00
|
|
|
|
|
|
|
// hijacker for http
|
|
|
|
func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
hj, ok := w.writer.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, errors.New("webserver doesn't support hijacking")
|
|
|
|
}
|
|
|
|
return hj.Hijack()
|
|
|
|
}
|
2014-06-08 12:24:01 +00:00
|
|
|
|
|
|
|
func tourl(params map[string]string) string {
|
|
|
|
if len(params) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
u := "?"
|
|
|
|
for k, v := range params {
|
|
|
|
u += k + "=" + v + "&"
|
|
|
|
}
|
|
|
|
return strings.TrimRight(u, "&")
|
|
|
|
}
|