mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
g包中增加HTTPServer,TCPServer,UDPServer单例对象封装
This commit is contained in:
22
g/g.go
22
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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("哈喽世界!")
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user