路由匹配

    • 静态路由 匹配 URL:/ 到 Action结构体的Get函数
      匹配 URL:/static 到 Action结构体的Get函数
      1. tg.Get("/(:name)", new(Action))
      2. tg.Get("/:name1/:name2", new(Action))
      3. tg.Get("/name2", new(Action))
      4. tg.Get("/(:name1)sss(:name2)", new(Action))
    • 通配路由

      1. tg.Get("/*name", new(Action))
      2. tg.Get("/ttt/*name", new(Action))
      3. tg.Get("/sss(*name)", new(Action))

      路由参数支持*通配符

    • 正则路由
    1. tg.Get("/(:name.*)", new(Action))
    2. tg.Get("/(:name[0-9]+)", new(Action))
    3. tg.Get("/(:name1)-(:name2[0-9]+)", new(Action))

    路由参数支持正则约束,不符合条件的参数会抛出404错误

    路由定义

    除了可以用Get Post等方法来定义外,也可以使用Route来绑定路由,形式更灵活

    • 自定义方法名
      ```Go
      type Action struct {}
      func (Action) MyPost() {}

    t.Route(“POST:MyPost”, “/“, new(Action))

    1. * 一次定义多个
    2. ```Go
    3. t.Route([]string{"GET:MyGet", "POST"}, "/", new(Action))
    • 自定义多个
      1. t.Route(map[string]string{"GET":"MyGet", "POST":"MyPost"}, "/", new(Action))
    • 静态路由和其它形式的路由都匹配时,静态路由优先,跟添加的顺序无关,如:

      1. /abc
      2. /(:name)

      那么对于请求/abc,则/abc会匹配

    • 那么对于请求/abc/abc123abc123abc,则/(:name1)/abc(:name1)abc(:name2)abc会匹配

    • 其它路由之间根据添加的顺序,先添加的优先。

    路由参数

    请求路径匹配后的参数可以通过*Context获得:

    • 命名路由和正则路由: ctx.Param(":name") 或者 ctx.Param("name")
    • 通配路由:ctx.Param("*name")
    1. import "github.com/tango-contrib/binding"
    2. type Getuser struct {
    3. tango.JSON
    4. tango.Ctx
    5. binding.Binder
    6. }
    7. func (this *Getuser) Get() interface{} {
    8. type GetStruct struct {
    9. }
    10. var data GetStruct
    11. err := this.Bind(&data)
    12. if err != nil {
    13. return map[string]interface{}{
    14. "res": "no",
    15. "msg": "get param error",
    16. "err:": err,
    17. "status": 500,
    18. }
    19. }
    20. fmt.Println(data.Name)//取值
    21. .....
    22. }
    23. //路由
    24. func main() {
    25. t := tango.Classic()
    26. t.Use(binding.Bind())
    27. t.Get("/api/1.0/users", new(Getuser))
    28. ......

    POST参数获取实例:JSON格式

    1. type Settid struct {
    2. tango.JSON
    3. tango.Ctx
    4. }
    5. func (this *Settid) Post() interface{} {
    6. type jsondata struct {
    7. Uid string `json:"Uid"`
    8. Tid string `json:"Tid"`
    9. }
    10. var data jsondata
    11. err = this.DecodeJSON(&data)
    12. return map[string]interface{}{
    13. "res": "no",
    14. "msg": "DecodeJSON error",
    15. "err:": err,
    16. "status": 500,
    17. }
    18. }
    19. fmt.Println(data.Uid)//取值
    20. .....
    21. }
    22. //路由
    23. func main() {
    24. t := tango.Classic()
    25. t.Post("/inapi/1.0/setaiport", new(Settid))
    26. ...
    • 路由规则: tg.Route(“GET”, “/node/:name”, new(Node))
    • curl

    代码

    1. package main
    2. import (
    3. "github.com/lunny/tango"
    4. )
    5. type Node struct {
    6. tango.Ctx
    7. }
    8. func (this *Node) Get() interface{} {
    9. return this.Param("name")
    10. }
    11. func main() {
    12. tg := tango.Classic()
    13. tg.Route("GET", "/node/:name", new(Node))
    14. tg.Run()