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"
|
|
|
|
|
"net/url"
|
2013-06-24 15:24:13 +00:00
|
|
|
|
"os"
|
2012-12-18 07:18:43 +00:00
|
|
|
|
"reflect"
|
|
|
|
|
"regexp"
|
|
|
|
|
"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 (
|
|
|
|
|
BeforeRouter = iota
|
|
|
|
|
AfterStatic
|
|
|
|
|
BeforeExec
|
|
|
|
|
AfterExec
|
|
|
|
|
FinishRouter
|
|
|
|
|
)
|
|
|
|
|
|
2013-12-15 18:17:27 +00:00
|
|
|
|
var (
|
|
|
|
|
HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
|
|
|
|
|
)
|
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
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
type controllerInfo struct {
|
|
|
|
|
pattern string
|
|
|
|
|
regex *regexp.Regexp
|
|
|
|
|
params map[int]string
|
|
|
|
|
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
|
2013-08-11 15:14:30 +00:00
|
|
|
|
hasMethod bool
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ControllerRegistor struct {
|
2013-12-18 16:43:29 +00:00
|
|
|
|
routers []*controllerInfo // regexp router storage
|
|
|
|
|
fixrouters []*controllerInfo // fixed router storage
|
2013-12-18 14:33:21 +00:00
|
|
|
|
enableFilter bool
|
|
|
|
|
filters map[int][]*FilterRouter
|
|
|
|
|
enableAuto bool
|
|
|
|
|
autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type
|
2013-12-18 16:43:29 +00:00
|
|
|
|
contextBuffer chan *beecontext.Context //context buffer pool
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewControllerRegistor() *ControllerRegistor {
|
2013-07-27 02:25:14 +00:00
|
|
|
|
return &ControllerRegistor{
|
2013-12-18 14:33:21 +00:00
|
|
|
|
routers: make([]*controllerInfo, 0),
|
|
|
|
|
autoRouter: make(map[string]map[string]reflect.Type),
|
|
|
|
|
filters: make(map[int][]*FilterRouter),
|
2013-12-18 16:43:29 +00:00
|
|
|
|
contextBuffer: make(chan *beecontext.Context, 1000),
|
2013-07-27 02:25:14 +00:00
|
|
|
|
}
|
2012-12-18 07:18:43 +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
|
|
|
|
//methods support like this:
|
|
|
|
|
//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")
|
|
|
|
|
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingMethods ...string) {
|
2012-12-18 07:18:43 +00:00
|
|
|
|
parts := strings.Split(pattern, "/")
|
|
|
|
|
|
|
|
|
|
j := 0
|
|
|
|
|
params := make(map[int]string)
|
|
|
|
|
for i, part := range parts {
|
|
|
|
|
if strings.HasPrefix(part, ":") {
|
2013-04-09 15:33:48 +00:00
|
|
|
|
expr := "(.+)"
|
2012-12-18 07:18:43 +00:00
|
|
|
|
//a user may choose to override the defult expression
|
2013-05-28 08:43:23 +00:00
|
|
|
|
// similar to expressjs: ‘/user/:id([0-9]+)’
|
2012-12-18 07:18:43 +00:00
|
|
|
|
if index := strings.Index(part, "("); index != -1 {
|
|
|
|
|
expr = part[index:]
|
|
|
|
|
part = part[:index]
|
2013-04-09 15:33:48 +00:00
|
|
|
|
//match /user/:id:int ([0-9]+)
|
2013-04-10 02:20:23 +00:00
|
|
|
|
//match /post/:username:string ([\w]+)
|
2013-04-09 15:33:48 +00:00
|
|
|
|
} else if lindex := strings.LastIndex(part, ":"); lindex != 0 {
|
|
|
|
|
switch part[lindex:] {
|
2013-04-09 15:51:19 +00:00
|
|
|
|
case ":int":
|
2013-04-09 15:33:48 +00:00
|
|
|
|
expr = "([0-9]+)"
|
2013-04-09 15:51:19 +00:00
|
|
|
|
part = part[:lindex]
|
2013-04-10 02:20:23 +00:00
|
|
|
|
case ":string":
|
2013-04-09 15:33:48 +00:00
|
|
|
|
expr = `([\w]+)`
|
2013-04-09 15:51:19 +00:00
|
|
|
|
part = part[:lindex]
|
2013-04-09 15:33:48 +00:00
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
params[j] = part
|
|
|
|
|
parts[i] = expr
|
|
|
|
|
j++
|
|
|
|
|
}
|
2013-04-09 15:33:48 +00:00
|
|
|
|
if strings.HasPrefix(part, "*") {
|
|
|
|
|
expr := "(.+)"
|
|
|
|
|
if part == "*.*" {
|
|
|
|
|
params[j] = ":path"
|
2013-04-09 15:51:19 +00:00
|
|
|
|
parts[i] = "([^.]+).([^.]+)"
|
2013-04-09 15:33:48 +00:00
|
|
|
|
j++
|
|
|
|
|
params[j] = ":ext"
|
|
|
|
|
j++
|
|
|
|
|
} else {
|
|
|
|
|
params[j] = ":splat"
|
|
|
|
|
parts[i] = expr
|
|
|
|
|
j++
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-13 03:22:14 +00:00
|
|
|
|
//url like someprefix:id(xxx).html
|
|
|
|
|
if strings.Contains(part, ":") && strings.Contains(part, "(") && strings.Contains(part, ")") {
|
|
|
|
|
var out []rune
|
|
|
|
|
var start bool
|
|
|
|
|
var startexp bool
|
|
|
|
|
var param []rune
|
|
|
|
|
var expt []rune
|
|
|
|
|
for _, v := range part {
|
|
|
|
|
if start {
|
|
|
|
|
if v != '(' {
|
|
|
|
|
param = append(param, v)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if startexp {
|
|
|
|
|
if v != ')' {
|
|
|
|
|
expt = append(expt, v)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if v == ':' {
|
|
|
|
|
param = make([]rune, 0)
|
|
|
|
|
param = append(param, ':')
|
|
|
|
|
start = true
|
|
|
|
|
} else if v == '(' {
|
|
|
|
|
startexp = true
|
|
|
|
|
start = false
|
|
|
|
|
params[j] = string(param)
|
|
|
|
|
j++
|
|
|
|
|
expt = make([]rune, 0)
|
|
|
|
|
expt = append(expt, '(')
|
|
|
|
|
} else if v == ')' {
|
|
|
|
|
startexp = false
|
|
|
|
|
expt = append(expt, ')')
|
|
|
|
|
out = append(out, expt...)
|
|
|
|
|
} else {
|
|
|
|
|
out = append(out, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
parts[i] = string(out)
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
2013-07-25 08:08:18 +00:00
|
|
|
|
reflectVal := reflect.ValueOf(c)
|
|
|
|
|
t := reflect.Indirect(reflectVal).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 := 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 {
|
2013-12-05 03:27:29 +00:00
|
|
|
|
panic("method mapping format is invalid")
|
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
|
|
|
|
}
|
|
|
|
|
comma := strings.Split(colon[0], ",")
|
|
|
|
|
for _, m := range comma {
|
2013-12-12 14:25:08 +00:00
|
|
|
|
if m == "*" || utils.InSlice(strings.ToLower(m), HTTPMETHOD) {
|
2013-07-25 08:08:18 +00:00
|
|
|
|
if val := reflectVal.MethodByName(colon[1]); val.IsValid() {
|
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[strings.ToLower(m)] = colon[1]
|
|
|
|
|
} else {
|
2013-12-05 03:27:29 +00:00
|
|
|
|
panic(colon[1] + " method doesn't exist in the controller " + t.Name())
|
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
|
|
|
|
}
|
|
|
|
|
} else {
|
2013-12-05 03:27:29 +00:00
|
|
|
|
panic(v + " is an invalid method mapping. Method doesn't exist " + m)
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
if j == 0 {
|
|
|
|
|
//now create the Route
|
|
|
|
|
route := &controllerInfo{}
|
|
|
|
|
route.pattern = pattern
|
|
|
|
|
route.controllerType = t
|
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
|
|
|
|
route.methods = methods
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if len(methods) > 0 {
|
|
|
|
|
route.hasMethod = true
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
p.fixrouters = append(p.fixrouters, route)
|
|
|
|
|
} else { // add regexp routers
|
|
|
|
|
//recreate the url pattern, with parameters replaced
|
|
|
|
|
//by regular expressions. then compile the regex
|
|
|
|
|
pattern = strings.Join(parts, "/")
|
|
|
|
|
regex, regexErr := regexp.Compile(pattern)
|
|
|
|
|
if regexErr != nil {
|
|
|
|
|
//TODO add error handling here to avoid panic
|
|
|
|
|
panic(regexErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//now create the Route
|
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
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
route := &controllerInfo{}
|
|
|
|
|
route.regex = regex
|
|
|
|
|
route.params = params
|
|
|
|
|
route.pattern = pattern
|
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
|
|
|
|
route.methods = methods
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if len(methods) > 0 {
|
|
|
|
|
route.hasMethod = true
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
route.controllerType = t
|
|
|
|
|
p.routers = append(p.routers, route)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 03:19:18 +00:00
|
|
|
|
// add auto router to controller
|
2013-12-18 16:43:29 +00:00
|
|
|
|
// example beego.AddAuto(&MainContorlller{})
|
|
|
|
|
// MainController has method List and Page
|
|
|
|
|
// you can visit the url /main/list to exec List function
|
|
|
|
|
// /main/page to exec Page function
|
2013-07-27 02:25:14 +00:00
|
|
|
|
func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
p.enableAuto = true
|
2013-07-27 02:25:14 +00:00
|
|
|
|
reflectVal := reflect.ValueOf(c)
|
|
|
|
|
rt := reflectVal.Type()
|
|
|
|
|
ct := reflect.Indirect(reflectVal).Type()
|
|
|
|
|
firstParam := strings.ToLower(strings.TrimSuffix(ct.Name(), "Controller"))
|
|
|
|
|
if _, ok := p.autoRouter[firstParam]; ok {
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
p.autoRouter[firstParam] = make(map[string]reflect.Type)
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < rt.NumMethod(); i++ {
|
|
|
|
|
p.autoRouter[firstParam][rt.Method(i).Name] = ct
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 07:59:40 +00:00
|
|
|
|
func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) {
|
|
|
|
|
mr := buildFilter(pattern, filter)
|
|
|
|
|
switch action {
|
2013-11-26 03:05:12 +00:00
|
|
|
|
case "BeforeRouter":
|
2013-11-25 07:59:40 +00:00
|
|
|
|
p.filters[BeforeRouter] = append(p.filters[BeforeRouter], mr)
|
|
|
|
|
case "AfterStatic":
|
|
|
|
|
p.filters[AfterStatic] = append(p.filters[AfterStatic], mr)
|
|
|
|
|
case "BeforeExec":
|
|
|
|
|
p.filters[BeforeExec] = append(p.filters[BeforeExec], mr)
|
|
|
|
|
case "AfterExec":
|
|
|
|
|
p.filters[AfterExec] = append(p.filters[AfterExec], mr)
|
|
|
|
|
case "FinishRouter":
|
|
|
|
|
p.filters[FinishRouter] = append(p.filters[FinishRouter], mr)
|
|
|
|
|
}
|
|
|
|
|
p.enableFilter = true
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 08:04:02 +00:00
|
|
|
|
func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) {
|
2013-11-25 07:59:40 +00:00
|
|
|
|
mr := buildFilter(pattern, filter)
|
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-08-11 16:14:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
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 ""
|
|
|
|
|
}
|
|
|
|
|
urlv := url.Values{}
|
|
|
|
|
if len(values) > 0 {
|
|
|
|
|
key := ""
|
|
|
|
|
for k, v := range values {
|
|
|
|
|
if k%2 == 0 {
|
|
|
|
|
key = v
|
|
|
|
|
} else {
|
|
|
|
|
urlv.Set(key, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
controllName := strings.Join(paths[:len(paths)-1], ".")
|
|
|
|
|
methodName := paths[len(paths)-1]
|
|
|
|
|
for _, route := range p.fixrouters {
|
|
|
|
|
if route.controllerType.Name() == controllName {
|
|
|
|
|
var finded bool
|
2013-12-12 14:25:08 +00:00
|
|
|
|
if utils.InSlice(strings.ToLower(methodName), HTTPMETHOD) {
|
2013-11-10 15:05:07 +00:00
|
|
|
|
if route.hasMethod {
|
|
|
|
|
if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName {
|
|
|
|
|
finded = false
|
|
|
|
|
} else if m, ok = route.methods["*"]; ok && m != methodName {
|
|
|
|
|
finded = false
|
|
|
|
|
} else {
|
|
|
|
|
finded = true
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
finded = true
|
|
|
|
|
}
|
|
|
|
|
} else if route.hasMethod {
|
|
|
|
|
for _, md := range route.methods {
|
|
|
|
|
if md == methodName {
|
|
|
|
|
finded = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !finded {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if len(values) > 0 {
|
|
|
|
|
return route.pattern + "?" + urlv.Encode()
|
|
|
|
|
}
|
|
|
|
|
return route.pattern
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, route := range p.routers {
|
|
|
|
|
if route.controllerType.Name() == controllName {
|
|
|
|
|
var finded bool
|
2013-12-12 14:25:08 +00:00
|
|
|
|
if utils.InSlice(strings.ToLower(methodName), HTTPMETHOD) {
|
2013-11-10 15:05:07 +00:00
|
|
|
|
if route.hasMethod {
|
|
|
|
|
if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName {
|
|
|
|
|
finded = false
|
|
|
|
|
} else if m, ok = route.methods["*"]; ok && m != methodName {
|
|
|
|
|
finded = false
|
|
|
|
|
} else {
|
|
|
|
|
finded = true
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
finded = true
|
|
|
|
|
}
|
|
|
|
|
} else if route.hasMethod {
|
|
|
|
|
for _, md := range route.methods {
|
|
|
|
|
if md == methodName {
|
|
|
|
|
finded = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !finded {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var returnurl string
|
|
|
|
|
var i int
|
|
|
|
|
var startreg bool
|
|
|
|
|
for _, v := range route.regex.String() {
|
|
|
|
|
if v == '(' {
|
|
|
|
|
startreg = true
|
|
|
|
|
continue
|
|
|
|
|
} else if v == ')' {
|
|
|
|
|
startreg = false
|
|
|
|
|
returnurl = returnurl + urlv.Get(route.params[i])
|
|
|
|
|
i++
|
|
|
|
|
} else if !startreg {
|
|
|
|
|
returnurl = string(append([]rune(returnurl), v))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if route.regex.MatchString(returnurl) {
|
|
|
|
|
return returnurl
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if p.enableAuto {
|
|
|
|
|
for cName, methodList := range p.autoRouter {
|
|
|
|
|
if strings.ToLower(strings.TrimSuffix(paths[len(paths)-2], "Controller")) == cName {
|
|
|
|
|
if _, ok := methodList[methodName]; ok {
|
|
|
|
|
if len(values) > 0 {
|
|
|
|
|
return "/" + strings.TrimSuffix(paths[len(paths)-2], "Controller") + "/" + methodName + "?" + urlv.Encode()
|
|
|
|
|
} else {
|
|
|
|
|
return "/" + strings.TrimSuffix(paths[len(paths)-2], "Controller") + "/" + methodName
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 16:43:29 +00:00
|
|
|
|
// main function to serveHTTP
|
2012-12-18 07:18:43 +00:00
|
|
|
|
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
2013-12-17 00:53:15 +00:00
|
|
|
|
if err == USERSTOPRUN {
|
2013-12-16 15:22:15 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2013-11-26 00:46:46 +00:00
|
|
|
|
if _, ok := err.(middleware.HTTPException); ok {
|
|
|
|
|
// catch intented errors, only for HTTP 4XX and 5XX
|
2012-12-18 07:18:43 +00:00
|
|
|
|
} else {
|
2013-12-13 13:25:25 +00:00
|
|
|
|
if RunMode == "dev" {
|
2013-11-26 00:46:46 +00:00
|
|
|
|
if !RecoverPanic {
|
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
2013-12-16 14:54:29 +00:00
|
|
|
|
if ErrorsShow {
|
|
|
|
|
if handler, ok := middleware.ErrorMaps[fmt.Sprint(err)]; ok {
|
|
|
|
|
handler(rw, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-26 00:46:46 +00:00
|
|
|
|
var stack string
|
|
|
|
|
Critical("Handler crashed with error", err)
|
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
Critical(file, line)
|
2013-12-13 13:25:25 +00:00
|
|
|
|
stack = stack + fmt.Sprintln(file, line)
|
2013-05-06 16:17:25 +00:00
|
|
|
|
}
|
2013-12-13 13:25:25 +00:00
|
|
|
|
middleware.ShowErr(err, rw, r, stack)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2013-12-16 14:54:29 +00:00
|
|
|
|
if !RecoverPanic {
|
|
|
|
|
panic(err)
|
2013-12-13 13:25:25 +00:00
|
|
|
|
} else {
|
2013-12-16 14:54:29 +00:00
|
|
|
|
// in production model show all infomation
|
|
|
|
|
if ErrorsShow {
|
|
|
|
|
handler := p.getErrorHandler(fmt.Sprint(err))
|
|
|
|
|
handler(rw, r)
|
2013-12-18 12:53:23 +00:00
|
|
|
|
return
|
2013-12-13 13:25:25 +00:00
|
|
|
|
} else {
|
|
|
|
|
Critical("Handler crashed with error", err)
|
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
Critical(file, line)
|
|
|
|
|
}
|
2013-05-06 16:17:25 +00:00
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
2013-04-11 06:35:43 +00:00
|
|
|
|
}
|
2013-12-13 13:25:25 +00:00
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
2013-06-20 07:49:43 +00:00
|
|
|
|
|
2013-11-13 13:11:03 +00:00
|
|
|
|
starttime := time.Now()
|
|
|
|
|
requestPath := r.URL.Path
|
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
|
2013-11-13 13:11:03 +00:00
|
|
|
|
params := make(map[string]string)
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
w := &responseWriter{writer: rw}
|
2013-10-28 14:38:50 +00:00
|
|
|
|
w.Header().Set("Server", BeegoServerName)
|
2013-12-18 14:33:21 +00:00
|
|
|
|
|
|
|
|
|
// init context
|
|
|
|
|
var context *beecontext.Context
|
|
|
|
|
select {
|
|
|
|
|
case context = <-p.contextBuffer:
|
|
|
|
|
context.ResponseWriter = w
|
|
|
|
|
context.Request = r
|
|
|
|
|
context.Input.Request = r
|
|
|
|
|
default:
|
|
|
|
|
context = &beecontext.Context{
|
|
|
|
|
ResponseWriter: w,
|
|
|
|
|
Request: r,
|
|
|
|
|
Input: beecontext.NewInput(r),
|
|
|
|
|
Output: beecontext.NewOutput(),
|
|
|
|
|
}
|
|
|
|
|
context.Output.Context = context
|
|
|
|
|
context.Output.EnableGzip = EnableGzip
|
2013-09-09 16:00:11 +00:00
|
|
|
|
}
|
2013-09-10 09:56:06 +00:00
|
|
|
|
|
2013-12-18 14:33:21 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
if context != nil {
|
|
|
|
|
select {
|
|
|
|
|
case p.contextBuffer <- context:
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if context.Input.IsWebsocket() {
|
|
|
|
|
context.ResponseWriter = rw
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 14:56:35 +00:00
|
|
|
|
// session init
|
|
|
|
|
if SessionOn {
|
|
|
|
|
context.Input.CruSession = GlobalSessions.SessionStart(w, r)
|
|
|
|
|
defer context.Input.CruSession.SessionRelease()
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 14:25:08 +00:00
|
|
|
|
if !utils.InSlice(strings.ToLower(r.Method), HTTPMETHOD) {
|
2013-11-07 14:10:46 +00:00
|
|
|
|
http.Error(w, "Method Not Allowed", 405)
|
2013-11-13 13:11:03 +00:00
|
|
|
|
goto Admin
|
2013-11-07 14:10:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 15:26:28 +00:00
|
|
|
|
if do_filter(BeforeRouter) {
|
|
|
|
|
goto Admin
|
2013-09-09 16:00:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
//static file server
|
|
|
|
|
for prefix, staticDir := range StaticDir {
|
2013-05-08 08:35:29 +00:00
|
|
|
|
if r.URL.Path == "/favicon.ico" {
|
|
|
|
|
file := staticDir + r.URL.Path
|
|
|
|
|
http.ServeFile(w, r, file)
|
|
|
|
|
w.started = true
|
2013-11-13 13:11:03 +00:00
|
|
|
|
goto Admin
|
2013-05-08 08:35:29 +00:00
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
if strings.HasPrefix(r.URL.Path, prefix) {
|
|
|
|
|
file := staticDir + r.URL.Path[len(prefix):]
|
2013-06-24 15:24:13 +00:00
|
|
|
|
finfo, err := os.Stat(file)
|
|
|
|
|
if err != nil {
|
2013-11-10 15:42:07 +00:00
|
|
|
|
if RunMode == "dev" {
|
|
|
|
|
Warn(err)
|
|
|
|
|
}
|
2013-11-10 15:26:28 +00:00
|
|
|
|
http.NotFound(w, r)
|
2013-11-13 13:11:03 +00:00
|
|
|
|
goto Admin
|
2013-06-24 15:24:13 +00:00
|
|
|
|
}
|
|
|
|
|
//if the request is dir and DirectoryIndex is false then
|
|
|
|
|
if finfo.IsDir() && !DirectoryIndex {
|
2013-09-11 09:00:39 +00:00
|
|
|
|
middleware.Exception("403", rw, r, "403 Forbidden")
|
2013-11-13 13:11:03 +00:00
|
|
|
|
goto Admin
|
2013-06-24 15:24:13 +00:00
|
|
|
|
}
|
2013-12-15 18:17:27 +00:00
|
|
|
|
|
2013-12-14 18:35:57 +00:00
|
|
|
|
//This block obtained from (https://github.com/smithfox/beego) - it should probably get merged into astaxie/beego after a pull request
|
|
|
|
|
isStaticFileToCompress := false
|
|
|
|
|
if StaticExtensionsToGzip != nil && len(StaticExtensionsToGzip) > 0 {
|
|
|
|
|
for _, statExtension := range StaticExtensionsToGzip {
|
|
|
|
|
if strings.HasSuffix(strings.ToLower(file), strings.ToLower(statExtension)) {
|
|
|
|
|
isStaticFileToCompress = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isStaticFileToCompress {
|
|
|
|
|
if EnableGzip {
|
|
|
|
|
w.contentEncoding = GetAcceptEncodingZip(r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memzipfile, err := OpenMemZipFile(file, w.contentEncoding)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.InitHeadContent(finfo.Size())
|
|
|
|
|
|
|
|
|
|
http.ServeContent(w, r, file, finfo.ModTime(), memzipfile)
|
|
|
|
|
} else {
|
|
|
|
|
http.ServeFile(w, r, file)
|
|
|
|
|
}
|
2013-12-15 18:17:27 +00:00
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
w.started = true
|
2013-11-13 13:11:03 +00:00
|
|
|
|
goto Admin
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 15:26:28 +00:00
|
|
|
|
if do_filter(AfterStatic) {
|
|
|
|
|
goto Admin
|
2013-09-09 16:00:11 +00:00
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
|
2013-07-08 15:12:31 +00:00
|
|
|
|
if CopyRequestBody {
|
2013-09-09 16:00:11 +00:00
|
|
|
|
context.Input.Body()
|
2013-07-08 15:12:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
//first find path from the fixrouters to Improve Performance
|
|
|
|
|
for _, route := range p.fixrouters {
|
|
|
|
|
n := len(requestPath)
|
2013-07-30 14:17:16 +00:00
|
|
|
|
if requestPath == route.pattern {
|
2013-12-17 03:19:18 +00:00
|
|
|
|
runrouter = route.controllerType
|
2013-12-20 05:20:09 +00:00
|
|
|
|
findrouter = true
|
2013-12-19 04:50:47 +00:00
|
|
|
|
runMethod = p.getRunMethod(r.Method, context, route)
|
2013-12-20 05:20:09 +00:00
|
|
|
|
break
|
2012-12-19 08:38:25 +00:00
|
|
|
|
}
|
2013-08-04 15:06:48 +00:00
|
|
|
|
// pattern /admin url /admin 200 /admin/ 404
|
|
|
|
|
// pattern /admin/ url /admin 301 /admin/ 200
|
|
|
|
|
if requestPath[n-1] != '/' && len(route.pattern) == n+1 &&
|
2013-09-28 13:45:52 +00:00
|
|
|
|
route.pattern[n] == '/' && route.pattern[:n] == requestPath {
|
2013-08-04 15:06:48 +00:00
|
|
|
|
http.Redirect(w, r, requestPath+"/", 301)
|
2013-11-13 13:11:03 +00:00
|
|
|
|
goto Admin
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-11 15:14:30 +00:00
|
|
|
|
//find regex's router
|
2012-12-18 07:18:43 +00:00
|
|
|
|
if !findrouter {
|
|
|
|
|
//find a matching Route
|
|
|
|
|
for _, route := range p.routers {
|
|
|
|
|
|
|
|
|
|
//check if Route pattern matches url
|
|
|
|
|
if !route.regex.MatchString(requestPath) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//get submatches (params)
|
|
|
|
|
matches := route.regex.FindStringSubmatch(requestPath)
|
|
|
|
|
|
|
|
|
|
//double check that the Route matches the URL pattern.
|
|
|
|
|
if len(matches[0]) != len(requestPath) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(route.params) > 0 {
|
|
|
|
|
//add url parameters to the query param map
|
|
|
|
|
values := r.URL.Query()
|
|
|
|
|
for i, match := range matches[1:] {
|
|
|
|
|
values.Add(route.params[i], match)
|
|
|
|
|
params[route.params[i]] = match
|
|
|
|
|
}
|
|
|
|
|
//reassemble query params and add to RawQuery
|
2013-07-08 15:12:31 +00:00
|
|
|
|
r.URL.RawQuery = url.Values(values).Encode()
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
2013-12-17 03:19:18 +00:00
|
|
|
|
runrouter = route.controllerType
|
2013-12-20 05:20:09 +00:00
|
|
|
|
findrouter = true
|
2013-12-17 03:19:18 +00:00
|
|
|
|
context.Input.Params = params
|
2013-12-19 04:50:47 +00:00
|
|
|
|
runMethod = p.getRunMethod(r.Method, context, route)
|
2013-12-20 05:20:09 +00:00
|
|
|
|
break
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-13 13:11:03 +00:00
|
|
|
|
|
2013-12-17 03:19:18 +00:00
|
|
|
|
if !findrouter && p.enableAuto {
|
|
|
|
|
// deal with url with diffirent ext
|
|
|
|
|
// /controller/simple
|
|
|
|
|
// /controller/simple.html
|
|
|
|
|
// /controller/simple.json
|
|
|
|
|
// /controller/simple.rss
|
|
|
|
|
lastindex := strings.LastIndex(requestPath, "/")
|
|
|
|
|
lastsub := requestPath[lastindex+1:]
|
|
|
|
|
if subindex := strings.LastIndex(lastsub, "."); subindex != -1 {
|
|
|
|
|
context.Input.Params[":ext"] = lastsub[subindex+1:]
|
|
|
|
|
r.URL.Query().Add(":ext", lastsub[subindex+1:])
|
|
|
|
|
r.URL.RawQuery = r.URL.Query().Encode()
|
|
|
|
|
requestPath = requestPath[:len(requestPath)-len(lastsub[subindex:])]
|
|
|
|
|
}
|
|
|
|
|
for cName, methodmap := range p.autoRouter {
|
|
|
|
|
// if prev already find the router break
|
|
|
|
|
if findrouter {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if strings.ToLower(requestPath) == "/"+cName {
|
|
|
|
|
http.Redirect(w, r, requestPath+"/", 301)
|
|
|
|
|
goto Admin
|
|
|
|
|
}
|
|
|
|
|
// if there's no action, set the default action to index
|
|
|
|
|
if strings.ToLower(requestPath) == "/"+cName+"/" {
|
|
|
|
|
requestPath = requestPath + "index"
|
|
|
|
|
}
|
|
|
|
|
// if the request path start with controllerName
|
|
|
|
|
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") {
|
|
|
|
|
for mName, controllerType := range methodmap {
|
|
|
|
|
if strings.ToLower(requestPath) == "/"+cName+"/"+strings.ToLower(mName) ||
|
|
|
|
|
(strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) &&
|
|
|
|
|
requestPath[len("/"+cName+"/"+strings.ToLower(mName)):len("/"+cName+"/"+strings.ToLower(mName))+1] == "/") {
|
|
|
|
|
runrouter = controllerType
|
|
|
|
|
runMethod = mName
|
|
|
|
|
findrouter = true
|
2013-12-18 02:00:52 +00:00
|
|
|
|
//parse params
|
|
|
|
|
otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):]
|
|
|
|
|
if len(otherurl) > 1 {
|
|
|
|
|
plist := strings.Split(otherurl, "/")
|
|
|
|
|
for k, v := range plist[1:] {
|
|
|
|
|
context.Input.Params[strconv.Itoa(k)] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-17 03:19:18 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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 {
|
2013-09-11 07:15:59 +00:00
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
r.ParseMultipartForm(MaxMemory)
|
|
|
|
|
}
|
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
|
|
|
|
}
|
2013-12-12 15:26:28 +00:00
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
//Invoke the request handler
|
2013-12-17 03:19:18 +00:00
|
|
|
|
vc := reflect.New(runrouter)
|
2013-12-18 13:32:25 +00:00
|
|
|
|
execController, ok := vc.Interface().(ControllerInterface)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("controller is not ControllerInterface")
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
|
|
|
|
|
//call the controller init function
|
2013-12-18 13:32:25 +00:00
|
|
|
|
execController.Init(context, runrouter.Name(), runMethod, vc.Interface())
|
2012-12-18 07:18:43 +00:00
|
|
|
|
|
2013-08-06 15:21:52 +00:00
|
|
|
|
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
|
|
|
|
if EnableXSRF {
|
2013-12-18 13:32:25 +00:00
|
|
|
|
execController.XsrfToken()
|
2013-08-06 15:21:52 +00:00
|
|
|
|
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
|
|
|
|
(r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
|
2013-12-18 13:32:25 +00:00
|
|
|
|
execController.CheckXsrfCookie()
|
2013-08-06 15:21:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-22 06:32:18 +00:00
|
|
|
|
//call prepare function
|
2013-12-18 13:32:25 +00:00
|
|
|
|
execController.Prepare()
|
2013-09-22 06:32:18 +00:00
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
if !w.started {
|
2013-12-17 03:19:18 +00:00
|
|
|
|
//exec main logic
|
2013-12-18 15:48:43 +00:00
|
|
|
|
switch runMethod {
|
|
|
|
|
case "Get":
|
|
|
|
|
execController.Get()
|
|
|
|
|
case "Post":
|
|
|
|
|
execController.Post()
|
|
|
|
|
case "Delete":
|
|
|
|
|
execController.Delete()
|
|
|
|
|
case "Put":
|
|
|
|
|
execController.Put()
|
|
|
|
|
case "Head":
|
|
|
|
|
execController.Head()
|
|
|
|
|
case "Patch":
|
|
|
|
|
execController.Patch()
|
|
|
|
|
case "Options":
|
|
|
|
|
execController.Options()
|
|
|
|
|
default:
|
|
|
|
|
in := make([]reflect.Value, 0)
|
|
|
|
|
method := vc.MethodByName(runMethod)
|
|
|
|
|
method.Call(in)
|
|
|
|
|
}
|
2013-08-11 15:14:30 +00:00
|
|
|
|
|
2013-12-17 03:19:18 +00:00
|
|
|
|
//render template
|
2013-09-10 09:56:06 +00:00
|
|
|
|
if !w.started && !context.Input.IsWebsocket() {
|
2012-12-18 07:18:43 +00:00
|
|
|
|
if AutoRender {
|
2013-12-18 13:32:25 +00:00
|
|
|
|
if err := execController.Render(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
|
|
2013-12-17 03:19:18 +00:00
|
|
|
|
// finish all runrouter. release resource
|
2013-12-18 13:32:25 +00:00
|
|
|
|
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-11-13 13:11:03 +00:00
|
|
|
|
Admin:
|
2013-12-12 15:26:28 +00:00
|
|
|
|
do_filter(FinishRouter)
|
|
|
|
|
|
2013-11-13 13:11:03 +00:00
|
|
|
|
//admin module record QPS
|
|
|
|
|
if EnableAdmin {
|
2013-11-15 10:08:53 +00:00
|
|
|
|
timeend := time.Since(starttime)
|
|
|
|
|
if FilterMonitorFunc(r.Method, requestPath, timeend) {
|
|
|
|
|
if runrouter != nil {
|
2013-12-17 03:19:18 +00:00
|
|
|
|
go toolbox.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.Name(), timeend)
|
2013-11-15 10:08:53 +00:00
|
|
|
|
} else {
|
2013-11-20 13:17:49 +00:00
|
|
|
|
go toolbox.StatisticsMap.AddStatistics(r.Method, requestPath, "", 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
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-06 05:47:34 +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
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 04:50:47 +00:00
|
|
|
|
func (p *ControllerRegistor) getRunMethod(method string, context *beecontext.Context, router *controllerInfo) string {
|
2013-12-17 03:19:18 +00:00
|
|
|
|
method = strings.ToLower(method)
|
2013-12-19 05:07:43 +00:00
|
|
|
|
if method == "post" && strings.ToLower(context.Input.Query("_method")) == "put" {
|
|
|
|
|
method = "put"
|
|
|
|
|
}
|
|
|
|
|
if method == "post" && strings.ToLower(context.Input.Query("_method")) == "delete" {
|
|
|
|
|
method = "delete"
|
|
|
|
|
}
|
2013-12-17 03:19:18 +00:00
|
|
|
|
if router.hasMethod {
|
|
|
|
|
if m, ok := router.methods[method]; ok {
|
|
|
|
|
return m
|
|
|
|
|
} else if m, ok = router.methods["*"]; ok {
|
|
|
|
|
return m
|
2013-12-20 05:20:09 +00:00
|
|
|
|
} else {
|
|
|
|
|
return strings.Title(method)
|
2013-12-17 03:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return strings.Title(method)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2013-12-15 18:17:27 +00:00
|
|
|
|
writer http.ResponseWriter
|
|
|
|
|
started bool
|
|
|
|
|
status int
|
2013-12-14 18:35:57 +00:00
|
|
|
|
contentEncoding string
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-14 18:35:57 +00:00
|
|
|
|
func (w *responseWriter) InitHeadContent(contentlength int64) {
|
|
|
|
|
if w.contentEncoding == "gzip" {
|
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
|
} else if w.contentEncoding == "deflate" {
|
|
|
|
|
w.Header().Set("Content-Encoding", "deflate")
|
|
|
|
|
} else {
|
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(contentlength, 10))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
// Write writes the data to the connection as part of an HTTP reply,
|
|
|
|
|
// and sets `started` to true
|
|
|
|
|
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,
|
|
|
|
|
// and sets `started` to true
|
|
|
|
|
func (w *responseWriter) WriteHeader(code int) {
|
|
|
|
|
w.status = code
|
|
|
|
|
w.started = true
|
|
|
|
|
w.writer.WriteHeader(code)
|
|
|
|
|
}
|