mirror of
https://gitee.com/johng/gf
synced 2026-07-09 06:50:30 +08:00
完善ghttp.Server路由控制示例程序
This commit is contained in:
@ -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")
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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"))
|
||||
})
|
||||
|
||||
24
geg/net/ghttp/server/router/router4.go
Normal file
24
geg/net/ghttp/server/router/router4.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user