mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 17:21:02 +00:00
add GetFIle & enhance router
This commit is contained in:
parent
f9e8b4f124
commit
2f02653b7d
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/astaxie/beego/session"
|
"github.com/astaxie/beego/session"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
@ -191,6 +192,10 @@ func (c *Controller) GetBool(key string) (bool, error) {
|
|||||||
return strconv.ParseBool(c.Input().Get(key))
|
return strconv.ParseBool(c.Input().Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, error) {
|
||||||
|
return c.Ctx.Request.FormFile(key)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Controller) StartSession() (sess session.SessionStore) {
|
func (c *Controller) StartSession() (sess session.SessionStore) {
|
||||||
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
|
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
|
||||||
return
|
return
|
||||||
|
30
router.go
30
router.go
@ -41,17 +41,43 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
|
|||||||
params := make(map[int]string)
|
params := make(map[int]string)
|
||||||
for i, part := range parts {
|
for i, part := range parts {
|
||||||
if strings.HasPrefix(part, ":") {
|
if strings.HasPrefix(part, ":") {
|
||||||
expr := "([^/]+)"
|
expr := "(.+)"
|
||||||
//a user may choose to override the defult expression
|
//a user may choose to override the defult expression
|
||||||
// similar to expressjs: ‘/user/:id([0-9]+)’
|
// similar to expressjs: ‘/user/:id([0-9]+)’
|
||||||
if index := strings.Index(part, "("); index != -1 {
|
if index := strings.Index(part, "("); index != -1 {
|
||||||
expr = part[index:]
|
expr = part[index:]
|
||||||
part = part[:index]
|
part = part[:index]
|
||||||
|
//match /user/:id:int ([0-9]+)
|
||||||
|
//match /post/:username:word ([\w]+)
|
||||||
|
} else if lindex := strings.LastIndex(part, ":"); lindex != 0 {
|
||||||
|
switch part[lindex:] {
|
||||||
|
case "int":
|
||||||
|
expr = "([0-9]+)"
|
||||||
|
part = part[:index]
|
||||||
|
case "word":
|
||||||
|
expr = `([\w]+)`
|
||||||
|
part = part[:index]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
params[j] = part
|
params[j] = part
|
||||||
parts[i] = expr
|
parts[i] = expr
|
||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(part, "*") {
|
||||||
|
expr := "(.+)"
|
||||||
|
if part == "*.*" {
|
||||||
|
params[j] = ":path"
|
||||||
|
parts[j] = "([^.]+)."
|
||||||
|
j++
|
||||||
|
params[j] = ":ext"
|
||||||
|
parts[j] = "([^.]+)"
|
||||||
|
j++
|
||||||
|
} else {
|
||||||
|
params[j] = ":splat"
|
||||||
|
parts[i] = expr
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if j == 0 {
|
if j == 0 {
|
||||||
//now create the Route
|
//now create the Route
|
||||||
@ -79,10 +105,8 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
|
|||||||
route.params = params
|
route.params = params
|
||||||
route.pattern = pattern
|
route.pattern = pattern
|
||||||
route.controllerType = t
|
route.controllerType = t
|
||||||
|
|
||||||
p.routers = append(p.routers, route)
|
p.routers = append(p.routers, route)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
|
func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
|
||||||
|
Loading…
Reference in New Issue
Block a user