Page:
Router
Pages
Actions
Basicauth
Binding
Captcha
Compress
Context
Debug
Dispatch
ErrHandler
Events
Flash
Forms
Group
Handler
Home
Injection
Logger
Params
QuickStart
Recovery
Renders
Return
Router
Session
Static
Tango
Tpongo2
Xsrf
ZH_Actions
ZH_Basicauth
ZH_Binding
ZH_Captcha
ZH_Compress
ZH_Context
ZH_Debug
ZH_Dispatch
ZH_ErrHandler
ZH_Events
ZH_Flash
ZH_Forms
ZH_Group
ZH_HOME
ZH_Handler
ZH_Injection
ZH_Logger
ZH_Params
ZH_Recovery
ZH_Renders
ZH_Return
ZH_Router
ZH_Session
ZH_Static
ZH_Tango
ZH_Tpongo2
ZH_Xsrf
12
Router
Lunny Xiao edited this page 2015-04-17 20:19:59 +08:00
Routers
Path
Tango supports 4 forms path:
- static path
tg.Get("/", new(Action))
tg.Get("/static", new(Action))
- named path
tg.Get("/:name", new(Action))
tg.Get("/(:name)", new(Action))
tg.Get("/:name1/:name2", new(Action))
tg.Get("/:name1-:name2", new(Action))
tg.Get("/(:name1)sss(:name2)", new(Action))
- catch-all path
tg.Get("/*name", new(Action))
tg.Get("/ttt/*name", new(Action))
tg.Get("/sss(*name)", new(Action))
- regexp path
tg.Get("/(:name.*)", new(Action))
tg.Get("/(:name[0-9]+)", new(Action))
tg.Get("/(:name1)-(:name2[0-9]+)", new(Action))
Router Priority
- Static router is always first matched before other routers even it was added last.
- Other routers will order by it's added sequence.
For examples:
t := tango.Classic()
t.Get("/:name", new(Others))
t.Get("/admin", new(Admin))
t.Run()
When you request /admin
, the Admin
's Get
method will be invoked.
t := tango.Classic()
t.Get("/:name", new(Admin))
t.Get("/*name", new(Others))
t.Run()
When you request /admin
, the Admin
's Get
method will be invoked. When you request /admin/ui
, Others
's Get
method will be invoked.
t := tango.Classic()
t.Get("/*name", new(Admin))
t.Get("/:name", new(Others))
t.Run()
Others
's Get
will never be invoked, because all matched requests will be matched to Admin
's Get
.
Params
The matched params can get By *Context
For named , catch-all or regexp Path: ctx.Params().Get(":name")
Funcs & Actions
Tango supports 5 forms functions or struct methods:
- func()
- func(http.ResponseWriter, *http.Request)
- func(*tango.Context)
- func(http.Response.Writer)
- func(*http.Request)
- struct.Get()
func()
t := tango.Classic()
t.Get("/", func() string {
return "hello tango"
})
func(http.ResponseWriter, *http.Request)
t := tango.Classic()
t.Post("/", func(w http.ResponseWriter, *http.Request) {
w.Write([]byte("hello tango"))
})
func(http.ResponseWriter)
t := tango.Classic()
t.Post("/", func(w http.ResponseWriter) {
w.Write([]byte("hello tango"))
})
func(*http.Request)
t := tango.Classic()
t.Post("/", func(req *http.Request) string {
return req.FormValue("key")
})
func(*tango.Context)
t := tango.Classic()
t.Get("/:name", func(ctx *tango.Context) {
ctx.Write([]byte("hello " + ctx.Params().Get(":name")))
})
structs
type Action struct {}
func (a *Action) Get() string {
return "haha"
}
t := tango.Classic()
t.Get("/:name", new(Action))
More routes support
GET route
t.Route("GET", "/", new(Action))
custom method route
type Action struct {}
func (Action) MyPost() {}
t.Route("POST:MyPost", "/", new(Action))
define more route once
t.Route([]string{"GET:MyGet", "POST"}, "/", new(Action))
define more route once via map
t.Route(map[string]string{"GET":"MyGet", "POST":"MyPost"}, "/", new(Action))