From d16be598f6eec16bc3c2596e45cee5fc2c78fb02 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 28 Apr 2018 12:04:00 +0800 Subject: [PATCH] =?UTF-8?q?g=E5=8C=85=E4=B8=AD=E5=A2=9E=E5=8A=A0HTTPServer?= =?UTF-8?q?,TCPServer,UDPServer=E5=8D=95=E4=BE=8B=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/g.go | 22 +++++++++++++++++++++- g/net/gtcp/tcp.go | 13 +++++++------ g/net/gudp/udp.go | 13 +++++++------ geg/net/ghttp/hello.go | 3 ++- 4 files changed, 37 insertions(+), 14 deletions(-) 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("哈喽世界!") })