mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
完善ghttp.Server自定义错误状态码回调函数处理示例代码
This commit is contained in:
5
TODO
5
TODO
@ -6,7 +6,7 @@ ON THE WAY:
|
||||
5. FAQ
|
||||
6. gdb对象管理增加二级连接池特性;
|
||||
7. 完善gconv类型转换功能,增加time.Time/time.Duration类型转换,并增加benchmark测试脚本
|
||||
8. 增加ghttp.Server不同状态码的自定义处理方法;
|
||||
|
||||
|
||||
|
||||
|
||||
@ -29,4 +29,5 @@ DONE:
|
||||
13. 由于去掉了gdb的单例模式,并且将gins的部分对象封装迁移到了g包中,需要同时梳理文档,完善修改;
|
||||
14. 在代码中增加https与http同时开启使用的示例代码,这块大家问得比较多;
|
||||
15. ghttp.Server多个事件之间通过ghttp.Request.Param自定义参数传参;
|
||||
16. 研究是否增加配置文件目录检索功能,特别是如何友好改进开发环境的配置文件默认目录问题;
|
||||
16. 研究是否增加配置文件目录检索功能,特别是如何友好改进开发环境的配置文件默认目录问题;
|
||||
17. 增加ghttp.Server不同状态码的自定义处理方法;
|
||||
@ -83,7 +83,7 @@ type HandlerItem struct {
|
||||
}
|
||||
|
||||
// http注册函数
|
||||
type HandlerFunc func(*Request)
|
||||
type HandlerFunc func(r *Request)
|
||||
|
||||
// Server表,用以存储和检索名称与Server对象之间的关联关系
|
||||
var serverMapping = gmap.NewStringInterfaceMap()
|
||||
|
||||
@ -9,7 +9,6 @@ func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("halo 世界!")
|
||||
r.Response.WriteStatus(404)
|
||||
})
|
||||
s.BindStatusHandler(404, func(r *ghttp.Request){
|
||||
r.Response.Writeln("This is customized 404 page")
|
||||
|
||||
17
geg/net/ghttp/status_map.go
Normal file
17
geg/net/ghttp/status_map.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindStatusHandlerByMap(map[int]ghttp.HandlerFunc {
|
||||
403 : func(r *ghttp.Request){r.Response.Writeln("403")},
|
||||
404 : func(r *ghttp.Request){r.Response.Writeln("404")},
|
||||
500 : func(r *ghttp.Request){r.Response.Writeln("500")},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
18
geg/net/ghttp/status_redirect.go
Normal file
18
geg/net/ghttp/status_redirect.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/status/:status", func(r *ghttp.Request) {
|
||||
r.Response.Write("woops, status ", r.Get("status"), " found")
|
||||
})
|
||||
s.BindStatusHandler(404, func(r *ghttp.Request){
|
||||
r.Response.RedirectTo("/status/404")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user