完善ghttp.Server自定义错误状态码回调函数处理示例代码

This commit is contained in:
John
2018-05-04 16:50:54 +08:00
parent 7370278da5
commit 2c604a62cf
5 changed files with 39 additions and 4 deletions

5
TODO
View File

@ -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不同状态码的自定义处理方法

View File

@ -83,7 +83,7 @@ type HandlerItem struct {
}
// http注册函数
type HandlerFunc func(*Request)
type HandlerFunc func(r *Request)
// Server表用以存储和检索名称与Server对象之间的关联关系
var serverMapping = gmap.NewStringInterfaceMap()

View File

@ -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")

View 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()
}

View 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()
}