完善ghttp.Server路由控制示例程序

This commit is contained in:
John
2018-07-24 23:43:00 +08:00
parent 387a9ff2f7
commit df255b12b6
4 changed files with 30 additions and 5 deletions

View File

@ -75,6 +75,7 @@ func (r *Response) Write(content ... interface{}) {
// 返回信息,末尾增加换行标识符"\n"
func (r *Response) Writeln(content ... interface{}) {
if len(content) == 0 {
r.Write("\n")
return
}
content = append(content, "\n")

View File

@ -8,7 +8,7 @@ import (
func main() {
s := g.Server()
s.BindHandler("/:name", func(r *ghttp.Request){
r.Response.Writeln(r.Router.Uri)
r.Response.Writeln(r.Router.Uri)
})
s.BindHandler("/:name/update", func(r *ghttp.Request){
r.Response.Writeln(r.Router.Uri)
@ -17,9 +17,9 @@ func main() {
r.Response.Writeln(r.Router.Uri)
})
s.BindHandler("/:name/*any", func(r *ghttp.Request){
r.Response.Writeln(r.Router.Uri)
r.Response.Writeln(r.Router.Uri)
})
s.BindHandler("/user/list/{page}.html", func(r *ghttp.Request){
s.BindHandler("/user/list/{field}.html", func(r *ghttp.Request){
r.Response.Writeln(r.Router.Uri)
})
s.SetPort(8199)

View File

@ -12,8 +12,8 @@ func main() {
r.Response.Writeln(r.Get("page"))
})
// {xxx} 规则与 :xxx 规则混合使用
s.BindHandler("/{obj}/:attr/{act}.php", func(r *ghttp.Request){
r.Response.Writeln(r.Get("obj"))
s.BindHandler("/{object}/:attr/{act}.php", func(r *ghttp.Request){
r.Response.Writeln(r.Get("object"))
r.Response.Writeln(r.Get("attr"))
r.Response.Writeln(r.Get("act"))
})

View File

@ -0,0 +1,24 @@
package main
import (
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g"
)
func main() {
s := g.Server()
// 该路由规则仅会在GET请求下有效
s.BindHandler("GET:/{table}/list/{page}.html", func(r *ghttp.Request){
r.Response.WriteJson(r.Router)
})
// 该路由规则仅会在GET请求及localhost域名下有效
s.BindHandler("GET:/order/info/{order_id}@localhost", func(r *ghttp.Request){
r.Response.WriteJson(r.Router)
})
// 该路由规则仅会在DELETE请求下有效
s.BindHandler("DELETE:/comment/{id}", func(r *ghttp.Request){
r.Response.WriteJson(r.Router)
})
s.SetPort(8199)
s.Run()
}