diff --git a/TODO b/TODO index 0194b756a..782f3ebce 100644 --- a/TODO +++ b/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. 研究是否增加配置文件目录检索功能,特别是如何友好改进开发环境的配置文件默认目录问题; \ No newline at end of file +16. 研究是否增加配置文件目录检索功能,特别是如何友好改进开发环境的配置文件默认目录问题; +17. 增加ghttp.Server不同状态码的自定义处理方法; \ No newline at end of file diff --git a/g/net/ghttp/http_server.go b/g/net/ghttp/http_server.go index 49b1b9ed2..1315bb00b 100644 --- a/g/net/ghttp/http_server.go +++ b/g/net/ghttp/http_server.go @@ -83,7 +83,7 @@ type HandlerItem struct { } // http注册函数 -type HandlerFunc func(*Request) +type HandlerFunc func(r *Request) // Server表,用以存储和检索名称与Server对象之间的关联关系 var serverMapping = gmap.NewStringInterfaceMap() diff --git a/geg/net/ghttp/status.go b/geg/net/ghttp/status.go index c68cafc66..f816f5b7e 100644 --- a/geg/net/ghttp/status.go +++ b/geg/net/ghttp/status.go @@ -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") diff --git a/geg/net/ghttp/status_map.go b/geg/net/ghttp/status_map.go new file mode 100644 index 000000000..91c39ee5c --- /dev/null +++ b/geg/net/ghttp/status_map.go @@ -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() +} \ No newline at end of file diff --git a/geg/net/ghttp/status_redirect.go b/geg/net/ghttp/status_redirect.go new file mode 100644 index 000000000..21e211d92 --- /dev/null +++ b/geg/net/ghttp/status_redirect.go @@ -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() +} \ No newline at end of file