add GetFIle & enhance router
Showing
2 changed files
with
32 additions
and
3 deletions
| ... | @@ -7,6 +7,7 @@ import ( | ... | @@ -7,6 +7,7 @@ import ( |
| 7 | "github.com/astaxie/beego/session" | 7 | "github.com/astaxie/beego/session" |
| 8 | "html/template" | 8 | "html/template" |
| 9 | "io/ioutil" | 9 | "io/ioutil" |
| 10 | "mime/multipart" | ||
| 10 | "net/http" | 11 | "net/http" |
| 11 | "net/url" | 12 | "net/url" |
| 12 | "path" | 13 | "path" |
| ... | @@ -191,6 +192,10 @@ func (c *Controller) GetBool(key string) (bool, error) { | ... | @@ -191,6 +192,10 @@ func (c *Controller) GetBool(key string) (bool, error) { |
| 191 | return strconv.ParseBool(c.Input().Get(key)) | 192 | return strconv.ParseBool(c.Input().Get(key)) |
| 192 | } | 193 | } |
| 193 | 194 | ||
| 195 | func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, error) { | ||
| 196 | return c.Ctx.Request.FormFile(key) | ||
| 197 | } | ||
| 198 | |||
| 194 | func (c *Controller) StartSession() (sess session.SessionStore) { | 199 | func (c *Controller) StartSession() (sess session.SessionStore) { |
| 195 | sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request) | 200 | sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request) |
| 196 | return | 201 | return | ... | ... |
| ... | @@ -41,17 +41,43 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) { | ... | @@ -41,17 +41,43 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) { |
| 41 | params := make(map[int]string) | 41 | params := make(map[int]string) |
| 42 | for i, part := range parts { | 42 | for i, part := range parts { |
| 43 | if strings.HasPrefix(part, ":") { | 43 | if strings.HasPrefix(part, ":") { |
| 44 | expr := "([^/]+)" | 44 | expr := "(.+)" |
| 45 | //a user may choose to override the defult expression | 45 | //a user may choose to override the defult expression |
| 46 | // similar to expressjs: ‘/user/:id([0-9]+)’ | 46 | // similar to expressjs: ‘/user/:id([0-9]+)’ |
| 47 | if index := strings.Index(part, "("); index != -1 { | 47 | if index := strings.Index(part, "("); index != -1 { |
| 48 | expr = part[index:] | 48 | expr = part[index:] |
| 49 | part = part[:index] | 49 | part = part[:index] |
| 50 | //match /user/:id:int ([0-9]+) | ||
| 51 | //match /post/:username:word ([\w]+) | ||
| 52 | } else if lindex := strings.LastIndex(part, ":"); lindex != 0 { | ||
| 53 | switch part[lindex:] { | ||
| 54 | case "int": | ||
| 55 | expr = "([0-9]+)" | ||
| 56 | part = part[:index] | ||
| 57 | case "word": | ||
| 58 | expr = `([\w]+)` | ||
| 59 | part = part[:index] | ||
| 60 | } | ||
| 50 | } | 61 | } |
| 51 | params[j] = part | 62 | params[j] = part |
| 52 | parts[i] = expr | 63 | parts[i] = expr |
| 53 | j++ | 64 | j++ |
| 54 | } | 65 | } |
| 66 | if strings.HasPrefix(part, "*") { | ||
| 67 | expr := "(.+)" | ||
| 68 | if part == "*.*" { | ||
| 69 | params[j] = ":path" | ||
| 70 | parts[j] = "([^.]+)." | ||
| 71 | j++ | ||
| 72 | params[j] = ":ext" | ||
| 73 | parts[j] = "([^.]+)" | ||
| 74 | j++ | ||
| 75 | } else { | ||
| 76 | params[j] = ":splat" | ||
| 77 | parts[i] = expr | ||
| 78 | j++ | ||
| 79 | } | ||
| 80 | } | ||
| 55 | } | 81 | } |
| 56 | if j == 0 { | 82 | if j == 0 { |
| 57 | //now create the Route | 83 | //now create the Route |
| ... | @@ -79,10 +105,8 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) { | ... | @@ -79,10 +105,8 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) { |
| 79 | route.params = params | 105 | route.params = params |
| 80 | route.pattern = pattern | 106 | route.pattern = pattern |
| 81 | route.controllerType = t | 107 | route.controllerType = t |
| 82 | |||
| 83 | p.routers = append(p.routers, route) | 108 | p.routers = append(p.routers, route) |
| 84 | } | 109 | } |
| 85 | |||
| 86 | } | 110 | } |
| 87 | 111 | ||
| 88 | func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) { | 112 | func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) { | ... | ... |
-
Please register or sign in to post a comment