2012-12-18 07:18:43 +00:00
|
|
|
|
package beego
|
|
|
|
|
|
|
|
|
|
import (
|
2013-04-11 06:35:43 +00:00
|
|
|
|
"fmt"
|
2013-09-09 16:00:11 +00:00
|
|
|
|
beecontext "github.com/astaxie/beego/context"
|
2013-09-11 09:00:39 +00:00
|
|
|
|
"github.com/astaxie/beego/middleware"
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
|
|
|
|
|
|
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-04-07 16:28:32 +00:00
|
|
|
|
routers []*controllerInfo
|
|
|
|
|
fixrouters []*controllerInfo
|
2013-08-11 15:14:30 +00:00
|
|
|
|
enableFilter bool
|
2013-09-09 16:00:11 +00:00
|
|
|
|
filters map[string][]*FilterRouter
|
2013-08-11 15:14:30 +00:00
|
|
|
|
enableAuto bool
|
2013-07-27 02:25:14 +00:00
|
|
|
|
autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewControllerRegistor() *ControllerRegistor {
|
2013-07-27 02:25:14 +00:00
|
|
|
|
return &ControllerRegistor{
|
2013-09-09 16:00:11 +00:00
|
|
|
|
routers: make([]*controllerInfo, 0),
|
|
|
|
|
autoRouter: make(map[string]map[string]reflect.Type),
|
|
|
|
|
filters: make(map[string][]*FilterRouter),
|
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++
|
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
|
|
|
|
panic("method mapping fomate is error")
|
|
|
|
|
}
|
|
|
|
|
comma := strings.Split(colon[0], ",")
|
|
|
|
|
for _, m := range comma {
|
|
|
|
|
if m == "*" || 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 {
|
|
|
|
|
panic(colon[1] + " method don't exist in the controller " + t.Name())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic(v + " is an error method mapping,Don't exist method named " + m)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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-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-09-09 16:00:11 +00:00
|
|
|
|
// Filter adds the middleware filter.
|
|
|
|
|
func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) {
|
|
|
|
|
p.enableFilter = true
|
|
|
|
|
mr := new(FilterRouter)
|
|
|
|
|
mr.filterFunc = filter
|
2013-04-07 16:28:32 +00:00
|
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
|
parts := strings.Split(pattern, "/")
|
2013-04-07 16:28:32 +00:00
|
|
|
|
j := 0
|
|
|
|
|
for i, part := range parts {
|
|
|
|
|
if strings.HasPrefix(part, ":") {
|
2013-09-09 16:00:11 +00:00
|
|
|
|
expr := "(.+)"
|
2013-04-07 16:28:32 +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]+)’
|
2013-04-07 16:28:32 +00:00
|
|
|
|
if index := strings.Index(part, "("); index != -1 {
|
|
|
|
|
expr = part[index:]
|
|
|
|
|
part = part[:index]
|
2013-09-09 16:00:11 +00:00
|
|
|
|
//match /user/:id:int ([0-9]+)
|
|
|
|
|
//match /post/:username:string ([\w]+)
|
|
|
|
|
} else if lindex := strings.LastIndex(part, ":"); lindex != 0 {
|
|
|
|
|
switch part[lindex:] {
|
|
|
|
|
case ":int":
|
|
|
|
|
expr = "([0-9]+)"
|
|
|
|
|
part = part[:lindex]
|
|
|
|
|
case ":string":
|
|
|
|
|
expr = `([\w]+)`
|
|
|
|
|
part = part[:lindex]
|
|
|
|
|
}
|
2013-04-07 16:28:32 +00:00
|
|
|
|
}
|
|
|
|
|
parts[i] = expr
|
|
|
|
|
j++
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
|
if j != 0 {
|
2013-04-07 16:28:32 +00:00
|
|
|
|
pattern = strings.Join(parts, "/")
|
|
|
|
|
regex, regexErr := regexp.Compile(pattern)
|
|
|
|
|
if regexErr != nil {
|
|
|
|
|
//TODO add error handling here to avoid panic
|
|
|
|
|
panic(regexErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
|
mr.regex = regex
|
|
|
|
|
mr.hasregex = true
|
2013-04-07 16:28:32 +00:00
|
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
|
mr.pattern = pattern
|
|
|
|
|
p.filters[action] = append(p.filters[action], mr)
|
2013-08-11 16:14:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
// AutoRoute
|
|
|
|
|
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
2013-05-06 16:17:25 +00:00
|
|
|
|
errstr := fmt.Sprint(err)
|
2013-09-11 09:00:39 +00:00
|
|
|
|
if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow {
|
2013-05-06 16:17:25 +00:00
|
|
|
|
handler(rw, r)
|
2012-12-18 07:18:43 +00:00
|
|
|
|
} else {
|
2013-05-06 16:17:25 +00:00
|
|
|
|
if !RecoverPanic {
|
|
|
|
|
// go back to panic
|
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
|
|
|
|
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)
|
|
|
|
|
if RunMode == "dev" {
|
|
|
|
|
stack = stack + fmt.Sprintln(file, line)
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
2013-04-11 06:35:43 +00:00
|
|
|
|
if RunMode == "dev" {
|
2013-09-11 09:00:39 +00:00
|
|
|
|
middleware.ShowErr(err, rw, r, stack)
|
2013-04-11 06:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
2013-06-20 07:49:43 +00:00
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
w := &responseWriter{writer: rw}
|
2013-06-20 07:49:43 +00:00
|
|
|
|
w.Header().Set("Server", "beegoServer")
|
2013-09-09 16:00:11 +00:00
|
|
|
|
context := &beecontext.Context{
|
|
|
|
|
ResponseWriter: w,
|
|
|
|
|
Request: r,
|
|
|
|
|
Input: beecontext.NewInput(r),
|
|
|
|
|
Output: beecontext.NewOutput(w),
|
|
|
|
|
}
|
|
|
|
|
context.Output.Context = context
|
2013-09-10 09:56:06 +00:00
|
|
|
|
|
|
|
|
|
if context.Input.IsWebsocket() {
|
|
|
|
|
context.ResponseWriter = rw
|
|
|
|
|
context.Output = beecontext.NewOutput(rw)
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
var runrouter *controllerInfo
|
|
|
|
|
var findrouter bool
|
|
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
|
context.Input.Param = params
|
|
|
|
|
if p.enableFilter {
|
|
|
|
|
if l, ok := p.filters["BeforRouter"]; ok {
|
|
|
|
|
for _, filterR := range l {
|
|
|
|
|
if filterR.ValidRouter(r.URL.Path) {
|
|
|
|
|
filterR.filterFunc(context)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
return
|
|
|
|
|
}
|
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 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//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")
|
|
|
|
|
return
|
2013-06-24 15:24:13 +00:00
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
http.ServeFile(w, r, file)
|
|
|
|
|
w.started = true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
|
if p.enableFilter {
|
|
|
|
|
if l, ok := p.filters["AfterStatic"]; ok {
|
|
|
|
|
for _, filterR := range l {
|
|
|
|
|
if filterR.ValidRouter(r.URL.Path) {
|
|
|
|
|
filterR.filterFunc(context)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
requestPath := r.URL.Path
|
|
|
|
|
|
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 {
|
|
|
|
|
runrouter = route
|
|
|
|
|
findrouter = true
|
|
|
|
|
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 &&
|
|
|
|
|
route.pattern[n] == '/' && route.pattern[:n-1] == requestPath {
|
|
|
|
|
http.Redirect(w, r, requestPath+"/", 301)
|
|
|
|
|
return
|
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
|
|
|
|
}
|
|
|
|
|
runrouter = route
|
|
|
|
|
findrouter = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if runrouter != nil {
|
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-08-11 16:14:42 +00:00
|
|
|
|
if p.enableFilter {
|
2013-09-09 16:00:11 +00:00
|
|
|
|
if l, ok := p.filters["BeforExec"]; ok {
|
|
|
|
|
for _, filterR := range l {
|
|
|
|
|
if filterR.ValidRouter(r.URL.Path) {
|
|
|
|
|
filterR.filterFunc(context)
|
|
|
|
|
}
|
2013-08-11 16:14:42 +00:00
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Invoke the request handler
|
|
|
|
|
vc := reflect.New(runrouter.controllerType)
|
|
|
|
|
|
|
|
|
|
//call the controller init function
|
|
|
|
|
init := vc.MethodByName("Init")
|
|
|
|
|
in := make([]reflect.Value, 2)
|
2013-09-09 16:00:11 +00:00
|
|
|
|
in[0] = reflect.ValueOf(context)
|
2012-12-18 07:18:43 +00:00
|
|
|
|
in[1] = reflect.ValueOf(runrouter.controllerType.Name())
|
|
|
|
|
init.Call(in)
|
|
|
|
|
//call prepare function
|
|
|
|
|
in = make([]reflect.Value, 0)
|
|
|
|
|
method := vc.MethodByName("Prepare")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
|
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 {
|
|
|
|
|
method = vc.MethodByName("XsrfToken")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
|
|
|
|
(r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
|
|
|
|
|
method = vc.MethodByName("CheckXsrfCookie")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
//if response has written,yes don't run next
|
|
|
|
|
if !w.started {
|
|
|
|
|
if r.Method == "GET" {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if runrouter.hasMethod {
|
|
|
|
|
if m, ok := runrouter.methods["get"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else if m, ok = runrouter.methods["*"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("Get")
|
|
|
|
|
}
|
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 {
|
|
|
|
|
method = vc.MethodByName("Get")
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
method.Call(in)
|
|
|
|
|
} else if r.Method == "HEAD" {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if runrouter.hasMethod {
|
|
|
|
|
if m, ok := runrouter.methods["head"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else if m, ok = runrouter.methods["*"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("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
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("Head")
|
|
|
|
|
}
|
2013-08-11 15:14:30 +00:00
|
|
|
|
|
2012-12-18 07:18:43 +00:00
|
|
|
|
method.Call(in)
|
2013-04-27 14:47:32 +00:00
|
|
|
|
} else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if runrouter.hasMethod {
|
|
|
|
|
if m, ok := runrouter.methods["delete"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else if m, ok = runrouter.methods["*"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("Delete")
|
|
|
|
|
}
|
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 {
|
|
|
|
|
method = vc.MethodByName("Delete")
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
method.Call(in)
|
2013-04-27 14:47:32 +00:00
|
|
|
|
} else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if runrouter.hasMethod {
|
|
|
|
|
if m, ok := runrouter.methods["put"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else if m, ok = runrouter.methods["*"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("Put")
|
|
|
|
|
}
|
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 {
|
|
|
|
|
method = vc.MethodByName("Put")
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
method.Call(in)
|
2013-04-27 14:47:32 +00:00
|
|
|
|
} else if r.Method == "POST" {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if runrouter.hasMethod {
|
|
|
|
|
if m, ok := runrouter.methods["post"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else if m, ok = runrouter.methods["*"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("Post")
|
|
|
|
|
}
|
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 {
|
|
|
|
|
method = vc.MethodByName("Post")
|
|
|
|
|
}
|
2013-04-27 14:47:32 +00:00
|
|
|
|
method.Call(in)
|
2012-12-18 07:18:43 +00:00
|
|
|
|
} else if r.Method == "PATCH" {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if runrouter.hasMethod {
|
|
|
|
|
if m, ok := runrouter.methods["patch"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else if m, ok = runrouter.methods["*"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("Patch")
|
|
|
|
|
}
|
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 {
|
|
|
|
|
method = vc.MethodByName("Patch")
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
method.Call(in)
|
|
|
|
|
} else if r.Method == "OPTIONS" {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if runrouter.hasMethod {
|
|
|
|
|
if m, ok := runrouter.methods["options"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else if m, ok = runrouter.methods["*"]; ok {
|
|
|
|
|
method = vc.MethodByName(m)
|
|
|
|
|
} else {
|
|
|
|
|
method = vc.MethodByName("Options")
|
|
|
|
|
}
|
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 {
|
|
|
|
|
method = vc.MethodByName("Options")
|
|
|
|
|
}
|
2012-12-18 07:18:43 +00:00
|
|
|
|
method.Call(in)
|
|
|
|
|
}
|
2013-07-08 09:35:09 +00:00
|
|
|
|
gotofunc := vc.Elem().FieldByName("gotofunc").String()
|
|
|
|
|
if gotofunc != "" {
|
|
|
|
|
method = vc.MethodByName(gotofunc)
|
|
|
|
|
if method.IsValid() {
|
|
|
|
|
method.Call(in)
|
|
|
|
|
} else {
|
|
|
|
|
panic("gotofunc is exists:" + gotofunc)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-10 09:56:06 +00:00
|
|
|
|
if !w.started && !context.Input.IsWebsocket() {
|
2012-12-18 07:18:43 +00:00
|
|
|
|
if AutoRender {
|
|
|
|
|
method = vc.MethodByName("Render")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
|
|
2013-08-10 13:44:19 +00:00
|
|
|
|
method = vc.MethodByName("Finish")
|
|
|
|
|
method.Call(in)
|
2013-08-11 16:14:42 +00:00
|
|
|
|
//execute middleware filters
|
2013-09-09 16:00:11 +00:00
|
|
|
|
if p.enableFilter {
|
|
|
|
|
if l, ok := p.filters["AfterExec"]; ok {
|
|
|
|
|
for _, filterR := range l {
|
|
|
|
|
if filterR.ValidRouter(r.URL.Path) {
|
|
|
|
|
filterR.filterFunc(context)
|
|
|
|
|
}
|
2013-08-11 16:14:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-08 05:56:48 +00:00
|
|
|
|
method = vc.MethodByName("Destructor")
|
|
|
|
|
method.Call(in)
|
2012-12-18 07:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 02:25:14 +00:00
|
|
|
|
//start autorouter
|
|
|
|
|
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if p.enableAuto {
|
|
|
|
|
if !findrouter {
|
|
|
|
|
for cName, methodmap := range p.autoRouter {
|
2013-08-04 15:13:29 +00:00
|
|
|
|
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if strings.ToLower(requestPath) == "/"+cName {
|
|
|
|
|
http.Redirect(w, r, requestPath+"/", 301)
|
|
|
|
|
return
|
|
|
|
|
}
|
2013-08-04 15:13:29 +00:00
|
|
|
|
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if strings.ToLower(requestPath) == "/"+cName+"/" {
|
|
|
|
|
requestPath = requestPath + "index"
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") {
|
|
|
|
|
for mName, controllerType := range methodmap {
|
|
|
|
|
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) {
|
2013-09-11 07:15:59 +00:00
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
r.ParseMultipartForm(MaxMemory)
|
|
|
|
|
}
|
2013-08-11 16:14:42 +00:00
|
|
|
|
//execute middleware filters
|
|
|
|
|
if p.enableFilter {
|
2013-09-09 16:00:11 +00:00
|
|
|
|
if l, ok := p.filters["BeforExec"]; ok {
|
|
|
|
|
for _, filterR := range l {
|
|
|
|
|
if filterR.ValidRouter(r.URL.Path) {
|
|
|
|
|
filterR.filterFunc(context)
|
|
|
|
|
}
|
2013-08-11 16:14:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-11 15:14:30 +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:] {
|
|
|
|
|
params[strconv.Itoa(k)] = v
|
|
|
|
|
}
|
2013-07-27 02:25:14 +00:00
|
|
|
|
}
|
2013-08-11 15:14:30 +00:00
|
|
|
|
//Invoke the request handler
|
|
|
|
|
vc := reflect.New(controllerType)
|
|
|
|
|
|
|
|
|
|
//call the controller init function
|
|
|
|
|
init := vc.MethodByName("Init")
|
|
|
|
|
in := make([]reflect.Value, 2)
|
2013-09-09 16:00:11 +00:00
|
|
|
|
in[0] = reflect.ValueOf(context)
|
2013-08-11 15:14:30 +00:00
|
|
|
|
in[1] = reflect.ValueOf(controllerType.Name())
|
|
|
|
|
init.Call(in)
|
|
|
|
|
//call prepare function
|
|
|
|
|
in = make([]reflect.Value, 0)
|
|
|
|
|
method := vc.MethodByName("Prepare")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
method = vc.MethodByName(mName)
|
2013-08-06 15:21:52 +00:00
|
|
|
|
method.Call(in)
|
2013-08-11 15:14:30 +00:00
|
|
|
|
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
|
|
|
|
if EnableXSRF {
|
|
|
|
|
method = vc.MethodByName("XsrfToken")
|
2013-08-06 15:21:52 +00:00
|
|
|
|
method.Call(in)
|
2013-08-11 15:14:30 +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")) {
|
|
|
|
|
method = vc.MethodByName("CheckXsrfCookie")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
}
|
2013-08-06 15:21:52 +00:00
|
|
|
|
}
|
2013-09-10 09:56:06 +00:00
|
|
|
|
if !w.started && !context.Input.IsWebsocket() {
|
2013-08-11 15:14:30 +00:00
|
|
|
|
if AutoRender {
|
|
|
|
|
method = vc.MethodByName("Render")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
}
|
2013-07-27 02:25:14 +00:00
|
|
|
|
}
|
2013-08-11 15:14:30 +00:00
|
|
|
|
method = vc.MethodByName("Finish")
|
|
|
|
|
method.Call(in)
|
2013-08-11 16:14:42 +00:00
|
|
|
|
//execute middleware filters
|
2013-09-09 16:00:11 +00:00
|
|
|
|
if p.enableFilter {
|
|
|
|
|
if l, ok := p.filters["AfterExec"]; ok {
|
|
|
|
|
for _, filterR := range l {
|
|
|
|
|
if filterR.ValidRouter(r.URL.Path) {
|
|
|
|
|
filterR.filterFunc(context)
|
|
|
|
|
}
|
2013-08-11 16:14:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-11 15:14:30 +00:00
|
|
|
|
method = vc.MethodByName("Destructor")
|
|
|
|
|
method.Call(in)
|
|
|
|
|
// set find
|
|
|
|
|
findrouter = true
|
2013-09-09 16:00:11 +00:00
|
|
|
|
goto Last
|
2013-07-27 02:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-11 15:14:30 +00:00
|
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
|
Last:
|
2012-12-18 07:18:43 +00:00
|
|
|
|
//if no matches to url, throw a not found exception
|
2013-05-13 08:31:17 +00:00
|
|
|
|
if !findrouter {
|
2013-09-11 09:00:39 +00:00
|
|
|
|
middleware.Exception("404", rw, r, "")
|
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 {
|
|
|
|
|
writer http.ResponseWriter
|
|
|
|
|
started bool
|
|
|
|
|
status int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
// 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)
|
|
|
|
|
}
|