diff --git a/g/g.go b/g/g.go index f58eaab9d..961214a69 100644 --- a/g/g.go +++ b/g/g.go @@ -17,6 +17,9 @@ import ( "gitee.com/johng/gf/g/os/gfsnotify" "gitee.com/johng/gf/g/database/gdb" "gitee.com/johng/gf/g/database/gredis" + "gitee.com/johng/gf/g/net/ghttp" + "gitee.com/johng/gf/g/net/gtcp" + "gitee.com/johng/gf/g/net/gudp" ) const ( @@ -29,6 +32,21 @@ type Map map[string]interface{} // 常用list数据结构 type List []Map +// HTTPServer单例对象 +func Server(name...interface{}) *ghttp.Server { + return ghttp.GetServer(name...) +} + +// TCPServer单例对象 +func TcpServer(name...interface{}) *gtcp.Server { + return gtcp.GetServer(name...) +} + +// UDPServer单例对象 +func UdpServer(name...interface{}) *gudp.Server { + return gudp.GetServer(name...) +} + // 核心对象:View func View() *gview.View { return gins.View() @@ -123,4 +141,6 @@ func Redis(name...string) *gredis.Redis { } } return nil -} \ No newline at end of file +} + + diff --git a/g/net/gtcp/tcp.go b/g/net/gtcp/tcp.go index 3d7a51d52..8cbf38d9e 100644 --- a/g/net/gtcp/tcp.go +++ b/g/net/gtcp/tcp.go @@ -10,6 +10,7 @@ package gtcp import ( "net" "gitee.com/johng/gf/g/container/gmap" + "gitee.com/johng/gf/g/util/gconv" ) const ( @@ -27,16 +28,16 @@ var serverMapping = gmap.NewStringInterfaceMap() // 获取/创建一个空配置的TCP Server // 单例模式,请保证name的唯一性 -func GetServer(names...string) (*Server) { - name := gDEFAULT_SERVER - if len(names) > 0 { - name = names[0] +func GetServer(name...interface{}) (*Server) { + sname := gDEFAULT_SERVER + if len(name) > 0 { + sname = gconv.String(name[0]) } - if s := serverMapping.Get(name); s != nil { + if s := serverMapping.Get(sname); s != nil { return s.(*Server) } s := NewServer("", nil) - serverMapping.Set(name, s) + serverMapping.Set(sname, s) return s } diff --git a/g/net/gudp/udp.go b/g/net/gudp/udp.go index 455ed4af5..967957943 100644 --- a/g/net/gudp/udp.go +++ b/g/net/gudp/udp.go @@ -10,6 +10,7 @@ package gudp import ( "net" "gitee.com/johng/gf/g/container/gmap" + "gitee.com/johng/gf/g/util/gconv" ) const ( @@ -27,16 +28,16 @@ var serverMapping = gmap.NewStringInterfaceMap() // 获取/创建一个空配置的UDP Server // 单例模式,请保证name的唯一性 -func GetServer(names...string) (*Server) { - name := gDEFAULT_SERVER - if len(names) > 0 { - name = names[0] +func GetServer(name...interface{}) (*Server) { + sname := gDEFAULT_SERVER + if len(name) > 0 { + sname = gconv.String(name[0]) } - if s := serverMapping.Get(name); s != nil { + if s := serverMapping.Get(sname); s != nil { return s.(*Server) } s := NewServer("", nil) - serverMapping.Set(name, s) + serverMapping.Set(sname, s) return s } diff --git a/geg/net/ghttp/hello.go b/geg/net/ghttp/hello.go index 000a026f7..be6bc7c4f 100644 --- a/geg/net/ghttp/hello.go +++ b/geg/net/ghttp/hello.go @@ -1,11 +1,12 @@ package main import ( + "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/net/ghttp" ) func main() { - s := ghttp.GetServer() + s := g.Server() s.BindHandler("/", func(r *ghttp.Request){ r.Response.Writeln("哈喽世界!") })